ADD file via upload

main
p3z2sacvh 1 year ago
parent fc6628b848
commit 6fe6cb6a99

@ -0,0 +1,136 @@
#include<stdio.h>
int board[9][9] = {{5, 3, 0, 0, 9, 0, 0, 0, 0},
{6, 0, 0, 1, 7, 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, 0},
{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, 2, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
//有什么方法可以做到输入多维数组的一行到定义的函数里面
int main(void)
{
printf("The original Sudoku matrix:\n");
for(int i=0;i<9;i++)
{
if(i%3==0)printf(" |----------------------|\n");
for(int j=0;j<9;j++)
{
if(j%3==0)printf(" |");
if(board[i][j]==0) printf(" .");
else printf(" %d",board[i][j]);
}
putchar('|');
putchar('\n');
}
printf(" |----------------------|\n");
//判断行
int count1=0;
int storage1;
int judge1=0;
for(int i=0;i<9;i++)
{
for(int n=1;n<10;n++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]==n)count1++;
if(count1>1)
{
judge1=1;
storage1=n;
break;
}
}
count1=0;
//判断
if(judge1)
{
printf("False:Invalid initial Sudoku matrix\n");
printf("The number %d in the col %d has been used!\n",storage1,i+1);
break;
}
}
if(judge1)break;
}
//判断列
int count2=0;
int storage2;
int judge2=0;
for(int i=0;i<9;i++)
{
for(int n=1;n<10;n++)
{
for(int j=0;j<9;j++)
{
if(board[j][i]==n)count2++;
if(count2>1)
{
judge2=1;
storage2=n;
break;
}
}
count2=0;
//判断
if(judge2)
{
printf("False:Invalid initial Sudoku matrix\n");
printf("The number %d in the list %d has been used!\n",storage2,i+1);
break;
}
}
if(judge2)break;
}
//判断区块
int count3=0;
int storage3;
int judge3=0;
for(int col=0;col<3;col++)
{
for(int list=0;list<3;list++)
{
for(int n=1;n<10;n++)
{
for(int i=3*col;i<3*col+3;i++)
{
for(int j=3*list;j<3*list+3;j++)
{
if(board[i][j]==n)count3++;
if(count3>1)
{
judge3=1;
storage3=n;
break;
}
}
if(judge3)break;
}
count3=0;
}
//判断
if(judge3)
{
printf("False:Invalid initial Sudoku matrix\n");
if(col==0)
printf("The number %d in the block %d has been used!\n",storage3,1+list);
else if(col==1)
printf("The number %d in the block %d has been used!\n",storage3,4+list);
else
printf("The number %d in the block %d has been used!\n",storage3,7+list);
break;
}
if(judge3)break;
}
if(judge3)break;
}
if(!judge1&&!judge2&&!judge3)printf("True:Valid initial Sudoku matrix!\n");
return 0;
}
Loading…
Cancel
Save