You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nailongnailong/3.cpp

36 lines
1.3 KiB

#include <stdio.h>
#include <stdbool.h>
bool panduan(int board[9][9]) {
for (int i = 0; i < 9; i++) {
bool row[10] = { 0 }, col[10] = { 0 }, block[10] = { 0 };
for (int j = 0; j < 9; j++) {
if (board[i][j] != 0) {
if (row[board[i][j]]) {
printf("The number %d in row %d has been used!\n", board[i][j], i);
return false;
}
row[board[i][j]] = true;
}
if (board[j][i] != 0) {
if (col[board[j][i]]) {
printf("The number %d in column %d has been used!\n", board[j][i], i);
return false;
}
col[board[j][i]] = true;
}
int blockRow = 3 * (i / 3), blockCol = 3 * (i % 3);
if (board[blockRow + j / 3][blockCol + j % 3] != 0) {
if (block[board[blockRow + j / 3][blockCol + j % 3]]) {
printf("The number %d in block %d has been used!\n",
board[blockRow + j / 3][blockCol + j % 3], (i / 3) * 3 + (i % 3));
return false;
}
block[board[blockRow + j / 3][blockCol + j % 3]] = true;
}
}
}
return true;
}