如果 Matrix RXC 中存在,C 程序查找 Sudoko 正方形 3x3 的问题
Problem with C program to find Sudoko square 3x3 if exist in Matrix RXC
#include <stdio.h>
#include <stdlib.h>
#define R 4
#define C 5
// functions for option 1 initializeCountArray and TestCount and Sudoku
void initializeCountArray(int *count)
{
int i;
for (i = 0; i < 10; i++)
count[i] = 0;
}
int TestCount(int *count)
{
int i;
for (i = 1; i < 10; i++)
if ( count[i] == 1)
count[0]++;
return 1;
}
int Sudoku(int mat[R][C])
{
int count[10] = { 0 };
int rows,cols,i,j;
for(rows=0; rows<R-2; rows++)
{
for (cols = 0; cols<C-2; cols++)
{
initializeCountArray(count);
for (i = rows; i <= rows+2; i++)
{
for (j = cols; j <= cols+2; j++)
{
count[ mat[i][j] ] ++;
printf("\n%d,%d",i,j);
}
}
printf("\n TestCount=%d",TestCount(count));
if (TestCount(count) == 9)
return 1;
}
}
return 0;
}
void main ()
{
int mat[R][C] = {{1,7,8,9,6},
{1,3,3,4,6},
{1,1,1,2,5},
{1,6,7,8,9}};
printf ("\n Check Matrix if Suduku 3X3 square found");
if (Sudoku(mat) == 1)
printf("\n Sudoku square matrix was found\n");
else
printf("\n Sudoku square matrix NOT found\n");
}
该程序应该解决我们在 class 中获得的特定代码测试,其中包含的功能
当 运行 程序 TestCount 函数给出错误的数字作为输出时,我们不能使用其他方法我使用了索引的测试打印输出,但我无法弄清楚哪里出了问题,请帮助
首先:
在您的 TestCount
函数中,您使用 count[0]
作为 3x3 子矩阵中出现的数字的计数器。我想你想做的是:
return count[0];
而不是你的:
return 1;
其次,你这样做:
printf("\n TestCount=%d", TestCount(count));
if (TestCount(count) == 9)
return 1;
但请注意您的 TestCount
有一些副作用。第一次调用它时,count[0]
对于数独式 3x3 矩阵会得到 9,但第二次继续递增 count[0]
,所以它会得到 18 并且无法通过 == 9
检查 -最终你的 Sudoku
函数错过了它。
您可能应该在 TestCount()
的调用之间设置 count[0] = 0;
,在开始计算 TestCount()
之前将 count[0]
重置为 0 或您能想到的任何其他内容(只要确保你没有覆盖它即可)。
#include <stdio.h>
#include <stdlib.h>
#define R 4
#define C 5
// functions for option 1 initializeCountArray and TestCount and Sudoku
void initializeCountArray(int *count)
{
int i;
for (i = 0; i < 10; i++)
count[i] = 0;
}
int TestCount(int *count)
{
int i;
for (i = 1; i < 10; i++)
if ( count[i] == 1)
count[0]++;
return 1;
}
int Sudoku(int mat[R][C])
{
int count[10] = { 0 };
int rows,cols,i,j;
for(rows=0; rows<R-2; rows++)
{
for (cols = 0; cols<C-2; cols++)
{
initializeCountArray(count);
for (i = rows; i <= rows+2; i++)
{
for (j = cols; j <= cols+2; j++)
{
count[ mat[i][j] ] ++;
printf("\n%d,%d",i,j);
}
}
printf("\n TestCount=%d",TestCount(count));
if (TestCount(count) == 9)
return 1;
}
}
return 0;
}
void main ()
{
int mat[R][C] = {{1,7,8,9,6},
{1,3,3,4,6},
{1,1,1,2,5},
{1,6,7,8,9}};
printf ("\n Check Matrix if Suduku 3X3 square found");
if (Sudoku(mat) == 1)
printf("\n Sudoku square matrix was found\n");
else
printf("\n Sudoku square matrix NOT found\n");
}
该程序应该解决我们在 class 中获得的特定代码测试,其中包含的功能 当 运行 程序 TestCount 函数给出错误的数字作为输出时,我们不能使用其他方法我使用了索引的测试打印输出,但我无法弄清楚哪里出了问题,请帮助
首先:
在您的 TestCount
函数中,您使用 count[0]
作为 3x3 子矩阵中出现的数字的计数器。我想你想做的是:
return count[0];
而不是你的:
return 1;
其次,你这样做:
printf("\n TestCount=%d", TestCount(count));
if (TestCount(count) == 9)
return 1;
但请注意您的 TestCount
有一些副作用。第一次调用它时,count[0]
对于数独式 3x3 矩阵会得到 9,但第二次继续递增 count[0]
,所以它会得到 18 并且无法通过 == 9
检查 -最终你的 Sudoku
函数错过了它。
您可能应该在 TestCount()
的调用之间设置 count[0] = 0;
,在开始计算 TestCount()
之前将 count[0]
重置为 0 或您能想到的任何其他内容(只要确保你没有覆盖它即可)。