Compare commits

..

No commits in common. 'main' and 'main' have entirely different histories.
main ... main

@ -1,178 +0,0 @@
#include <stdio.h>
int board0[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 board1[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 board2[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 err_group=0,err_num=0;
int check_submatrix(int * ptr){
int bottle[10]={0};
int * cur_ptr=ptr;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
bottle[*(cur_ptr+j)]++;
//printf("%d ",*(cur_ptr+j));
}
//printf("\n");
cur_ptr+=9;
}
for(int i=1;i<=9;i++){
//printf("%d",bottle[i]);
if(bottle[i]>1){
err_num = i;
return 0;
}
}
return 1;
}
int check_col(int * ptr){
int bottle[10]={0};
int * cur_ptr=ptr;
for(int i=0;i<9;i++){
bottle[* cur_ptr]++;
//printf("%d \n",* cur_ptr);
cur_ptr+=9;
}
for(int i=1;i<=9;i++){
//printf("%d",bottle[i]);
if(bottle[i]>1){
err_num = i;
return 0;
}
}
return 1;
}
int check_row(int * ptr){
int bottle[10]={0};
int * cur_ptr=ptr;
for(int i=0;i<9;i++){
bottle[* cur_ptr]++;
//printf("%d ",* cur_ptr);
cur_ptr++;
}
for(int i=1;i<=9;i++){
//printf("%d",bottle[i]);
if(bottle[i]>1){
err_num = i;
return 0;
}
}
return 1;
}
int check_whole(int * ptr,int output_on){
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
if(!check_submatrix(&ptr[i*9+j])){
if(output_on){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!",err_num,i+j/3+1);
}
return 0;
}
}
}
for(int i=0;i<9;i++){
if(!check_row(&ptr[i*9+0])){
if(output_on) {
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!", err_num, i);
}
return 0;
}
}
for(int i=0;i<9;i++){
if(!check_col(&ptr[i])){
if(output_on){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!",err_num,i);
}
return 0;
}
}
if(output_on){
printf("True:Valid initial Sudoku matrix!\n");
}
return 1;
}
int dfs(int * ptr,int * start_pos){
for(int * i=ptr;i<start_pos+81;i++){
if((* i)==0){
for(int j=1;j<=9;j++){
* i = j;
if(check_whole(start_pos,0) && dfs(i+1,start_pos))return 1;
}
* i =0;
return 0;
}
}
return 1;
}
void printGrid(int grid[9][9]) {
int i, j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
printf("%2d ", grid[i][j]);
if ((j + 1) % 3 == 0 && j < 8) {
printf("| ");
}
}
printf("\n");
if ((i + 1) % 3 == 0 && i < 8) {
printf("------------------------------\n");
}
}
}
int Sudoku_validator(int grid[9][9]){
printf("The original Sudoku matrix:\n");
printGrid(grid);
check_whole(&grid[0][0],1);
if(dfs(&grid[0][0],&grid[0][0])) {
printf("The solution of Sudoku matrix:\n");
printGrid(grid);
}
else printf("No solution");
return 0;
}
int main(void) {
Sudoku_validator(board2);
}

@ -1,146 +0,0 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
bool isValid(int grid[9][9], int r, int c, int num);
void generateIncompleteGrid(int grid[9][9]) {
srand(time(NULL));
int i, j, block;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
grid[i][j] = 0;
}
}
for (block = 0; block < 3; block++) {
int used[10] = {0};
for (i = 0; i < 3; i++) {
int r = block * 3 + i;
int placedNum = 0;
while (placedNum < 3) {
int num = (rand() % 9) + 1;
int c = rand() % 9;
if (grid[r][c] == 0 && isValid(grid, r, c, num) && used[num] == 0) {
grid[r][c] = num;
used[num] = 1;
placedNum++;
}
}
}
}
}
void print(int grid[9][9]) {
int i, j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
printf("%2d ", grid[i][j]);
if ((j + 1) % 3 == 0 && j < 8) {
printf("| ");
}
}
printf("\n");
if ((i + 1) % 3 == 0 && i < 8) {
printf("-----------------------\n");
}
}
}
bool isValid(int grid[9][9], int r, int c, int num) {
int x, i, j;
for (x = 0; x < 9; x++) {
if (grid[r][x] == num || grid[x][c] == num) {
return false;
}
}
int startR = r - r % 3, startC = c - c % 3;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (grid[i + startR][j + startC] == num) {
return false;
}
}
}
return true;
}
bool solve(int grid[9][9]) {
int r, c;
bool empty = false;
for (r = 0; r < 9; r++) {
for (c = 0; c < 9; c++) {
if (grid[r][c] == 0) {
empty = true;
break;
}
}
if (empty) break;
}
if (!empty) return true;
int num;
for (num = 1; num <= 9; num++) {
if (isValid(grid, r, c, num)) {
grid[r][c] = num;
if (solve(grid)) {
return true;
}
grid[r][c] = 0;
}
}
return false;
}
bool validate(int grid[9][9]) {
int r, c;
for (r = 0; r < 9; r++) {
for (c = 0; c < 9; c++) {
if (grid[r][c] != 0) {
int num = grid[r][c];
grid[r][c] = 0;
if (!isValid(grid, r, c, num)) {
return false;
}
grid[r][c] = num;
}
}
}
return true;
}
int main() {
int grid[9][9];
int board0[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}};
generateIncompleteGrid(grid);
printf("The original Sudoku matrix:\n");
print(board0);
if (validate(board0)) {
printf("True:Valid initial Sudoku matrix!\n");
if (solve(board0)) {
printf("Completed Sudoku Grid:\n");
print(board0);
} else {
printf("No solution exists for this Sudoku.\n");
}
} else {
printf("False:Invalid initial Sudoku matrix!\n");
}
return 0;
}

@ -1,17 +0,0 @@
#include <stdio.h>
void printGrid(int grid[9][9]) {
int i, j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
printf("%2d ", grid[i][j]);
if ((j + 1) % 3 == 0 && j < 8) {
printf("| ");
}
}
printf("\n");
if ((i + 1) % 3 == 0 && i < 8) {
printf("-----------------------\n");
}
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save