import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Sudoku {
static int[][] map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
map = new int[9][9];
for (int i = 0; i < 9; i++) {
String str = br.readLine();
for (int j = 0; j < 9; j++) {
map[i][j] = str.charAt(j)-'0';
}
}
dfs(0,0);
}
public static void dfs(int row, int col){
if(row==9){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sb.append(map[i][j]);
}
sb.append("\n");
}
System.out.println(sb);
System.exit(0);
}
if(col==9){
dfs(row+1,0);
return;
}
if(map[row][col]!=0){
dfs(row,col+1);
}
else if(map[row][col]==0){
for (int i = 1; i <= 9; i++) {//들어갈 숫자 완전탐색
boolean rowflag = false;
for (int j = 0; j < 9; j++) {//중복확인
if(map[row][j] == i){
rowflag = true;
break;
}
}
boolean colflag = false;
for (int j = 0; j < 9; j++) {
if(map[j][col] == i){
colflag = true;
break;
}
}
boolean squareflag = false;
squareflag = squareCheck(row,col,i);
if(!rowflag&&!colflag&&!squareflag){
map[row][col] = i;
dfs(row,col+1);
map[row][col] = 0;
}
}
}
}
public static boolean squareCheck(int row,int col,int num){
if(row<3){
row = 0;
}
else if(row<6){
row = 3;
} else if (row <9) {
row = 6;
}
if(col<3){
col = 0;
}
else if(col<6){
col = 3;
}
else if(col<9){
col = 6;
}
for (int i = row; i <row+3; i++) {
for (int j = col; j < col+3; j++) {
if(map[i][j] == num){
return true;
}
}
}
return false;
}
}
전체적인 풀이의 설계는 완전탐색을 바탕으로 첫번째 열부터 열을 하나씩 늘려가며 dfs탐색을 수행한다.
마지막열에서는 행을 하나 늘려서 dfs탐색을 해주고, 마지막 행에 도착하면 스도쿠를 끝낸다.
map[row][col] = i;
를 통해 값을 넣어 줬기 때문에,if(map[row][col]!=0)
조건식에 걸리게 되어 1번 조건이 실행된다.map[row][col] = 0;
을 통해 원상복귀를 시켜준다.2가지가 핵심이었고, 2번에서 헤매긴 했지만, 구현,dfs,백트래킹 3가지의 알고리즘 실력을 늘릴 수 있었던 좋은 문제였다.
나중에 다시 풀어봐야겠다.