37. Sudoku Solver (Array;Back-Track)
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.
class Solution {
public:
void solveSudoku(vector<vector<char>> &board) {
int line=,column=-;
findNextEmpty(board,line,column);
backtracking(board,line,column);
} bool backtracking(vector<vector<char>> &board,int line, int column)
{
int i=line, j = column;
for(int k = ; k<=; k++)
{
if(!isValid(board,line,column,k+'')) continue;
board[line][column] = k+''; if(!findNextEmpty(board,line,column)) return true; //if no empty cell is found
if(backtracking(board,line,column)) return true; //backTracking
line = i;
column = j;
} //backTracking
board[line][column] = '.';
return false; //if no valid number is found
} //为回溯法写一个独立的check函数
bool isValid(vector<vector<char>> &board, int line, int column, char value)
{
//check九宫格的一个格
int upperBoard = line/ * ;
int leftBoard = column/ * ;
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(board[upperBoard+i][leftBoard+j] == value) return false;
}
} //check 列
for(int i = ; i<; i++)
{
if(board[line][i] == value) return false;
} //check行
for(int i = ; i<; i++)
{
if(board[i][column] == value) return false;
}
return true;
} bool findNextEmpty(vector<vector<char>> &board, int &line, int &column){
int i,j;
for(j = column+; j < ; j++){
if(board[line][j]!='.') continue; column=j;
return true;
} for(i = line+; i < ; i++){
for(j = ; j < ; j++){
if(board[i][j]!='.') continue; line = i;
column=j;
return true;
}
} return false;
} };
37. Sudoku Solver (Array;Back-Track)的更多相关文章
- 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 ...
- 36. Valid Sudoku + 37. Sudoku Solver
▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 37. Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- [转]Java 运算符的优先级
Java 运算符的优先级(从高到低) 优先级 描述 运算符 1 括号 ().[] 2 正负号 +.- 3 自增自减,非 ++.--.! 4 乘除,取余 *./.% 5 加减 +.- 6 移位运算 &l ...
- 1019 General Palindromic Number (20 分)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...
- Vcenter5.5+vmwarePowercli6.5+powershell5批量创建虚拟机
另存为xxx.ps1 ##########################通过模版批量部署虚拟机以下内容需要人工定义变量####################### #Vcenter的IP $vce ...
- [UE4]射中机器人
- no_unnest,push_subq,push_pred的用法 (转)
常常有人把这三个hint搞混,主要是因为对三种重写原理不清楚.特总结如下.(实验环境为10204)1. no_unnest, unnestunnest我们称为对子查询展开,顾名思义,就是别让子查询孤单 ...
- 探究CSS中border-top属性的使用
上一节我们介绍了CSS top属性的用法,那么这节关于CSS border-top属性用法学习起来就会轻松一些,border-top 简写属性把上边框的所有属性设置到一个声明中. 本文向大家描述一下C ...
- xss总结漏洞篇
Xss漏洞 Xss漏洞出现在1996年.Xss漏洞又名跨站脚本漏洞 Xss可能造成的危害 网站弹框(刷流量) 网站挂马 会话劫持 Cookie被盗取 用户提权 账号被盗 尽量DDOS 蠕虫攻击 Xss ...
- hbase 简介
hbase是hadoop的数据库,能够对大数据提供随机实时的读写访问功能,具有开源,分布式,可扩展行,面向列存储的特点.hbase的目标是存储并处理大量的数据. 它可以直接使用本地文件系统,也可以使用 ...
- leetcode237
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...
- redis该怎么用
最近一些人在介绍方案时,经常会出现redis这个词,于是很多小伙伴百度完redis也就觉得它是一个缓存,然后项目里面把数据丢进去完事,甚至有例如将实体属性拆分塞进redis hash里面的奇怪用法等等 ...