【题目】

每一行、每一列、每个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 附解释的更多相关文章

  1. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  2. leetcode第36题--Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  3. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  4. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  5. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  6. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  7. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  8. Sudoku 数独游戏

    #include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...

  9. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

随机推荐

  1. 目标指定法——S.M.A.R.T.

    一个有效的目标一定要是 具体的(Specific), 可测量的(Measureable), 可实现的(Attainable), 有现实意义的(Realistic), 以及有明确期限的(Time-bas ...

  2. Unity日常记录-本地保存未来时间实现倒计时

    本地保存未来时间实现倒计时 TimeTool工具类:获取当前时间.未来时间.两时间差 using System; using UnityEngine; public class TimeTool { ...

  3. 用shell统计表格数据

    今天有个人问了这样一个问题,图片是原题,在这个题的基础上写了一个实现方法 首先日志存到a.txt文本里,如下 Zhangsan|lisi1|0|Zhangsan|lisi2|10|Zhangsan|l ...

  4. Struts 2 框架搭建HelloWorld

    1.导包 导入相应的jar包,在blank项目中会出现 2.书写Action类 package com.littlepage.struts; public class HelloAction { pu ...

  5. GT sport赛道详解 - 富士国际赛车场

    练了3-4个小时,最好的成绩只有2'09多,这5秒真的很难跨越,很是绝望,感觉碰到瓶颈了. 看了几个视频,发现大家的走线有些差异,但是切apx的极速都是一样的,所以在复合弯道,走线其实不止一种. 分析 ...

  6. WPF界面假死

    首先要检查那些滥用 Timer.Dispacher Timer 或者滥用什么“线程+死循环+阻塞”轮询的代码. 这种是编程大忌,有些人不会设计事件驱动程序,而是滥用轮询. 若是:触发事件后的假死,搜W ...

  7. Python自学:第三章 使用方法pop()删除元素

    motorcycle = ["honda", "yamaha", "suzuki"] last_owned = motorcycle.pop ...

  8. 关于index.html被缓存问题

    关于web的缓存策略,推荐这篇文章:点击 在开发时候经常遇到一个问题,我们根据版本号去控制缓存问题,当我们发布新版本,使用心得版本号的时候,发现html里面引用的版本号却是旧的版本号 ,原来是该htm ...

  9. 使用Visual Studio Installer 2015打包WPF程序

    前言 做过WPF项目,就少不了要将程序打包部署到客户现场,因为一般长时间不会更新打包程序,每次变动较大需要重新配置打包程序时,就会有些生疏,不那么得心应手.为了方便记忆,记录到博客中. 准备 因为做过 ...

  10. MySQL Packets larger than max_allowed_packet are not allowed

    MySQL的一个系统参数:max_allowed_packet,其默认值为1048576(1M), 查询:show VARIABLES like '%max_allowed_packet%'; 修改此 ...