Abort Trap Error: 6 - where is it coming from? - Sudoku solver
Abort Trap Error: 6 - where is it coming from? - Sudoku solver
有一个数独求解器方法,可以检查 row/col 解决方案的位置,如果数字不正确,则在测试后回溯。它打印出 No Solution
,然后是 Abort Trap Error: 6
。我正在使用一个应该 return TRUE 的测试用例。 Row 和 Col 最初作为 0
传入参数
可能是什么问题?我在想这可能是我增加行和列的方式。它读取单行文件并转换为二维网格
这里是测试输入: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;
}
在递归函数:findSolution() 中,最终 'col' 参数将为 9(或更大)!发生这种情况时,代码将访问数组中当前行的边界之外。这会导致未定义的行为,并可能导致段错误事件。
有一个数独求解器方法,可以检查 row/col 解决方案的位置,如果数字不正确,则在测试后回溯。它打印出 No Solution
,然后是 Abort Trap Error: 6
。我正在使用一个应该 return TRUE 的测试用例。 Row 和 Col 最初作为 0
可能是什么问题?我在想这可能是我增加行和列的方式。它读取单行文件并转换为二维网格
这里是测试输入: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;
}
在递归函数:findSolution() 中,最终 'col' 参数将为 9(或更大)!发生这种情况时,代码将访问数组中当前行的边界之外。这会导致未定义的行为,并可能导致段错误事件。