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 ...
随机推荐
- R语言学习——欧拉计划(11)Largest product in a grid
Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...
- [html] 回到页首
[转]本文来自:最简单最强大的插件框架(Net 2.0+) http://www.cnblogs.com/baihmpgy/p/3305215.html <!doctype html> & ...
- 1011 World Cup Betting (20 分)
1011 World Cup Betting (20 分) With the 2010 FIFA World Cup running, football fans the world over wer ...
- 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题
cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...
- 如何干净的清除Slave同步信息
MySQL> show master status; +------------------+-----------+--------------+------------------+---- ...
- osx 安装redis
brew install redis 想关文章 http://www.tuicool.com/articles/nM73Enr http://www.iteye.com/topic/1124400
- sqlserver操作命令
启动命令:Net Start MSSqlServer 暂停命令:Net Pause MSSqlServer 重新启动暂停的命令:Net Continue MSSqlServer 停止命令:Net st ...
- eclipse菜单栏工具
1. new Class 和 new Package 通过右键->new -> 找到java->class 方式太慢. 在window->perspective -> c ...
- iOS toll-free bridge
https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Art ...
- ios 给图片加文字
- (UIImage*) drawText:(NSString*)text inImage:(UIImage*)image { //prepare image context UIGraphicsBe ...