Notice
Recent Posts
Link
- Today
- Total
hye-log
[SWEA]1974번 스도쿠 검증(JAVA) 본문
0. 문제 링크
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5Psz16AYEDFAUq#none
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
1. 문제 설명
1) 가로 줄에 1~9 까지 있는지 확인
2) 세로 줄에 1~9 까지 있는지 확인
3) 3x3 격자에 1~9 까지 있는지 확인
2. 입출력
// input
10
7 3 6 4 2 9 5 8 1
5 8 9 1 6 7 3 2 4
2 1 4 5 8 3 6 9 7
8 4 7 9 3 6 1 5 2
1 5 3 8 4 2 9 7 6
9 6 2 7 5 1 8 4 3
4 2 1 3 9 8 7 6 5
3 9 5 6 7 4 2 1 8
6 7 8 2 1 5 4 3 9
…
// output
#1 1
3. 코드
import java.util.*;
public class swea1974 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t = 1; t <= T; t++) {
int[][] map = new int[9][9]; // 스도쿠 맵
boolean flag = true; // 겹치는 숫자가 없으면 true
for(int i = 0; i < 9; i++) { // 맵 채우기
for(int j = 0; j < 9; j++) {
map[i][j] = sc.nextInt();
}
}
// 가로
for(int i = 0; i < 9; i++) {
int[] arr = new int[9];
for(int j = 0; j < 9; j++) {
arr[map[i][j]-1]++;
}
for(int k = 0; k < 9; k++) {
if(arr[k] == 0) {
flag = false;
break;
}
}
}
// 세로
for(int i = 0; i < 9; i++) {
int[] arr = new int[9];
for(int j = 0; j < 9; j++) {
arr[map[j][i]-1]++;
}
for(int k = 0; k < 9; k++) {
if(arr[k] == 0) {
flag = false;
break;
}
}
}
// 격자
for(int i = 0; i <= 6; i += 3) {
for(int j = 0; j <= 6; j += 3) {
int[] arr = new int[9];
int r = i+3;
int c = j+3;
for(int m = i; m < r; m++) {
for(int n = j; n < c; n++) {
arr[map[m][n]-1]++;
}
}
for(int k = 0; k < 9; k++) {
if(arr[k] == 0) {
flag = false;
break;
}
}
}
}
if(flag == true) {
System.out.printf("#%d %d\n", t, 1);
} else {
System.out.printf("#%d %d\n", t, 0);
}
}
}
}
실행 결과
4. 회고
1) for 문을 통한 배열 완전 탐색 구현
5. Github
https://github.com/iihye/Algorithm/blob/main/SWEA/swea1974.java
728x90
'CodingTest > SWEA' 카테고리의 다른 글
[SWEA]5432번 쇠막대기 자르기(JAVA) (0) | 2023.07.30 |
---|---|
[SWEA]1940번 가랏! RC카!(JAVA) (0) | 2023.07.30 |
[SWEA]1208번 Flatten(JAVA) (0) | 2023.07.30 |
[SWEA]1234번 비밀번호(JAVA) (0) | 2023.07.29 |
[SWEA]2805번 농작물 수확하기(JAVA) (0) | 2023.07.29 |
Comments