1.题目描述

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.

 

2.解法分析

class Solution
{
public:
bool isValid(vector<vector<char> > &board, int x, int y)
{
int i, j;
for (i = 0; i < 9; i++)
if (i != x && board[i][y] == board[x][y])
return false;
for (j = 0; j < 9; j++)
if (j != y && board[x][j] == board[x][y])
return false;
for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
if (i != x && j != y && board[i][j] == board[x][y])
return false;
return true;
} bool solveSudoku(vector<vector<char>> &board)
{
for(int y=0;y<9;++y)
{
for(int x=0;x<9;++x)
{
if(board[y][x]=='.')
{
for(int i=1;i<=9;++i)
{
board[y][x]='0'+i;
if(isValid(board,y,x))
{
// if(x==8)if(mySolveSudoku(board,sh+1))return true;
if(solveSudoku(board))return true;
} board[y][x]='.';
} return false;
}
}
} return true;
}
};

leetcode—sudoku solver的更多相关文章

  1. [LeetCode] Sudoku Solver 求解数独

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

  2. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  3. LEETCODE —— Sudoku Solver

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

  4. [LeetCode] Sudoku Solver(迭代)

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

  5. leetcode Sudoku Solver python

    #the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...

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

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

  7. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

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

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

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

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

随机推荐

  1. SQL创建链接服务器

    二.--创建链接服务器 exec sp_addlinkedserver  'srv_lnk','','SQLOLEDB','远程服务器名或ip地址' exec sp_addlinkedsrvlogin ...

  2. 寻找最小的k个数

    1. 能想到的最直接的办法,就是对数组进行排序,最好的排序算法的时间复杂性为O(n*logn),这一个方法请参照各种排序算法. 2. 另外申请一个k空间数组,依次更改里面的最大值,每做一次最多要扫描一 ...

  3. hdu - 2216 Game III && xtu 1187 Double Maze (两个点的普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2216 zjt和sara在同一个地图里,zjt要去寻找sara,zjt每移动一步sara就要往相反方向移动,如果他 ...

  4. Java中final、finally、finalize的区别

    简单区别: final用于声明属性,方法和类,分别表示属性不可交变,方法不可覆盖,类不可继承. finally是异常处理语句结构的一部分,表示总是执行. finalize是Object类的一个方法,在 ...

  5. ArcGis Javascript API (V3.6)加载天地图

    Arcgis的Javascript api开发很活跃,不知不觉都发布了3.6的版本了.该版本基于dojo 1.8.3开发的. 从dojo 1.8开始,AMD机制用得越来越多了,而且require([& ...

  6. && 用法解释

    A&&B 首先判断A,A成功然后判断B:A不成功则结束判断.

  7. DataGridView导出Excel

    将DataGridView里面的数据,导出到表格里面去. 首先,需要添加三个引用 直接在解决方案里,右键添加引用,找到路径即可.然后再把这三个文件复制到项目的根目录下. 然后定义导出表格的函数: pu ...

  8. get 与post 的接收传值方式

    1.get方法接收值写法 string ad = Request["name"]; 2.post方法接收值写法 <%string a = Request.Form[" ...

  9. Java Socket 模拟HTTP请求

    public static void main(String[] args) { try { String url = "192.168.1.103"; Socket socket ...

  10. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...