|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
int sudoku[9][9];
|
|
|
|
|
|
|
|
|
|
//<2F>ж<EFBFBD><D0B6><EFBFBD>
|
|
|
|
|
int in_row(int row, int num) {
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
if (sudoku[row][i] == num) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F>ж<EFBFBD><D0B6><EFBFBD>
|
|
|
|
|
int in_col(int col, int num) {
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
if (sudoku[i][col] == num) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F>жϹ<D0B6>
|
|
|
|
|
int in_box(int row, int col, int num) {
|
|
|
|
|
int box_row = row / 3 * 3;
|
|
|
|
|
int box_col = col / 3 * 3;
|
|
|
|
|
for (int i = box_row; i < box_row + 3; i++) {
|
|
|
|
|
for (int j = box_col; j < box_col + 3; j++) {
|
|
|
|
|
if (sudoku[i][j] == num) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int is_valid(int row, int col, int num) {
|
|
|
|
|
return !in_row(row, num) && !in_col(col, num) && !in_box(row, col, num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int solve_sudoku(int row, int col) {
|
|
|
|
|
if (row == 9) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (col == 9) { // <20><><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>꣬ת<EAA3AC><D7AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
return solve_sudoku(row + 1, 0);
|
|
|
|
|
}
|
|
|
|
|
if (sudoku[row][col] != 0) { // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return solve_sudoku(row, col + 1);
|
|
|
|
|
}
|
|
|
|
|
for (int num = 1; num <= 9; num++) { //ö<>ٻ
|
|
|
|
|
if (is_valid(row, col, num)) {
|
|
|
|
|
sudoku[row][col] = num;
|
|
|
|
|
if (solve_sudoku(row, col + 1)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
sudoku[row][col] = 0; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
|
scanf("%d", &sudoku[i][j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (solve_sudoku(0, 0)) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
|
printf("%d ", sudoku[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf("No solution!\n");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|