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)的更多相关文章

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

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

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

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

  3. 【LeetCode】37. Sudoku Solver

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

  4. 36. Valid Sudoku + 37. Sudoku Solver

    ▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...

  5. [LeetCode] 37. Sudoku Solver 求解数独

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

  6. 37. Sudoku Solver

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

  7. Java [leetcode 37]Sudoku Solver

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

  8. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  9. 【LeetCode题意分析&解答】37. Sudoku Solver

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

随机推荐

  1. numpy里的randn

    这个函数的作用就是从标准正态分布中返回一个或多个样本值.什么是标准正态分布 来源:http://www.360doc.com/content/17/0306/13/32342759_634411464 ...

  2. win2003 server的域用户加入本地管理员组

    很久没有搞windows的域管理,今天碰到一个问题,一直找不到解决方法,一直在网络找资料,直到刚刚才找到方法,方法如下: C:\>net localgroup administrators li ...

  3. SimpleXML概述

    要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM树,通过对DOM 树的遍 ...

  4. 使用Golang进行性能分析(Profiling)

    转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...

  5. python + docker, 实现天气数据 从FTP获取以及持久化(五)-- 利用 Docker 容器化 Python 程序

    背景 不知不觉中,我们已经完成了所有的编程工作.接下来,我们需要把 Python 程序 做 容器化 (Docker)部署. 思考 考虑到项目的实际情况,“持久化天气”的功能将会是一个独立的功能模块发布 ...

  6. solr查询空值、null、不存在的字段的方法

    正常情况下我们都是按有值的方式去搜索,但是有时候有一些字段为null,solr中就没有存储进去,我们怎么获取这个字段不存在为条件的搜索结果了,我们只需要在搜索字段前加上负号,如下图 摘要: Solr的 ...

  7. noip 2011 选择客栈

    题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...

  8. JAVA Spring JavaBean 属性值的注入方式( 属性注入, 特殊字符注入 <![CDATA[ 带有特殊字符的值 ]]> , 构造器注入 )

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match

    spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...

  10. Pycharm远程连接服务器(windows下远程修改服务器代码)

    1.写在前面     之前一致用putty,ssh,修改代码,或者本地修改,上传到服务器,各种不爽,现在改用xshell,但是有时候还是不方便感觉,于是自己配置了远程连接pycharm,这样不用总是到 ...