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 {
private:
bool isValidSudoku(vector<vector<char>> & board, int row, int col,
char val) {
int N = board.size(); for (int i = ; i < N; i++) {
if (board[row][i] == val)
return false;
} for (int i = ; i < N; i++) {
if (board[i][col] == val)
return false;
} int r = row / * ;
int c = col / * ;
for (int i = r; i < r + ; i++) {
for (int j = c; j < c + ; j++) {
if (board[i][j] == val)
return false;
}
}
return true;
}
bool solveSudoku(vector<vector<char>>& board, int row, int col) { int N = board.size();
if (row == N) return true;
if (col == N) return solveSudoku(board, row + , );
if (board[row][col] != '.')
return solveSudoku(board, row, col + ); for (char k = ''; k <= ''; k++) {
if (!isValidSudoku(board, row, col, k))
continue;
board[row][col] = k;
if(solveSudoku(board, row, col + ))
return true;
board[row][col] = '.';
}
return false;
} public:
void solveSudoku(vector<vector<char>>& board) {
solveSudoku(board, , );
}
};

leetcde37. Sudoku Solver的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

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

  2. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  3. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  4. 【leetcode】Sudoku Solver

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

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

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

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

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

  7. 【LeetCode】37. Sudoku Solver

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

  8. Valid Sudoku&&Sudoku Solver

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  9. LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver

    1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...

随机推荐

  1. Python3基础 把一个列表中内容给另外一个列表,形成两个独立的列表

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. nodejs 访问mysql

    安装 $ npm install mysql 简介 这个一个mysql的nodejs版本的驱动,是用JavaScript来编写的.不需要编译 这儿有个例子来示范如何使用: var mysql = re ...

  3. Delphi 使用之dll文件生成与调用

    DLL是Dynamic-Link Libraries(动态链接库)的缩写,库里面是一些可执行的模块以及资源(如位图.图标等).可以认为DLL和EXE基本上是一回事,只是DLL不能直接执行,而必须由应用 ...

  4. SPSS数据分析—分段回归

    在SPSS非线性回归过程中,我们讲到了损失函数按钮可以自定义损失函数,但是还有一个约束按钮没有讲到,该按钮的功能是对自 定义的损失函数的参数设定条件,这些条件通常是由逻辑表达式组成,这就使得损失函数具 ...

  5. 用OPencv配置vs2010

    第一次作业,图像的识别存储和显示 此次选用的是Microsoft Visual Studio 2010软件,在配置和opencv的时候,要选好版本苟泽造成不兼容,因此我选用的为opencv 2.4.1 ...

  6. 登陆界面Login

    最终界面:   XMAL 代码: <Grid > <Grid.RowDefinitions> <RowDefinition /> <RowDefinition ...

  7. 初学者:JSP登陆界面

    学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...

  8. tomcat通过conf-Catalina-localhost目录发布项目详解 摘自:http://www.cnblogs.com/iyangyuan/archive/2013/09/12/3316444.html

    Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需 ...

  9. PHP判断变量是否为整型

    1.使用is_int() 2.使用is_numeric() 两个函数的区别,当变量为'1'时, is_int()的返回值为false, is_numeric()的返回值为true 当变量为1时, 两个 ...

  10. Reflector反编译WinForm程序重建项目资源和本地资源

    工具:vs2012..NET Reflector8.1.0.35 要解决的问题: 通过Reflector反编译生成的代码可以编译通过并显示窗体的本地资源和项目资源图片 一.测试项目 两个图片分别放在项 ...