题意: 经典的递归题,

要求:除了要求 横竖都填满 1~9外, 每个3*3也都要求满足 1~9

36. 数组可以部分填充, 问是否一个有效的 sudoku.

写了个好烧脑的 四重循环来check 3*3 的部分。  重点在于 用数组作为hash 。 然后对于 check 3*3 部分, 其实就是9个小方块,  9个小方块 定点坐标为 [0 0] [0 3] [06] [3 0] [3 3 ] [3 6] [6 0] [ 6 3] [6 6] ,每个顶点递增为横竖 递增都 3 , 因此写了个二重循环来控制顶点坐标:

for(int i=0; i<9; i= i+3){
   for(int j=0; j<9; j=j+3) {

class Solution {public boolean isValidSudoku(char[][] board) {boolean[][] map_row = new boolean[10][10];boolean[][] map_col = new boolean[10][10];       //check row and colfor(int i=0; i<9; i++){for(int j=0; j<9; j++){if(board[i][j] != '.'){int num = board[i][j] - '0';if(map_row[i][num]) return false;map_row[i][num] = true;if(map_col[num][j]) return false;map_col[num][j] = true;}}}//check 3*3for(int i=0; i<9; i= i+3){for(int j=0; j<9; j=j+3) {boolean[] map = new boolean[10];for(int k=0; k<3; k++){for(int t=0; t<3; t++){int num = board[k+i][t+j] - '0';if(board[k+i][t+j] != '.'){if(map[num]) return false;map[num] = true;}}}}}return true;}
}

37 终于回到数独问题经典 backtracking 了。

class Solution {public void solveSudoku(char[][] board) {List<Integer> indexs = new ArrayList<>();   for(int i=0; i<9; i++)for(int j=0; j<9; j++)if(board[i][j] == '.')indexs.add(i*9+j);dfs(board, indexs,0) ;}private boolean dfs(char[][] board, List<Integer> indexs, int depth){if(depth == indexs.size()) {return true;}boolean flag = false;int i, j=0;int index =  indexs.get(depth);i = index/9;j = index%9;for(int n=1; n<=9; n++){if(isValid(board, (char)(n+'0'), i, j )  ){board[i][j] = (char)(n+'0');if (dfs(board,indexs, depth+1)) return true;    //如果满足条件就直接return 了
           } }// 如果循环结束了,说明 for 循环里 没有成功, 所以 此位置尝试 1~9 均失败,所以 back tracking 回去。board[i][j] = '.'; return false;}private boolean isValid(char[][] board, char num, int x,int y){int i=0, j=0; for( i=0; i<9; i++){if( board[x][i] == num) return false;}for( j=0; j<9; j++){if( board[j][y] == num) return false;}for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)if (i!=x && j!= y &&board[i][j] == num)return false;return true;}
}

转载于:https://www.cnblogs.com/keepAC/p/10014527.html

36. Valid Sudoku/37. Sudoku Solver - 数独问题-- backtracking 经典相关推荐

  1. leetcode 36. Valid Sudoku | 37. Sudoku Solver(数独)

    36. Valid Sudoku https://leetcode.com/problems/valid-sudoku/ 题解 class Solution {public boolean isVal ...

  2. 36. Valid Sudoku数独判断

    题目:数独填写正确判断 https://leetcode.com/problems/valid-sudoku/description/ Determine if a Sudoku is valid, ...

  3. 【LeetCode】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. [leetcode]36. Valid Sudoku c语言

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. 【DFS + backtrack】LeetCode 37. Sudoku Solver

    LeetCode 37. Sudoku Solver Solution1: 转载自花花酱:http://zxi.mytechroad.com/blog/searching/leetcode-37-su ...

  6. LeetCode 36. Valid Sudoku

    LeetCode 36. Valid Sudoku Solution1:我的答案,比较笨.但是清晰易懂好上手啊~~~ class Solution { public:bool isValidSudok ...

  7. 【027】Sudoku–随时在线做数独

    数独游戏是最受欢迎的益智游戏之一,游戏规则是用数字填充9x9的宫格,让每一行,每一列和每个3x3小九宫部分都包含1到9之间的所有数字,不得重复.我们可以在Sudoku网站,在线做数独. 地址:参见文末 ...

  8. sudoku me_Java Sudoku解算器程序

    sudoku me Remember Sudoku sections from newspapers? That grid-like puzzle filled with numbers. 还记得报纸 ...

  9. 【LeetCode】第37题——解数独(难度:困难)

    [LeetCode]第37题--解数独(难度:困难) 题目描述 解题思路 代码详解 注意点 题目描述 编写一个程序,通过填充空格来解决数独问题. 数独的解法需 遵循如下规则: 数字 1-9 在每一行只 ...

  10. 问题 B: 数独(九宫格经典问题+dfs)

    问题 B: 数独(九宫格经典问题+dfs) 题目描述 这个游戏只有一个规则: 将格子填满使得每一行,每一列,和每一个小的九宫格,恰好包含1-9这9个数字 正是由于规则简单而又变化多端,数独一时间风靡全 ...

最新文章

  1. 开源中国git关联xcode操作步骤
  2. 使用c语言标准库中的时间函数
  3. DELL服务器装2003系统
  4. uploadify控制 上传图片到百度云存储
  5. RocketMQ集群启动报错:java.lang.RuntimeException: Lock failed,MQ already started
  6. 深入理解javascript原型和闭包(12)——简介【作用域】
  7. Firebug的安装方法
  8. long类型python_Python类型long vs C'long long'
  9. mysql优化要点_你需要掌握的 Mysql 优化的一些要点
  10. iphone最新款手机_从甲骨到iphone,那些掉进中国式陷阱的歪果仁
  11. 还不知道spring的RestTemplate的妙用吗
  12. 【转】阿里巴巴性能测试规划思路
  13. 关于软件逆向工程的一些不错博客
  14. CrossApp推出移动应用开发神器 CrossApp Style
  15. Pytorch模型训练(0) - CPN源码解析
  16. java 判断字符 不等于 或者_java中字符串不等于怎么判断
  17. 推荐一个非常不错得网站——精品视频网
  18. Mel,Bark以及ERB刻度
  19. 绿氢、蓝氢、灰氢,原来氢也可以这么出彩
  20. 「HTML和CSS入门指南」hr 标签详解

热门文章

  1. java作用域public ,private ,protected 及不写时的区别(转)
  2. as3 Box2d Hello box2d
  3. Silverlight+WCF 新手实例 象棋 棋子移动-规则补充(三十七)
  4. 【window操作系统下Github版本的回滚问题】
  5. 【VS2010学习笔记】【类型转换】整形转换为CString类型
  6. Rabin-Karp算法详解和实现(python)
  7. 进程控制(PCB,进程ID,进程状态,fork函数,文件共享)
  8. Spark大数据计算框架知识总结
  9. 在PyCharm切换Python2和Python3
  10. CTO 要我把这份 MySQL 规范贴在工位上!