From 83e1700f640bb1d1047a27150e18b6bc817a7742 Mon Sep 17 00:00:00 2001 From: pacmjqzwe <2636859704@qq.com> Date: Tue, 5 Nov 2024 17:27:19 +0800 Subject: [PATCH] ADD file via upload --- 未命名1(1).cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 未命名1(1).cpp diff --git a/未命名1(1).cpp b/未命名1(1).cpp new file mode 100644 index 0000000..ec5ec18 --- /dev/null +++ b/未命名1(1).cpp @@ -0,0 +1,144 @@ +#include +#include +int is_valid(int board[9][9], int row, int col, int num) {// У + for (int i = 0; i < 9; i++) { + if (board[row][i] == num) { + return 0; + } + } + for (int i = 0; i < 9; i++) {// + if (board[i][col] == num) { + return 0; + } + } + int startr = row - row % 3; + int startc = col - col % 3; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (board[startr + i][startc + j] == num) { + return 0; + } + } + } + return 1; +}// +int solve_sudoku(int board[9][9]) { + for (int row = 0; row < 9; row++) { + for (int col = 0; col < 9; col++) { + if (board[row][col] == 0) { + for (int num = 1; num <= 9; num++) { + if (is_valid(board, row, col, num)) { + board[row][col] = num; + if (solve_sudoku(board)) { + return 1; + } + board[row][col] = 0; + } + } + return 0; + } + } + } + return 1; +} +// +int main(){ + 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}}; + printf("The original Sudoku matrix:\n"); + printf("|-----------------------|\n"); + for(int i=0;i<9;i++){ + printf("| "); + for(int j=0;j<9;j++){ + printf("%d ",board[i][j]); + if((j+1)%3==0){ + printf("| "); + } + } + printf("\n"); + if((i+1)%3==0) { + printf("|-----------------------|\n"); + } + // + }int a=0,b=0,c=0,d=1; + for(int i=0;i<9;i++){ + for(int m=1;m<=9;m++){ + for(int j=0;j<9;j++){ + if(board[i][j]==m){ + a=a+1; + if(a>1){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the row %d has been used!\n",m,i+1); + return 0; + } + } + }a=0; + + } + } + //еж + for(int start=0;start<=6;){ + for(int end=0;end<=6;){ + for(int m=1;m<9;m++){ + for(int i=start;i<3;i++){ + for(int j=end;j<3;j++){ + if(board[i][j]==m){ + c=c+1;} + if(c>1){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the block %d has been used!\n",m,d); + return 0; + } + } + } + c=0; + } + end=end+3; + d=d+1; + } + start=start+3; + } + //жϣ + for(int i=0;i<9;i++){ + for(int m=1;m<9;m++){ + for(int j=0;j<9;j++){ + if(board[j][i]==m){ + b=b+1;} + if(b>1){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the col %d has been used!\n",m,i+1); + return 0; + } + }b=0; + }b=0; + } +printf("True:Valid initial Sudoku matrix!\n"); + //ǶжϣǷΪ +if (solve_sudoku(board)) { +printf("The solution of Sudoku matrix:\n"); +printf("|-----------------------|\n"); + for(int i=0;i<9;i++){ + printf("| "); + for(int j=0;j<9;j++){ + printf("%d ",board[i][j]); + if((j+1)%3==0){ + printf("| "); + } + } + printf("\n"); + if((i+1)%3==0) { + printf("|-----------------------|\n"); + } + // + } +}else { + printf("No solution!\n");} + return 0; +}