【LeetCode】37. Sudoku Solver
Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.
![]()
A sudoku puzzle...
![]()
...and its solution numbers marked in red.
这题跟N-Queens是一个套路,回溯法尝试所有解。
需要注意的区别是:
本题找到解的处理是return true,因此返回值为bool
N-Queen找到解的处理是保存解,因此返回值为void
对于每个空位'.',遍历1~9,check合理之后往下一个位置递归。
由于这里路径尝试本质上是有序的,即1~9逐个尝试,因此无需额外设置状态位记录已经尝试过的方向。
注意:只有正确达到最终81位置(即成功填充)的填充结果才可以返回,若不然,将会得到错误的填充。
因此辅助函数solve需要设为bool而不是void
class Solution {
public:
void solveSudoku(vector<vector<char> > &board) {
solve(board, );
}
bool solve(vector<vector<char> > &board, int position)
{
if(position == )
return true;
int row = position / ;
int col = position % ;
if(board[row][col] == '.')
{
for(int i = ; i <= ; i ++)
{//try each digit
board[row][col] = i + '';
if(check(board, position))
if(solve(board, position + ))
//only return valid filling
return true;
board[row][col] = '.';
}
}
else
{
if(solve(board, position + ))
//only return valid filling
return true;
}
return false;
}
bool check(vector<vector<char> > &board, int position)
{
int row = position / ;
int col = position % ;
int gid;
if(row >= && row <= )
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
}
else if(row >= && row <= )
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
}
else
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
}
//check row, col, subgrid
for(int i = ; i < ; i ++)
{
//check row
if(i != col && board[row][i] == board[row][col])
return false;
//check col
if(i != row && board[i][col] == board[row][col])
return false;
//check subgrid
int r = gid/*+i/;
int c = gid%*+i%;
if((r != row || c != col) && board[r][c] == board[row][col])
return false;
}
return true;
}
};
check的另一种实现方式如下:
bool check(vector<vector<char> > &board, int pos)
{
int v = pos/;
int h = pos%;
char target = board[v][h];
//row
for(vector<char>::size_type st = ; st < ; st ++)
{
if(st != h)
{
if(target == board[v][st])
return false;
}
} //col
for(vector<char>::size_type st = ; st < ; st ++)
{
if(st != v)
{
if(target == board[st][h])
return false;
}
} //subgrid
int beginx = v/*;
int beginy = h/*;
for(int i = beginx; i < beginx+; i ++)
{
for(int j = beginy; j < beginy+; j ++)
{
if(i != v && j != h)
{
if(target == board[i][j])
return false;
}
}
} return true;
}

【LeetCode】37. Sudoku Solver的更多相关文章
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- 【LeetCode】 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
随机推荐
- 机器学习算法之旅A Tour of Machine Learning Algorithms
In this post we take a tour of the most popular machine learning algorithms. It is useful to tour th ...
- 莫比乌斯函数&莫比乌斯反演
莫比乌斯函数:http://wenku.baidu.com/view/fbec9c63ba1aa8114431d9ac.html Orz PoPoQQQ
- 使用navicat工具创建MySQL存储过程
使用Navicat for MySQL工具创建存储过程步骤: 1. 新建函数(选择函数标签 -> 点击新建函数): 2.输入函数的参数个数.参数名.参数类型等: 3.编写存储过程: 代码如下: ...
- 解决:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面
错误重现: 在发布网站的过程中,虽然不是第一次发布了,但是还是遇到了很多的问题.为了以后可以轻松解决此类问题还是积累下来比较好. 问题:HTTP 错误 404.2 - Not Found. 由于 We ...
- 第二十章 springboot + consul(1)
consul的具体安装与操作查看博客的consul系列. 一.启动consul (1个server+1个client,方便起见,client使用本机):查看:http://www.cnblogs.co ...
- iOS开发-UI基础Demo
现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...
- 根据百度API获得经纬度,然后根据经纬度在获得城市信息
package com.pb.baiduapi; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...
- GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
- supervisord不重启更新配置文件
二.更新新的配置到supervisord supervisorctl update 1 三.重新启动配置中的所有程序 supervisorctl reload
- tcp协议的端口状态
Listening Syn_sent syn_rcvd established close_wait time_wait --------- 对方主动关闭(对方调用close()), 我方的状态变为 ...