代码工作,数独解决,但如果你写了几个相同的数字,程序挂起,不会产生错误,它应该。请帮忙。
class Game
{
private int[,] puzzle = new int[9, 9];
public bool isAvailable(int row, int col, int num)
{
int rowStart = (row / 3) * 3;
int colStart = (col / 3) * 3;
for (int i = 0; i < 9; ++i)
{
if (puzzle[row, i] == num) return false;
if (puzzle[i, col] == num) return false;
if (puzzle[rowStart + (i % 3), colStart + (i / 3)] == num) return false;
}
return true;
}
public bool fillSudoku(int row, int col)
{
if (row < 9 && col < 9)
{
if (puzzle[row, col] != 0)
{
if ((col + 1) < 9) return fillSudoku(row, col + 1);
else if ((row + 1) < 9) return fillSudoku(row + 1, 0);
else return true;
}
else
{
for (int i = 0; i < 9; ++i)
{
if (isAvailable(row, col, i + 1))
{
puzzle[row,col] = i + 1;
if (fillSudoku(row, col)) return true;
else puzzle[row,col] = 0;
}
}
}
return false;
}
else return true;
}
public void checkSolutions()
{
if (!fillSudoku(0, 0))
{
Console.Write("\n\nNow Solutions\n\n");
}
}
}
使用此布局,开始循环并且没有错误
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 5 5 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
fullcode:https://dotnetfiddle.net/YoyWdN
答案 0 :(得分:0)
因此,您进入紧密循环的原因是由于以下几行代码。
if (fillSudoku(row, col)) return true;
else puzzle[row,col] = 0;
这里假设您获得的输入始终有效。
在fillSudoku的开头调用isValid函数可能是个好主意,这样可以更快地消除重复...
- 以下添加。 将isValid称为你的fillsudoku的第一行 请根据需要进行优化!
public int isThisValid(int[] oneGroup) {
// return 0 for invalid
// return 1 for valid but incomplete
// return 2 for valid and complete.
boolean isFilled[] = new boolean[9];
for(int i = 0; i < 9; i++) {
int num = oneGroup[i];
if(isFilled[num] == true) {
// duplicate - bailout.
return 0;
}
// else fill the spot
isFilled[num] = true;
}
for(int i = 0; i < 9; i++) {
if(isFilled[i] == false) {
return 1;
}
}
return 2;
}
// check for validity
// empty is allowed.
public boolean isValid()
{
for( int row = 0; row < 9; row++) {
if(!isRowValid(row)) return false;
}
for( int col = 0; col < 0; col++) {
if(!isColValid(col)) return false;
}
for( int boxNo = 0; boxNo < 9; boxNo++) {
if(!isBoxValid(boxNo)) return false;
}
return true;
}
public boolean isRowValid(int rowNum) {
int[] row = new int[9];
for( int i = 0 ; i < 9; i++ ) {
row[i] = puzzle[rowNum][i];
}
int isValid = isThisValid(row);
return (isValid == 0);
}
public boolean isColValid(int colNum) {
int[] cols = new int[9];
for( int i = 0 ; i < 9; i++ ) {
cols[i] = puzzle[i][colNum];
}
int isValid = isThisValid(cols);
return (isValid == 0);
}
public boolean isBoxValid(int boxNum) {
// please do needful here :)
return true;
}
答案 1 :(得分:0)
在添加之前添加了检查,并且没有错误。
public bool isAvailable(int row, int col, int num)
{
int rowStart = (row / 3) * 3;
int colStart = (col / 3) * 3;
for (int i = 0; i < 9; ++i)
{
if (puzzle[row, i] == num) return false;
if (puzzle[i, col] == num) return false;
if (puzzle[rowStart + (i % 3), colStart + (i / 3)] == num) return false;
}
return true;
}
致电:
if (isAvailable(x - 1, y - 1, num))
{
puzzle[x - 1, y - 1] = num;
}
else
{
Console.WriteLine("ERROR");
}