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. Windows API 进程状态信息函数

    这里的进程状态信息函数主要分为两类,一类是PS(PROCESS STATUS HELPER) API,另外一类是Th(TOOL HELP) API. 话说第一次遇到这个ToolHelp函数时我在看&l ...

  2. Java 字符串拼接方式

    import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; impor ...

  3. javascript callback函数的理解与使用

    最近做的一个项目中用到了callback函数,于是就研究了下总结下我对javascript callback的理解 首先从callback的字面翻译“回调” 可以理解这是一个函数被调用的机制 当我们遇 ...

  4. debian系统安装Thinkpad T410s的无线网卡驱动:centrino Advanced-N 6200 2x2 AGN

    前几天搞到手一台小黑:T410s.自带系统是win7.由于想学习debian,所以就搞成了双系统,安装了一套debian 6.0. 可是让我困惑的是在debian下,无法使用T410s的网卡,因为默认 ...

  5. Perl文件读写

    Perl File Handling: open, read, write and close files #==================== Opening files Solution 1 ...

  6. leetcode:Power of Two

    Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...

  7. 《OD学hadoop》第二周0703

    hdfs可视化界面: http://beifeng-hadoop-01:50070/dfshealth.html#tab-overview yarn可视化界面: http://beifeng-hado ...

  8. grunt <% %>模板和使用配置文件

        使用<% %>分隔符指定的模板会在任务从它们的配置中读取相应的数据时将自动扩展扫描.模板会被递归的展开,直到配置中不再存在遗留的模板相关的信息(与模板匹配的).    整个配置对象 ...

  9. 1343. Fairy Tale

    1343 想了好一会 以为会有什么定理呢 没想到 就试着搜了 看来素数还是很多的 跑的飞快 注意会有前导0的情况 还有0,1不是素数... #include <iostream> #inc ...

  10. 【笨嘴拙舌WINDOWS】GDI(2)

    古时候,大师舞文之时需要笔墨纸伺候,不同笔,不同墨,作品风格迥异! 以下是windows提供的笔(带有墨): CreatePen(PS_SOLID,0,clRed);                  ...