如果数字不正确,请使用数独求解器方法检查行/列位置以获得解决方案和回溯。打印出No Solution
后跟Abort Trap Error: 6
。我正在使用一个应该返回TRUE的测试用例。 Row和Col最初在参数中传递为0
可能是什么问题?我认为这可能是我增加行和列的方式。它读入单行文件并转换为2D网格
这是测试输入:4 ..... 8.5.3 .......... 7 ...... 2 ..... 6 ..... 8.4 ...... ... 1 ....... 6.3.7.5..2 ..... 1.4 ......
#include <stdio.h>
#include <stdlib.h>
#define BLANK '.'
#define TRUE 1
#define FALSE 0
#define BLANK_SPACE '.'
int findBlankSpace(int grid2D[9][9]);
int valid(int grid2D[9][9], int rowIndex, int colIndex, int num);
void prettyPrint(int grid2D[9][9]);
int readLine(int grid2D[9][9]);
int findSolution(int grid2D[9][9],int row, int col);
int UsedInRow(int grid2D[9][9], int row, int num);
int UsedInCol(int grid2D[9][9], int col, int num);
int UsedInBox(int grid2D[9][9], int boxStartRow, int boxStartCol, int num);
int findSolution(int grid2D[9][9], int row, int col)
{
row = 0;
if (findBlankSpace(grid2D) == FALSE)
return TRUE;
if (row == 9) {
row = 0;
}
for (int num = 1; num <= 9; num++)
{
if (valid(grid2D, row, col, num))
{
grid2D[row][col] = num;
if (findSolution(grid2D, row, ++col))
{
return TRUE;
}
}
grid2D[row][col] = 0;
row++;
}
return FALSE;
}
int findBlankSpace(int grid2D[9][9])
{
int row;
int col;
for (row = 0; row < 9; row++)
for (col = 0; col < 9; col++)
if (grid2D[row][col] == 0)
return TRUE;
return FALSE;
}
int UsedInRow(int grid2D[9][9], int row, int num)
{
for (int col = 0; col < 9; col++)
if (grid2D[row][col] == num)
return TRUE;
return FALSE;
}
int UsedInCol(int grid2D[9][9], int col, int num)
{
for (int row = 0; row < 9; row++)
if (grid2D[row][col] == num)
return TRUE;
return FALSE;
}
int UsedInBox(int grid2D[9][9], int row1, int col1, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid2D[row+row1][col+col1] == num)
return TRUE;
return FALSE;
}
int valid(int grid2D[9][9], int row, int col, int num)
{
return !UsedInRow(grid2D, row, num) &&
!UsedInCol(grid2D, col, num) &&
!UsedInBox(grid2D, row - row % 3, col - col % 3, num);
}
int readLine(int grid2D[9][9])
{
int c;
int row = 0;
int col = 0;
while((c = getchar()) != EOF)
{
if(c == '.')
{
grid2D[row][col] = 0;
}
else
{
grid2D[row][col] = c - '0';
col++;
}
if(col%9 == 0)
{
row++;
col = 0;
}
}
return 0;
}
void prettyPrint(int grid2D[9][9])
{
int count = 0;
int row;
int col;
printf("solution:\n");
/* Use nested for-loops to iterate through 2-D array */
for(row = 1; row <= 9; row++)
{
for(col = 1; col<=9; col++)
{
/* After every 3rd character, print out "|", forming the board */
if((col%3==0)&&(col%9!=0))
{
printf("%d| ", grid2D[row - 1][col - 1]);
}
else
printf("%d ", grid2D[row - 1][col - 1]);
count++;
}
/* After every 3rd row, print out horizontal separations */
if((row%3==0)&&(row!=9))
{
/* Advance to the next line prior to printing separation to give space */
printf("\n");
printf("------+-------+------");
}
printf("\n");
}
}
int main()
{
int grid2D[9][9];
readLine(grid2D);
if(findSolution(grid2D, 0, 0)==TRUE)
{
prettyPrint(grid2D);
}
else
{
printf("No Solution Found!\n");
}
return 0;
}
答案 0 :(得分:0)
在递归函数中:findSolution(),最终是&#39; col&#39;参数将是9(或更高)!当发生这种情况时,代码将访问数组中当前行的边界之外。这会导致未定义的行为,并可能导致seg故障事件。