1. ๋ด ์ฝ๋
import java.util.HashSet;
import java.util.Scanner;
class Solution
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int T;
T=sc.nextInt();
Test:
for (int test_case = 1; test_case <= T; test_case++) {
// 1. 2์ฐจ์ ๋ฐฐ์ด๋ก ๊ฐ๋ค์ ๋ฐ๋๋ค.
int[][] sudoku = new int[10][10];
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudoku[i][j] = sc.nextInt();
}
}
// --> 2์ฐจ์ ๋ฐฐ์ด์ ๊ฐ ์๊ฑด์ ๋ง๊ฒ Hash set์ ๋ฃ๊ณ set์ size๊ฐ 9๊ฐ ์๋๋ฉด, 0 ์ถ๋ ฅํ๋ฉด ๋ ๋ฏ.
// 2. ๊ฐ๋ก์์ ๊ฒน์น๋ ๊ฐ์ด ์๋์ง ๊ฒ์ฆํ๋ค. -> ์์ผ๋ฉด ๋ฐ๋ก 0 ์ถ๋ ฅ ํ continue
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
set.add(sudoku[i][j]);
}
if(set.size() != 9){
System.out.printf("#%d %d\n" ,test_case, 0);
continue Test;
}
set.clear();
}
// 3. ์ธ๋ก์์ ๊ฒน์น๋ ๊ฐ์ด ์๋์ง ๊ฒ์ฆํ๋ค. -> ์์ผ๋ฉด ๋ฐ๋ก 0 ์ถ๋ ฅ ํ continue
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
set.add(sudoku[j][i]);
}
if(set.size() != 9){
System.out.printf("#%d %d\n" ,test_case, 0);
continue Test;
}
set.clear();
}
// 4. 3x3์์ ๊ฒน์น๋ ๊ฐ์ด ์๋์ง ๊ฒ์ฆํ๋ค. -> ์์ผ๋ฉด ๋ฐ๋ก 0 ์ถ๋ ฅ ํ continue
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = i*3; k <= (i*3)+2; k++) {
for (int l = j*3; l <= (j*3)+2; l++) {
set.add(sudoku[k][l]);
}
}
if(set.size() != 9){
System.out.printf("#%d %d\n" ,test_case, 0);
continue Test;
}
set.clear();
}
}
System.out.printf("#%d %d\n" ,test_case, 1);
}
}
}
2. ๋ค๋ฅธ ์ฌ๋ ์ฝ๋
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Solution {
public static void main(String args[]) throws Exception {
//System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int[][] arr = new int[9][9];
for (int test_case = 1; test_case <= T; test_case++) {
// ๋ฐฐ์ด ์
๋ ฅ
for (int i = 0; i < 9; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 9; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
// 1~9๊น์ง, ์ค๋ณต๋๋ ์ซ์๊ฐ x > 1์ถ๋ ฅ, ์๋๋ฉด 0์ถ๋ ฅ
// ์ดํฉ์ 45
int answer = 0;
for (int i = 0; i < 9; i++) {
int hap1 = 0; // ๊ฐ๋ก ํฉ
int hap2 = 0; // ์ธ๋ก ํฉ
int hap3 = 0; // 3x3 ๋ถ๋ถ๋ฐฐ์ด ํฉ
for (int j = 0; j < 9; j++) {
hap1 += arr[i][j];
hap2 += arr[j][i];
hap3 += arr[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3];
}
if (hap1 != 45 || hap2 != 45 || hap3 != 45) {
answer = 0;
break;
} else {
answer = 1;
}
}
System.out.print("#" + test_case + " ");
System.out.println(answer);
}
}
}
3x3 ๊ณ์ฐ ๋ถ๋ถ์ด ์ธ์ ๊น๋ค.
ํด๋น ๋ถ๋ถ์ ๋ก์ง์ ์ด๋ ๊ฒ ๋์๊ฐ๋ค.
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
// ํ์ i ๊ณ์ฐ ๋ถ๋ถ์ i๊ฐ 0,1,2์ผ ๋ 0 / 3,4,5์ผ ๋ 3 / 6,7,8์ผ ๋ 6
// ํ์ j ๊ณ์ฐ ๋ถ๋ถ์ j๊ฐ 0,1,2์ผ ๋ 0/ 3,4,5์ผ ๋ 1, / 6,7,8์ผ ๋ 2
// ์ด์ i ๊ณ์ฐ ๋ถ๋ถ์ i๊ฐ 0,3,6์ผ๋ 0 / 1,4,7์ผ๋ 3 / 2,5,8์ผ๋ 6์ฉ ๋ํด์ค
// ์ด์ j ๊ณ์ฐ ๋ถ๋ถ์ j๊ฐ 0,3,6์ผ๋ 0 / 1,4,7์ผ๋ 1 / 2,5,8์ผ๋ 2์ฉ ๋ํด์ค
System.out.printf("[%d][%d] ",((i / 3) * 3 )+ (j / 3),((i % 3) * 3 )+ (j % 3));
}
System.out.println();
}
์ด๋ฐ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค๋ ๋๋จํ๋ค.
ํ๊ณผ ์ด์ i ๋ถ๋ถ์ ํฐ ํฑ๋๋ฐํด, j ๋ถ๋ถ์ ์์ ํฑ๋๋ฐํด๋ผ ๋ณผ ์ ์๋ค.
0