[Leetcode 37]*数独游戏 Sudoku Solver 附解释
【题目】
每一行、每一列、每个3*3的格子里只能出现一次1~9。
【思路】
参考了思路,附加了解释。
dfs遍历所有非空格子,n是已经填好的个数。
初始化条件。n=81,都填了,返回结束。对于已经填好的b[x][y] != '.'跳过,到下一个。
xy的设计保证先行后列填写。
if (n == 81)
return true;
int x = n / 9;
int y = n % 9;
if (b[x][y] != '.')
return dfs(b, n + 1);
开始填数字,validate(b, x, y) && dfs(b, n + 1)共同确认是否填对。
for (int i = 0; i < 9; i++) {
b[x][y] = (char)('1' + i);//开始试数1~9
if (validate(b, x, y) && dfs(b, n + 1))
//试填后进行validate检验+填下一个n+1数,成功返true。
return true;
b[x][y] = '.';//否则擦除尝试,return false。
}
validate函数,check每一行、每一列、每3*3的格子。
public boolean validate(char[][] b, int x, int y) {
for (int i = 0; i < 9; i++) {
if (i != x && b[i][y] == b[x][y]) return false;
if (i != y && b[x][i] == b[x][y]) return false;
}
int r = x / 3 * 3;//判断在哪个3*3的格子里
int c = y / 3 * 3;
for (int i = r; i < r + 3; i++) {
for (int j = c; j < c + 3; j++) {
if (i == x && j == y) continue;
if (b[i][j] == b[x][y]) return false;
}
}
return true;
}
【代码】
class Solution {
public void solveSudoku(char[][] board) {
dfs(board, 0);
}
public boolean dfs(char[][] b, int n) {
if (n == 81)
return true;
int x = n / 9;
int y = n % 9;
if (b[x][y] != '.')
return dfs(b, n + 1);
for (int i = 0; i < 9; i++) {
b[x][y] = (char)('1' + i);
if (validate(b, x, y) && dfs(b, n + 1)) return true;
b[x][y] = '.';
}
return false;
}
public boolean validate(char[][] b, int x, int y) {
for (int i = 0; i < 9; i++) {
if (i != x && b[i][y] == b[x][y]) return false;
if (i != y && b[x][i] == b[x][y]) return false;
}
int r = x / 3 * 3;//判断在哪个3*3的格子里
int c = y / 3 * 3;
for (int i = r; i < r + 3; i++) {
for (int j = c; j < c + 3; j++) {
if (i == x && j == y) continue;
if (b[i][j] == b[x][y]) return false;
}
}
return true;
}
}
[Leetcode 37]*数独游戏 Sudoku Solver 附解释的更多相关文章
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
- leetcode第36题--Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Sudoku 数独游戏
#include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
随机推荐
- Servlet和Filter的区别
1.Filter是一个接口,而Servlet是一个类继承于Httpservlet 2.生命周期的不同,Filter在Web App被加载时,创建该Filter的实例,并调用init()方法(仅创建一个 ...
- Mac在Finder中显示隐藏文件
1.显示隐藏文件 打开终端,输入下面的命令: defaults write com.apple.finder AppleShowAllFiles -bool true KillAll Finder ...
- The History of spring
Spring的出现 Spring最早出现对早期J2EE规范复杂性的回应 .虽然有些人一直认为Java EE和Spring处于竞争中,但Spring实际上是对Java EE的补充.Spring编程模型 ...
- git checkout --ours 【学习笔记】
用新分支:git checkout --theirs 文件用master分支:git checkout --ours 文件 执行之后git add
- Request method 'PUT'/ 'POST' not supported
起因 在项目中遇到需要进行crud操作的功能,用的是Springboot+MybatisPlus+MySQL+AVue,在通过postman测试接口正确性时遇到此错误. 排查过程 因为项目运行是没问题 ...
- hadoop搭建笔记(一)
环境:mac/linux hadoop版本:3.1.1 安装特性:非HA 准备: 1. jdk8以上 2. ssh 3. 下载hadoop安装包 配置文件,这里都只有简易配置: 1. core-sit ...
- GIT导出差异版本更新的文件列表
之前写了一篇SVN导出差异版本更新的文件列表 这次写git如何导出差异化版本文件列表 查找了一番,发现git diff这个命令 $ git diff 2da595c daea1d6 --name-on ...
- 微观:心流,宏观:ikigai
ikigai: 心流:在心理学中是一种某者在专注进行某行为时所表现的心理状态.如艺术家在创作时所表现的心理状态.某者在此状态时,通常不愿被打扰,即抗拒中断.定义是一种将个人精神力完全投注在某种活动上的 ...
- 出错:(unicode error) 'unicodeescape' codec can't decode bytes in position 8-9: malformed \N character escape
报错原因:python 中 \N 是换行的意思.这里要把 N 前面的 \ 转义一下.用 \\ 代替即可. Nokia_mac = np.loadtxt('data\oui\\NokiaMac201 ...
- jmockit mock 类的static 属性
final Object[] originValue = new Object[1];try{ new Expectations(XXStatic.class){ { originValue[0] = ...