[SWEA] 1974 스도쿠 검증

cheeeese·2022년 7월 4일
0

코딩테스트 연습

목록 보기
113/151
post-thumbnail

📖 문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5Psz16AYEDFAUq&categoryId=AV5Psz16AYEDFAUq&categoryType=CODE&problemTitle=1974&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

💻 내 코드

python

def check(mlist):
    ans=1
    for i in range(9):
        if len(set(mlist[i]))==9:
            ans=1
        else:
            return 0
    for i in range(9):
        klist=[]
        for j in range(9):
            klist.append(mlist[j][i])
        if len(set(klist))==9:
            ans=1
        else:
            return 0

    for i in range(0, 7, 3):
        for x in range(0, 7, 3):
            klist=[]
            for j in range(3):
                for k in range(3):
                    klist.append(mlist[j+i][k+x])
            if len(set(klist))==9:
                ans=1
            else:
                return 0
    return 1

t=int(input())

for tc in range(1, t+1):
    mlist=[]
    
    for i in range(9):
        mlist.append(list(map(int, input().split())))
    
    print(f"#{tc} {check(mlist)}")

Java-Hashset을 사용한 코드

import java.util.HashSet;
import java.util.Scanner;

public class d2_1974 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();

		for (int tc = 1; tc <= t; tc++) {
			int[][] arr = new int[9][9];

			for (int i = 0; i < 9; i++) {
				for (int j = 0; j < 9; j++) {
					arr[i][j] = sc.nextInt();
				}
			}
			
			System.out.println("#"+t+" "+check(arr));
		}

	}

	private static int check(int[][] mlist) {
		
		int ans=1;
		HashSet<Integer> hs= new HashSet<Integer>();

		
		for(int i=0;i<9;i++) {

			for(int j=0;j<9;j++) {
				hs.add(mlist[i][j]);
			}
			if (hs.size()==9) {
				ans=1;
			}
			else {
				return 0;
			}
			hs.clear();
				
		}
		
		for(int i=0;i<9;i++) {

			for(int j=0;j<9;j++) {
				hs.add(mlist[j][i]);
			}
			if (hs.size()==9) {
				ans=1;
			}
			else {
				return 0;
			}
			hs.clear();
	
		}
		
		for(int i=0;i<7;i+=3) {
			for(int j=0;j<7;j+=3) {

				for(int x=0;x<3;x++) {
					for(int y=0;y<3;y++) {
						hs.add(mlist[x+i][y+j]);

					}
				}
				if(hs.size()==9) {
					ans=1;
				}
				else {
					return 0;
				}
				hs.clear();

			}
		}
		return 1;
	}

}

Java-합을 이용한 풀이

import java.util.Scanner;

public class d2_1974 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();

		for (int tc = 1; tc <= t; tc++) {
			int[][] arr = new int[9][9];

			for (int i = 0; i < 9; i++) {
				for (int j = 0; j < 9; j++) {
					arr[i][j] = sc.nextInt();
				}
			}

			System.out.println("#" + tc + " " + check(arr));
		}

	}

	private static int check(int[][] mlist) {

		int ans = 1;
		int res = 0;

		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				res += mlist[i][j];
			}

			if (res == 45) {
				ans = 1;
				res = 0;
			} else {
				return 0;
			}

		}

		for (int i = 0; i < 9; i++) {

			for (int j = 0; j < 9; j++) {
				res += mlist[j][i];
			}
			if (res == 45) {
				ans = 1;
				res = 0;
			} else {
				return 0;
			}

		}

		for (int i = 0; i < 7; i += 3) {
			for (int j = 0; j < 7; j += 3) {

				for (int x = 0; x < 3; x++) {
					for (int y = 0; y < 3; y++) {
						res += mlist[x + i][y + j];

					}
				}
				if (res == 45) {
					ans = 1;
					res = 0;
				} else {
					return 0;
				}

			}
		}
		return 1;
	}

}

💡 풀이

1, 2)

  • python의 set과 java의 HashSet은 중복된 값은 저장되지 않는다
  • 따라서 각 열, 행, 사각형의 수를 set, HashSet에 저장했을 때 1부터 9까지 모두 중복 없이 있으면 길이(크기)가 9가 됨
  • 만약 9보다 작다면 중복된 값이 있어 그 수는 한 개만 저장된 것이므로 1을 return

3)

  • 1부터 9까지 중복없이 저장되었다면 합이 45임을 이용

0개의 댓글