leetcde37. Sudoku Solver
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的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- 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 ...
随机推荐
- GITHUB使用简介
前提:本地开通SSH服务 我是Ubuntu OS,其他OS自行查找,不难目的:利用Github的免费托管服务,创建自己的repo或者fork别人的repo.步骤:·安装客户端 安装如下两个git ...
- ReactiveCocoa(RAC)
好处:代码高聚合,方便我们管理: 链式编程: CaculatorMaker.h #import <Foundation/Foundation.h> #define ADD #define ...
- touch穿透
出现穿透的前提:上层绑定touch事件,下层绑定click事件. 解决方案:touchend绑定: e.preventDefault();$(下层).css("pointer-events& ...
- CSS3之尖角标签
如图所示,Tag标签的制作通常使用背景图片,现在用CSS3代码就能实现尖角效果(需浏览器支持CSS3属性). 运用CSS3样式实现尖角标签,只需要写简单的HTML结构和CSS样式. <p> ...
- python走起之第三话
一. SET集合 set是一个无序且不重复的元素集 class set(object): """ set() -> new empty set object set ...
- Shiro Security
手动创建shiro Filter的java代码 // Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecu ...
- Flyweight
1 意图:运用共享技术有效地大量支持细粒度的对象 2 动机:flyweight是一个共享对象,可以在多个场景使用. 分为内部状态和外部状态,内部状态存储于flyweight中,包含了独立于flywei ...
- es6箭头函数中this
普通函数: $scope.$on('$stateChangeSuccess',function(){this.list = this.getList();}); 箭头函数: $scope.$on('$ ...
- OPENGL学习之路(0)--安装
此次实验目的: 安装并且配置环境. 1 下载 https://www.opengl.org/ https://www.opengl.org/wiki/Getting_Started#Downloadi ...
- mysql分组查询取分组后各分组中的最新一条记录
SELECT * FROM ( SELECT * FROM `CFG_LOGIN_LOG` ORDER BY LOGTIME DESC ) test GROUP BY login_name DESC