[英]C sudoku backtracking solver precheck function
这是我在StackOverflow上的第一篇文章,因此,如果我做错了什么,我深表歉意。 我是C语言的新手,所以我确定我的代码在某些地方很丑陋,但是大多数代码都能达到我的期望。 我无法使用precheck
方法来检查数独板,然后才开始通过求解器逻辑对其进行检查。 我正在使用文本字符串从文本文件重定向输入
4 ..... 8.5.34 ......... 7 ...... 2 ..... 6 ..... 8.4 ...... 1 ..... ..6.3.7.5..2 ..... 1.4 ...... 4 ..... 8.5.3 .......... 7 ...... 2 ... ..6 ..... 8.4 ...... 1 ....... 6.3.7.5..2 ..... 1.5 ...... 4 ..... 8.5.3 .......... 7 ...... 2 ..... 6 ..... 8.4 ...... 1 ... x ... 6.3.7.5..2 ..... 1.4 ...... 417369825632158947958724316825437169791586432346912758289643571573291684164875293 417369825632158947958724316825437169791586432346912758289643.71572694
每个字符串(理想情况下)是81个字符,仅包含数字1-9和'。'。 我将字符串解析为临时char数组,然后使用方法fillBoard
将临时数组中的char转换为2d int数组。 完成此操作后,我将调用我的precheck
方法。 如果填充的板未通过行,列和框检查,则precheck
方法将返回一个,表示难题不可解决(意味着应打印一条错误消息,并且程序应移至下一个字符串)。 由于某种原因,我的precheck
方法即使对于应该可解决的字符串也返回一个。 我不确定为什么会这样。 任何帮助,将不胜感激。 谢谢。
#include <stdio.h>
#include <string.h>
int alphaError = 0;
struct Point findEmpty(int board[9][9]);
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num);
int positionSafe(int board[9][9], int row, int col, int num);
int usedInCol(int board[9][9], int col, int num);
int usedInRow(int board[9][9], int row, int num);
int solvePuzzle(int board[9][9]);
int precheck(int board[9][9]);
int main()
{
char c;
int charCount = 0;
int i = 0;
char tempStr[100000];
int board[9][9];
while((fscanf(stdin, "%c", &c)) != EOF)
{
printf("%c", c);
if(c != '\n')
{
if(isalpha(c))
{
alphaError = 1;
}
tempStr[i] = c;
i++;
charCount++;
}
else
{
if(charCount != 81 || alphaError == 1)
{
printf("Error\n\n");
i = 0;
charCount = 0;
alphaError = 0;
}
else
{
fillBoard(board, tempStr);
printBoard(board);
if(precheck(board) == 1)
{
printf("Error\n\n");
}
else
{
if(solvePuzzle(board) == 1)
{
printBoard(board);
}
else
{
printf("No solution\n\n");
}
}
i = 0;
charCount = 0;
}
}
}
return 0;
}
struct Point
{
int x;
int y;
} point;
struct Point findEmpty(int board[9][9])
{
struct Point point1;
point1.x = -1;
point1.y = -1;
int row, col;
for(row = 0; row < 9; row++)
{
for(col = 0; col < 9; col++)
{
if(board[row][col] == 0)
{
point1.x = col;
point1.y = row;
}
}
}
return point1;
}
int usedInBox(int board[9][9], int boxStartRow, int boxStartCol, int num)
{
int row, col;
for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
if(board[row + boxStartRow][col + boxStartCol] == num)
{
return 1;
}
}
}
return 0;
}
int positionSafe(int board[9][9], int row, int col, int num)
{
if((usedInRow(board, row, num)) == 0 && (usedInCol(board, col, num)) == 0 && (usedInBox(board, (row-row%3), (col-col%3), num)) == 0)
{
return 1;
}
else
{
return 0;
}
}
int usedInCol(int board[9][9], int col, int num)
{
int row;
for(row = 0; row < 9; row++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int usedInRow(int board[9][9], int row, int num)
{
int col;
for(col = 0; col < 9; col++)
{
if(board[row][col] == num)
{
return 1;
}
}
return 0;
}
int solvePuzzle(int board[9][9])
{
int num;
struct Point point2;
point2 = findEmpty(board);
if(point2.x == -1)
{
return 1;
}
for(num = 1; num <= 9; num++)
{
if(positionSafe(board, point2.y, point2.x, num) == 1)
{
board[point2.y][point2.x] = num;
if(solvePuzzle(board) == 1)
{
return 1;
}
board[point2.y][point2.x] = 0;
}
}
return 0;
}
void printBoard(int board[9][9])
{
int i, j;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
printf("%d", board[i][j]);
}
}
printf("\n\n");
}
void fillBoard(int board[9][9], char tempStr[100000])
{
int i, j;
int k = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(tempStr[k] == '.')
{
board[i][j] = 0;
}
else
{
board[i][j] = (tempStr[k] - '0');
}
k++;
}
}
}
int precheck(int board[9][9])
{
int i, j, num;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(board[i][j] != 0)
{
num = board[i][j];
if(positionSafe(board, i, j, num) == 0)
{
return 1;
}
}
}
}
return 0;
}
因此,您在已经装满的板上使用precheck
吗? 这可能是问题usedInCol
,因为usedInCol
, usedInRow
和usedInBlock
如果值已经存在,则将返回1。 因此,仅在填充电路板时才应使用precheck
,而不是在之后。 如果检查从已填充的板上获取的值,它将始终返回1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.