#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int arr[9][9];
bool Check_row(int target, int row_num) {
for (int col_num = 0; col_num < 9; col_num++) {
if (arr[row_num][col_num] == target) return false;
}
return true;
}
bool Check_col(int target, int col_num) {
for (int row_num = 0; row_num < 9; row_num++) {
if (arr[row_num][col_num] == target) return false;
}
return true;
}
bool Check_square(int target, int row_num, int col_num) {
int row = row_num / 3 * 3;
int col = col_num / 3 * 3;
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
if (arr[i][j] == target) return false;
}
}
return true;
}
bool Check_target(int target, int row_num, int col_num) {
if (Check_row(target, row_num) && Check_col(target, col_num) && Check_square(target, row_num, col_num)) return true;
return false;
}
void Print_sdoku() {
for (int u = 0; u < 9; u++) {
for (int y = 0; y < 9; y++) {
printf("%d ", arr[u][y]);
}
printf("\n");
}
exit(0);
}
void Dfs(int a, int b) {
if (arr[a][b] != 0) {
if (a == 8 && b == 8) Print_sdoku();
if (b + 1 == 9) Dfs(a + 1, 0);
else Dfs(a, b + 1);
}
else {
for (int j = 1; j <= 9; j++) {
if (Check_target(j, a, b)) {
arr[a][b] = j;
if (a == 8 && b == 8) Print_sdoku();
if (b + 1 == 9) Dfs(a + 1, 0);
else Dfs(a, b + 1);
arr[a][b] = 0;
}
}
return;
}
}
int main() {
int i, j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
scanf("%d", &arr[i][j]);
}
}
Dfs(0, 0);
}
Test Case 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Test Case 1(출력 예시)
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2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