问题描述
所以我正在为数独求解器编写代码,该部分适用于我的9x9,但不适用于我的16x16(isZero,我已经遗漏了,但它返回了网格上零的地址)和(isNum通过检查是否为零数字是行,列和4x4框,也省略了),对于16x16数独为什么不能解决的一些解释将不胜感激,这也有助于解决问题
edit:这个精确的代码适用于我的9x9,不包括将十六进制转换的部分以及添加为16x16的部分,如果我的9x9使用此确切的逻辑,为什么我的16x16不起作用,我不明白。
#include <iostream>
#include <fstream>
#include <cmath>
#include <sstream>
using namespace std;
// 1 Check if the number is zero
bool isZero(char grid[16][16],int &row,int &col)
{
//cout << "1st";
for (row = 0; row < 16; row++)
for (col = 0; col < 16; col++)
if (grid[row][col] == '0')
return true;
return false;
}
// 2. Check if the number is not in row col or grid
int isNum(char grid[16][16],int row,int col,char num)
{
int rowStart = (row/4) * 4;
int colStart = (col/4) * 4;
for(int i=0; i<16; ++i)
{
if (grid[row][i] == num)
return 0;
if (grid[i][col] == num)
return 0;
if (grid[rowStart + (i%4)][colStart + (i/4)] == num)
return 0;
}
return 1;
}
// 3. Deep search Solve sudoku
bool solve(char grid[16][16])
{
int row,col;
stringstream hexVal;
char val;
//stack <int> grid[row][col];
//stack <int> grid[row];
/*for (row = 0; row < 9; row++)
for (col = 0; col < 9; col++)
if (grid[row][col] == 0)
return true;
return false;*/
if (!isZero(grid,row,col))
return true;
int num = 1;
while(num<16)
{
hexVal << hex<< uppercase<<num;
hexVal >> val;
if (isNum(grid,col,val))
{
grid[row][col]=val;
if (solve(grid))
return true;
grid[row][col] = 0;
}
num++;
}
return false;
}
/*the main goes here,I have edited it out its just basically printing the solved grid*/
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)