题目链接

【题解】

回溯法搞一下。
用set和数组下标判重。

【代码】

class Solution {
public: set<int> myset[9];
int hang[9][10],lie[9][10]; bool dfs(vector<vector<char>>& board,int x,int y){
if (y==9){
x++;y = 0;
}
if (x==9){
return true;
}
if (board[x][y]!='.'){
return dfs(board,x,y+1);
}
for (int i = 1;i <= 9;i++){
if (hang[x][i] || lie[y][i]) continue;
int idx = (x/3)*3+y/3;
if (myset[idx].find(i)!=myset[idx].end()) continue; hang[x][i] = lie[y][i] = 1;
myset[idx].insert(i);
char key = i+'0';
board[x][y] = key;
if (dfs(board,x,y+1)) return true;
board[x][y] = '.';
hang[x][i] = lie[y][i] = 0;
myset[idx].erase(i);
}
return false;
} void solveSudoku(vector<vector<char>>& board) {
for (int i = 0;i < 9;i++) myset[i].clear();
memset(hang,0,sizeof hang);memset(lie,0,sizeof lie); for (int i = 0 ;i < 9;i++)
for (int j = 0;j < 9;j++)
if (board[i][j]!='.'){
int x = board[i][j]-'0';
hang[i][x]=1;
lie[j][x] = 1;
int ti = i/3,tj = j/3;
int idx = ti*3+tj;
myset[idx].insert(x);
} dfs(board,0,0);
}
};

最新文章

  1. 第一章 Web应用程序简介
  2. linux 查找文件的命令
  3. vue服务端渲染
  4. POJ 1012 Joseph
  5. easyui datagrid 编辑器绑定事件
  6. Android Studio 的安装和配置篇(Windows篇)
  7. gcc中动态库和静态库的链接顺序
  8. C#,PHP对应加密函数
  9. MySql基础入门-mysql的结构层次
  10. JDBC详解系列(四)之建立Stament和执行SQL语句
  11. 【一天一道LeetCode】#13. Roman to Integer
  12. L364 Should Your Resume Be One Page or Two?
  13. WRF 安装备忘
  14. EF + mysql 异常:Unable to load the specified metadata resource
  15. C#概念总结(三)
  16. gitlab-ci的注意点
  17. 【原】Spring整合Redis(第二篇)—SDR环境搭建具体步骤
  18. 第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码
  19. python管道pipe,两个进程,使用管道的两端分别执行写文件动作,带锁(lock)
  20. Sitemesh 3使用及配置

热门文章

  1. 跨域AJAX
  2. c# 如何获取系统管理员权限(UAC) 及判断当前是否是管理员权限
  3. 【HDOJ6606】Distribution of books(二分,BIT)
  4. VC++ 控件赋值取值
  5. BZOJ 2741: 【FOTILE模拟赛】L(可持久化Trie+分块)
  6. JS获取浏览器地址栏的多参数值的任意值
  7. VMware中对Linux虚拟机的网络配置静态IP的配置
  8. mysql使用crontab定时备份
  9. Python 进阶_OOP 面向对象编程_静态方法和类方法
  10. 初识redis基础