From 584b3ea49fcec3c643cd38a267741f9167e1c0a5 Mon Sep 17 00:00:00 2001 From: pr769xcqp <1163930010@qq.com> Date: Fri, 8 Nov 2024 16:39:30 +0800 Subject: [PATCH] ADD file via upload --- 任务4.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 任务4.c diff --git a/任务4.c b/任务4.c new file mode 100644 index 0000000..aeab11f --- /dev/null +++ b/任务4.c @@ -0,0 +1,114 @@ +#include +#include + +#define SIZE 9 +void geshihua(int arr0[9][9]) +{ + int i,j; + printf("|"); + for(i=0;i<9;i++) + { + for(j=0;j<9;j++) + { + printf("%d ",arr0[i][j]); + + if(j==8) + { + printf("|\n"); + } + if(i==8&&j==8) + break; + if((i+1)%3==0&&j==8&&i!=8) + { + printf("|--------------------|\n"); + } + if((j+1)%3==0) + { + printf("|"); + + } + + } + } +} + +// ¼ì²éÐÐÖÐÊÇ·ñÓÐÖظ´Êý×Ö +bool checkRow(int board[SIZE][SIZE], int row, int num) { + int col; + for (col = 0; col < SIZE; col++) { + if (board[row][col] == num && board[row][col] != 0) { // Ö»¼ì²é·ÇÁãÔªËØ + return false; + } + } + return true; +} + +// ¼ì²éÁÐÖÐÊÇ·ñÓÐÖظ´Êý×Ö +bool checkColumn(int board[SIZE][SIZE], int col, int num) { + int row; + for (row = 0; row < SIZE; row++) { + if (board[row][col] == num && board[row][col] != 0) { // Ö»¼ì²é·ÇÁãÔªËØ + return false; + } + } + return true; +} + +// ¼ì²é3x3×Ó¾ØÕóÖÐÊÇ·ñÓÐÖظ´Êý×Ö +bool checkBox(int board[SIZE][SIZE], int rowStart, int colStart, int num) { + int row,col; + for (row = 0; row < 3; row++) { + for (col = 0; col < 3; col++) { + if (board[row + rowStart][col + colStart] == num && board[row + rowStart][col + colStart] != 0) { // Ö»¼ì²é·ÇÁãÔªËØ + return false; + } + } + } + return true; +} + +// ¼ì²é³õʼÊý¶À¾ØÕóÊÇ·ñÓÐЧ +bool isValidInitialSudoku(int board[SIZE][SIZE]) { + int row,col; + for (row = 0; row < SIZE; row++) { + for (col = 0; col < SIZE; col++) { + if (board[row][col] != 0) { // Ö»¼ì²é·ÇÁãÔªËØ + int num = board[row][col]; + if (!checkRow(board, row, num) || !checkColumn(board, col, num) || !checkBox(board, row - row % 3, col - col % 3, num)) { + // Êä³ö²»Âú×ãµÄÔ­Òò + if (!checkRow(board, row, num)) { + printf("False:Invalid initial Sudoku matrix! The number %d in the row %d has been used!\n", num, row + 1); + } else if (!checkColumn(board, col, num)) { + printf("False:Invalid initial Sudoku matrix! The number %d in the column %d has been used!\n", num, col + 1); + } else { + printf("False:Invalid initial Sudoku matrix! The number %d in the block %d has been used!\n", num, (row / 3) * 3 + 1); // ¼ò»¯Êä³ö£¬Ö»±íʾÊÇÄĸö3x3¿é£¬²»¾«È·µ½ÁÐ + } + return false; + } + } + } + } + printf("True:Valid initial Sudoku matrix!\n"); + return true; +} + +int main() { + printf("¡°The original Sudoku matrix:\n"); + int board[SIZE][SIZE] = { + // ÕâÀïÌîÈëÄãµÄ²Ðȱ¾ØÕó£¬0±íʾ¿Õȱ²¿·Ö£¬·ÇÁãÊý×Ö±íʾÒѸø³öµÄÊý×Ö + {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} + }; + geshihua(board); + + isValidInitialSudoku(board); + + return 0; +}