题目链接

【题解】

回溯法搞一下。
用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);
}
};

【LeetCode 37】解数独的更多相关文章

  1. Java实现 LeetCode 37 解数独

    37. 解数独 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗实 ...

  2. [leetcode] 37. 解数独(Java)(dfs,递归,回溯)

    37. 解数独 1A 这个题其实15分钟左右就敲出来并且对了...但是由于我输错了一个数..导致我白白debug一个多小时.. 没啥难度,练递归-dfs的好题 class Solution { pri ...

  3. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

  4. Leetcode——37.解数独 [##]

    @author: ZZQ @software: PyCharm @file: leetcode37_solveSudoku.py @time: 2018/11/20 16:41 思路:递归回溯 首先, ...

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

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

  6. leetcode刷题-37解数独

    题目 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1-9 在每一列只能出现一次.数字 1-9 在每一个以粗实线分隔的 3x ...

  7. 【LeetCode】解数独

    做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...

  8. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  9. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  10. LeetCode37 使用回溯算法实现解数独,详解剪枝优化

    本文始发于个人公众号:TechFlow,原创不易,求个关注 数独是一个老少咸宜的益智游戏,一直有很多拥趸.但是有没有想过,数独游戏是怎么创造出来的呢?当然我们可以每一关都人工设置,但是显然这工作量非常 ...

随机推荐

  1. commons-fileupload-1.2.1.jar 插件上传与下载

    1:首先在页面上写个文本域: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  2. 【LeetCode 75】颜色分类

    题目链接 [题解] 维护一个左边界l和一个右边界r 其中0..l-1都是'0' 而 r+1..n-1都是'2' 我们令i=l;i<=r; 枚举每一个a[i]; ①如果a[i]=2.那么把a[i] ...

  3. python3安装pdfminer并使用

    1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...

  4. 新建工程spring boot

    新建工程spring boot 使用Maven管理, 在官网(http://atart.spring.io)下载demo后,加入依赖 <dependency>         <gr ...

  5. vs 2019 create new project 创建新项目

    下面的place solution and project in the same directory 不需要勾选

  6. C++ allocator类学习理解

    前言 在学习STL中containers会发现C++ STL里定义了很多的容器(containers),每一个容器的第二个模板参数都是allocator类型,而且默认参数都是allocator.但是a ...

  7. 用 Flask 来写个轻博客 (28) — 使用 Flask-Assets 压缩 CSS/JS 提升网页加载速度

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 Flask-Assets 将 Flask-Assets 应用 ...

  8. vue项目在IE下显示空白打不开问题

    近期遇到了项目是vue做的,在IE浏览器下打不开,显示空白问题,解决方案如下: 打不开的原因是因为少了babel-polyfill处理器,所以第一步需要下载: npm install babel-po ...

  9. Yahoo34条军规——雅虎WEB前端网站优化

    雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...

  10. 【读书笔记】:MIT线性代数(5):Four fundamental subspaces

    At the beginning, the difference between rank and dimension: rank is a property for matrix, while di ...