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.

这题跟N-Queens是一个套路,回溯法尝试所有解。

需要注意的区别是:

本题找到解的处理是return true,因此返回值为bool

N-Queen找到解的处理是保存解,因此返回值为void

对于每个空位'.',遍历1~9,check合理之后往下一个位置递归。

由于这里路径尝试本质上是有序的,即1~9逐个尝试,因此无需额外设置状态位记录已经尝试过的方向。

注意:只有正确达到最终81位置(即成功填充)的填充结果才可以返回,若不然,将会得到错误的填充。

因此辅助函数solve需要设为bool而不是void

class Solution {
public:
void solveSudoku(vector<vector<char> > &board) {
solve(board, );
}
bool solve(vector<vector<char> > &board, int position)
{
if(position == )
return true; int row = position / ;
int col = position % ;
if(board[row][col] == '.')
{
for(int i = ; i <= ; i ++)
{//try each digit
board[row][col] = i + '';
if(check(board, position))
if(solve(board, position + ))
//only return valid filling
return true;
board[row][col] = '.';
}
}
else
{
if(solve(board, position + ))
//only return valid filling
return true;
}
return false;
}
bool check(vector<vector<char> > &board, int position)
{
int row = position / ;
int col = position % ;
int gid;
if(row >= && row <= )
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
}
else if(row >= && row <= )
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
}
else
{
if(col >= && col <= )
gid = ;
else if(col >= && col <= )
gid = ;
else
gid = ;
} //check row, col, subgrid
for(int i = ; i < ; i ++)
{
//check row
if(i != col && board[row][i] == board[row][col])
return false; //check col
if(i != row && board[i][col] == board[row][col])
return false; //check subgrid
int r = gid/*+i/;
int c = gid%*+i%;
if((r != row || c != col) && board[r][c] == board[row][col])
return false;
}
return true;
}
};

check的另一种实现方式如下:

bool check(vector<vector<char> > &board, int pos)
{
int v = pos/;
int h = pos%;
char target = board[v][h];
//row
for(vector<char>::size_type st = ; st < ; st ++)
{
if(st != h)
{
if(target == board[v][st])
return false;
}
} //col
for(vector<char>::size_type st = ; st < ; st ++)
{
if(st != v)
{
if(target == board[st][h])
return false;
}
} //subgrid
int beginx = v/*;
int beginy = h/*;
for(int i = beginx; i < beginx+; i ++)
{
for(int j = beginy; j < beginy+; j ++)
{
if(i != v && j != h)
{
if(target == board[i][j])
return false;
}
}
} return true;
}

【LeetCode】37. Sudoku Solver的更多相关文章

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

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

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  4. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  5. 【LeetCode】 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  6. 【leetcode】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  7. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

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

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

随机推荐

  1. org-mode入门教程

    org-mode 入门教程By Z.H. Fu 切问录 www.fuzihao.org org-mode 入门教程 org-mode是Emacs提供的一个强大的编辑模式,可以用于做会议笔记以及制作各种 ...

  2. Coursera课程《Python数据结构》中课程目录

    Python Data Structures Python Data Structures is the second course in the specialization Python for ...

  3. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  4. 原创D3D几何实例化的DEMO

    CUBE的几何实例化DEMO 鼠标右键按下并拖动         旋转视角WSAD                         前后左右RF                             ...

  5. Informatica 常用组件Source Qualifier之八 Distinct

    如果希望 PowerCenter 从源选择唯一值,您可以使用"选择相异"选项.例如,您可以使用此功能从列出总销售额的表中提取唯一客户标识.使用"选择相异"过滤器 ...

  6. 局域网Cesium离线影像及瓦片影像地图加载【转】

    http://www.mamicode.com/info-detail-2161992.html 1.Cesium简介 优点: cesium展示地图数据效果比较好,解析2D地图各种不同服务类型的数据源 ...

  7. Android之SlideMenu实例Demo

    年末加班基本上一周都没什么时候回家写代码,回到家就想睡觉,周末难得有时间写个博客,上次写了一篇关于SlideMenu开源项目的导入问题,这次主要讲讲使用的问题,SlideMenu应用的广泛程度就不用说 ...

  8. Builder 建造者模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. Oracle中的日期处理方法

    日期处理方法                                                        当前日期和时间 Select sysdate from dual; 本月最后 ...

  10. 极客Web前端开发资源集锦

    本周我们带来的前端推荐包含当前热门的bootstrap,html5,css3等技术内容和新闻话题,如果你还想近一步学习如何开发,还可以关注我们的极客课程库,里面涵盖了现代开发技术的‘学’与‘习’的全新 ...