【发布时间】:2020-03-02 20:19:18
【问题描述】:
我试图写一个数独求解器算法,它应该像这样工作: 选一个空格, 选择一个号码并验证是否可以在该位置拥有该号码, 如果不选择其他号码, 递归地尝试找到一个解决方案,如果没有可能的回溯,直到找到一个。 问题是这个打印绝对没有,我不知道该怎么做请帮忙。
#include<bits/stdc++.h>
using namespace std;
bool possible(int y,int x,int n,int grid[9][9]){
for(int i=0;i<9;i++){
if(grid[y][i]==n)
return false;
}
for(int i=0;i<9;i++){
if(grid[i][x]==n)
return false;
}
int y0 = y/3;
int x0 = x/3;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(grid[y0+i][x0+j]==n)
return false;
}
}
return true;
}
void display(int grid[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
cout<<grid[y][x]<<" ";
}
cout<<endl;
}
}
void solve (int grid[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
if(grid[y][x]==0){
for(int n=1;n<10;n++){
if(possible(y,x,n,grid)){
grid[y][x] = n;
solve(grid);
grid[y][x] = 0;
}
}
return;
}
}
}
display(grid);
}
int main(){
int grid[9][9];
ifstream in ("input.txt");
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
in>>grid[y][x];
}
}
solve(grid);
}
【问题讨论】:
-
我不知道该怎么办请帮忙。 -- How to debug small programs
-
输入是什么?
-
问题是这个打印完全没有,我不知道该怎么做使用你的调试器找出原因。具体来说,它是永远循环,还是在输入有效时找不到解决方案。
-
你也可以,除了调试,这是最好的开始,把
std::cout信息放在你的solve(...)另外:Why should I not #include <bits/stdc++.h>Why is “using namespace std;” considered bad practice -
程序退出而不打印任何内容,因为您的回溯不起作用并且它提前退出。从
000000200,080007090,602000500,070060000,000901000,000020040,005000603,090400070,006000000开始,solve() 在957643281,483107090,602000500,070060000,000901000,000020040,005000603,090400070,006000000上达到 x=3,y=1 并尝试每个数字 - 它们都不适合,因此它一直返回并退出程序。
标签: c++ recursion backtracking sudoku recursive-backtracking