weixin_46609088 2021-05-21 11:08 采纳率: 0%
浏览 28

用回溯法解数独问题,结果一直为空

class Solution { public: vector> spaces; bool row[9][9],col[9][9],degree[3][3][9]; int len; void solveSudoku(vector>& board) { memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); memset(degree,0,sizeof(degree)); for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(board[i][j]>='1'&&board[i][j]<='9'){ int a=board[i][j]-'0'; row[i][a-1]=col[j][a-1]=degree[i/3][j/3][a-1]=true; } else{ spaces.push_back(make_pair(i,j)); } } } len=spaces.size(); dfs(0,board); return; } bool check(int a,int b,int k){ if(row[a][k]||col[b][k]||degree[a/3][b/3][k]){ return false; } return true; } bool dfs(int start,vector> board){ if(start==len){ return true; } int x=spaces[start].first,y=spaces[start].second; for(int i=1;i<=9;i++){ if(check(x,y,i-1)){ row[x][i-1]=col[y][i-1]=degree[x/3][y/3][i-1]=true; board[x][y]=i+'0'; if(dfs(start+1,board)) return true; row[x][i-1]=col[y][i-1]=degree[x/3][y/3][i-1]=false; } } return false; } };

  • 写回答

2条回答 默认 最新

  • 关注

    有没有什么错误信息提示呢?把你的代码插入代码段,格式化一下。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog