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 ...
随机推荐
- Python3基础 sort(reverse=True) 将一个列表降序排列
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- lambda的使用ret = filter(lambda x : x > 22 ,[11,22,33,44])
#!/usr/bin/env python #def f1(x) : # return x > 22 ret = filter(lambda x : x > 22 ,[11,22,33,4 ...
- myeclipse/eclipse没有Project Facets的解决方法
http://www.cnblogs.com/jerome-rong/archive/2012/12/18/2822783.html 经常在eclipse中导入web项目时,出现转不了项目类型的问题, ...
- 解决maven的报错
昨晚用Mars版本建maven工程,测试springboot,一路很顺畅,没有啥阻碍. 今天换了台机器,结果就不好用了,建完maven工程后,pom文件报错,该生成的代码结构也没有,更别提jar包了. ...
- 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的
在MVC中添加授权认证配置之后报了这样的错 原因是在整个MVC项目中有两个Web.Config文件存在authentication节点,一个Web.Config文件在View目录下,一个在根目录下 解 ...
- 基础知识复习(二)——stdafx.h 头文件及x&(x-1)运算
今天好久没写过C++程序了,使用VS2013 新建空的控制台程序,结果自动生成了头文件和main 方法. 就了解了stdafx.h头文件的含义及用法. stdafx:standard Applicat ...
- 69道Spring面试题和答案
1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标是简化Jav ...
- Unix 初步(一)
1.Unix文件系统 Unix文件系统有三种文件类型:普通文件.目录文件和设备文件(将外部设备作为一种特殊的文件进行管理,实现输入输出统一而单纯的操作.) 2.Unix的网络功能 TCP/IP 3.r ...
- C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)
C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...
- Java设计模式系列1--原型模式(Prototype Method)
2014-02-14 11:27:33 声明:本文不仅是本人自己的成果,有些东西取自网上各位大神的思想,虽不能一一列出,但在此一并感谢! 原型模式,从名字即可看出,该模式的思想就是将一个对象作为原型, ...