37. Sudoku Solver (Array;Back-Track)
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 {
public:
void solveSudoku(vector<vector<char>> &board) {
int line=,column=-;
findNextEmpty(board,line,column);
backtracking(board,line,column);
}
bool backtracking(vector<vector<char>> &board,int line, int column)
{
int i=line, j = column;
for(int k = ; k<=; k++)
{
if(!isValid(board,line,column,k+'')) continue;
board[line][column] = k+'';
if(!findNextEmpty(board,line,column)) return true; //if no empty cell is found
if(backtracking(board,line,column)) return true;
//backTracking
line = i;
column = j;
}
//backTracking
board[line][column] = '.';
return false; //if no valid number is found
}
//为回溯法写一个独立的check函数
bool isValid(vector<vector<char>> &board, int line, int column, char value)
{
//check九宫格的一个格
int upperBoard = line/ * ;
int leftBoard = column/ * ;
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(board[upperBoard+i][leftBoard+j] == value) return false;
}
}
//check 列
for(int i = ; i<; i++)
{
if(board[line][i] == value) return false;
}
//check行
for(int i = ; i<; i++)
{
if(board[i][column] == value) return false;
}
return true;
}
bool findNextEmpty(vector<vector<char>> &board, int &line, int &column){
int i,j;
for(j = column+; j < ; j++){
if(board[line][j]!='.') continue;
column=j;
return true;
}
for(i = line+; i < ; i++){
for(j = ; j < ; j++){
if(board[i][j]!='.') continue;
line = i;
column=j;
return true;
}
}
return false;
}
};
37. Sudoku Solver (Array;Back-Track)的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 36. Valid Sudoku + 37. Sudoku Solver
▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 37. Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- jenkins 执行python脚本 断言失败就可以构建失败
可以配合try: 那个语句去搭配
- 小甲鱼-002用python设计第一个游戏
第一个游戏 示例1: #/usr/bin/env python3 # -*-coding:utf-8 -*- print("-----我是自恋狂-----") temp = inp ...
- 1036 Boys vs Girls (25 分)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...
- 阿里云内网和公网NTP服务器和其他互联网基础服务时间同步服务器
阿里云为云服务器ECS提供了内网NTP服务器,对于阿里云以外的设备,阿里云同时提供了 公网NTP服务器,供互联网上的设备使用. 内网和公网NTP服务器 以下为阿里云提供的内网和公网NTP服务器列表. ...
- RedisCluster读写分离改造
RedisCluster模式启动的环境中,通过Redis中的每个连接,都可以访问 cluster nodes 访问到所有的服务器列表以及其所处于的角色(master/slave).对于RedisC ...
- NHibernate 学习笔记(一)
NHibernate 的简介: NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping (ORM))这个术语表示 ...
- uva-10392-因数分解
#include<stdio.h> #include<iostream> #include<queue> #include<memory.h> #inc ...
- Mysql数据库查询数据文件大小
参考网站:https://zhidao.baidu.com/question/201227796936321525.html 用SQL命令查看Mysql数据库大小 要想知道每个数据库的大小的话,步骤如 ...
- 6.package配置相关
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 属性名 是否必须 说明 Name 是 Package的唯一标识,不允许同名 ...
- 子元素scroll父元素容器不跟随滚动JS实现
仅供参考: function parentNotRoll($id){ var flg;//标记滚动方向,true-向下,false-向上 var $test = document.getElement ...