diff --git a/t4.cpp b/t4.cpp deleted file mode 100644 index e6d5b4f..0000000 --- a/t4.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include -int f[9] = {0}, copy[9][9] = {0}; -int t1[9][9] = {0}, t2[9][9] = {0}, t3[9][9] = {0}; -// int board[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, -// {6, 0, 0, 1, 9, 5, 0, 0, 0}, -// {0, 9, 8, 0, 0, 0, 0, 6, 0}, -// {8, 0, 0, 0, 6, 0, 0, 0, 3}, -// {4, 0, 0, 8, 0, 3, 0, 0, 1}, -// {7, 0, 0, 0, 2, 0, 0, 0, 6}, -// {0, 6, 0, 0, 0, 0, 2, 8, 0}, -// {0, 0, 0, 4, 1, 9, 0, 0, 5}, -// {0, 0, 0, 0, 8, 0, 0, 7, 9}}; -// int board[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0}, -// {6, 0, 0, 1, 9, 5, 0, 0, 0}, -// {0, 9, 8, 0, 0, 0, 0, 6, 0}, -// {8, 0, 0, 0, 6, 0, 0, 0, 3}, -// {4, 0, 0, 8, 0, 3, 0, 0, 1}, -// {7, 0, 0, 0, 2, 0, 0, 0, 6}, -// {0, 6, 0, 0, 0, 0, 2, 8, 0}, -// {0, 0, 0, 4, 1, 9, 0, 0, 5}, -// {0, 0, 0, 0, 8, 0, 0, 7, 9}}; -int board[9][9] = {{5, 2, 0, 0, 7, 0, 0, 0, 0}, -{6, 0, 0, 1, 9, 5, 0, 0, 0}, -{0, 9, 8, 0, 0, 0, 0, 6, 0}, -{8, 0, 0, 0, 6, 0, 0, 0, 3}, -{4, 0, 0, 8, 0, 3, 0, 0, 1}, -{7, 0, 0, 0, 2, 0, 0, 0, 6}, -{0, 6, 0, 0, 0, 0, 2, 8, 0}, -{0, 0, 0, 4, 1, 9, 0, 0, 5}, -{0, 0, 0, 0, 8, 0, 0, 7, 9}}; -int ifans = 0; -void f1() -{ - printf("|"); - for (int i = 0; i < 23; i++) - { - printf("-"); - } - printf("|\n"); -} -void operate() -{ - for (int i = 0; i < 9; i++) - { - if (i % 3 == 0) - { - f1(); - } - printf("| "); - for (int j = 0; j < 9; j++) - { - if (j % 3 == 0 && j) - { - printf("| "); - } - printf("%d ", board[i][j]); - } - printf("|\n"); - } - f1(); -} -int judge() -{ - for (int i = 0; i < 9; i++) - { - for(int j = 0; j < 9; j++) - { - if (board[i][j]) - { - t1[i][board[i][j] - 1]++; - t2[j][board[i][j] - 1]++; - t3[(i / 3) * 3 + j / 3][board[i][j] - 1]++; - } - } - } - for (int i = 0; i < 9; i++) - { - for (int num = 0; num < 9; num++) - { - if (t1[i][num] > 1) - { - printf("False:Invalid initial Sudoku matrix!\n"); - printf(" The number %d in the row %d has been used!\n", num + 1, i + 1); - printf("No solution!"); - return 0; - } - else if (t2[i][num] > 1) - { - printf("False:Invalid initial Sudoku matrix!\n"); - printf(" The number %d in the col %d has been used!\n", num + 1, i + 1); - printf("No solution!"); - return 0; - } - else if (t3[i][num] > 1) - { - printf("False:Invalid initial Sudoku matrix!\n"); - printf(" The number %d in the block %d has been used!\n", num + 1, i + 1); - printf("No solution!"); - return 0; - } - } - } - printf("True:Valid initial Sudoku matrix!\n"); - return 1; -} -void fill(int x, int y) -{ - int num; - if (x == 9 && y == 0) - { - printf("The solution of Sudoku matrix:\n"); - operate(); - ifans = 1; - } - else - { - if (copy[x][y]) - { - if (y == 8) - { - fill(x + 1, 0); - if (x == 0 && y == 0 && ifans == 0) - { - printf("No solution!"); - } - return; - } - else{ - fill(x, y + 1); - if (x == 0 && y == 0 && ifans == 0) - { - printf("No solution!"); - } - return; - } - } - else - { - for(num = 1; num <= 9; num++) - { - if (t1[x][num - 1] == 0 && t2[y][num - 1] == 0 && t3[(x / 3) * 3 + y / 3][num - 1] == 0) - { - board[x][y] = num; - t1[x][num - 1] = 1; - t2[y][num - 1] = 1; - t3[(x / 3) * 3 + y / 3][num - 1] = 1; - if (y == 8) - { - // operate(); - fill(x + 1, 0); - board[x][y] = 0; - t1[x][num - 1] = 0; - t2[y][num - 1] = 0; - t3[(x / 3) * 3 + y / 3][num - 1] = 0; - } - else{ - // operate(); - fill(x, y + 1); - board[x][y] = 0; - t1[x][num - 1] = 0; - t2[y][num - 1] = 0; - t3[(x / 3) * 3 + y / 3][num - 1] = 0; - } - } - } - if (x == 0 && y == 0 && ifans == 0) - { - printf("No solution!"); - } - return; - } - } -} -int main() -{ - printf("The original Sudoku matrix:\n"); - operate(); - for (int i = 0; i < 9; i++) - { - for (int j = 0; j < 9; j++) - { - copy[i][j] = board[i][j]; - } - } - if(judge()) - { - fill(0,0); - } -} \ No newline at end of file