[leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法。回溯法当时初学的时候在思路上比较拧,不容易写对。写了几个回溯法的算法之后心里总算有了点底。回溯法的代码一般都是长成下面这样子:
void backtracking(int[] arr, int boundary, int current, int[] result)
{
if(current>=boundary) //到达终止条件
{
//判断result是否符合要求
//如果符合,记录结果,即result数组的值 return;
} for(int i=0; i<arr.length;i++)
{
STEP 1: //从arr中取出第i个元素
STEP 2: //用第i个元素加入到result中
STEP 3: backtracking(arr, boundary, current+1, result); //进入下一步探索
STEP 4: //把第i个元素从result中拿出来
STEP 5: //把第i个元素再放回arr中去
}
}
回溯法代码特点非常鲜明,一上来先判断递归的终止条件,到达终止条件,就检查结果是否合格,合格就记录下来。而回溯法的精髓就在下面的for循环中,一共有5个步骤,后两个步骤刚好是前两个步骤的反操作,因此才被成为‘回溯’。我们可以认为,回溯法就是暴力(brute force)搜索的一种优化方式。
扯了点闲话,来看Sudoku Solver题:
/*************************Question**************************
* 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.
*
* 5 3 . . 7 . . . .
* 6 . . 1 9 5 . . .
* . 9 8 . . . . 6 .
* 8 . . . 6 . . . 3
* 4 . . 8 . 3 . . 1
* 7 . . . 2 . . . 6
* . 6 . . . . 2 8 .
* . . . 4 1 9 . . 5
* . . . . 8 . . 7 9
* A sudoku puzzle...
* 5 3 4 6 7 8 9 1 2
* 6 7 2 1 9 5 3 4 8
* 1 9 8 3 4 2 5 6 7
* 8 5 9 7 6 1 4 2 3
* 4 2 6 8 5 3 7 9 1
* 7 1 3 9 2 4 8 5 6
* 9 6 1 5 3 7 2 8 4
* 2 8 7 4 1 9 6 3 5
* 3 4 5 2 8 6 1 7 9
*
* ...and its solution.
***********************************************************/
我的代码如下:
bool isValid(char *board[], int row, int column, char number){
for(int i = ; i < ; i++){
if(board[row][i] == number){
return false;
}
if(board[i][column] == number){
return false;
}
}
for (int i = ; i < ; i++){
int a = (row / ) * + i / ;
int b = (column / ) * + i % ;
if(board[a][b] == number){
return false;
}
}
return true;
}
bool sudokuSolution(char *board[], int row, int column){
if(row >= ){
return true;
}
if(board[row][column] != '.'){
if(column >= ){
return sudukuSolution(board, row+, );
}else {
return sudukuSolution(board, row, column+);
}
}
for(int i=; i<;i++){
if(isValid(board, row, column, (char)(''+i))){
board[row][column] = (char)(''+i);
if(column >= && sudukuSolution(board, row+, )){
return true;
}else if (sudukuSolution(board, row, column +)){
return true;
}
board[row][column] = '.';
}
}
return false;
}
void solveSudoku(char *board[]){
if(sudokuSolution(board, , )){
//successfully find the solution
} else {
// cannot find a solution
}
return;
}
代码使用C语言编写,其中bool sudokuSolution函数就很明显有回溯法的函数结构。
[leetcode]算法题目 - Sudoku Solver的更多相关文章
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [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 ...
- [leetcode]算法题目 - Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- LeetCode OJ:Sudoku Solver(数独游戏)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 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
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
随机推荐
- 第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList();
list.add(new Student("Tom", 18, 100, "class05")); list.add(new Student("Jer ...
- mongo数据备份及恢复脚本
#!/bin/bashtime="$(date +"%Y.%m.%d")" id=`echo "show dbs;"|/usr/local/ ...
- 【学习笔记】Wireshark的用法
计算机网络课上,需要我们灵活运用网络协议分析仪wireshark,最近一直在看,感觉有点难,并不是软件本身操作难,而是看懂一大群包的含义难,这个难主要也因为它是全英文的~~.. 好了,大致总结一下,基 ...
- C#编程普通型计算器 经验与感悟
先贴图: 这是用C# 语言编写的普通型计算器,功能基本模仿Windows8自带计算器程序(版本6.3,内部版本9600).支持加.减.乘.除.退格.清除.平方根.倒数.相反数.连续四则.连续等号.自动 ...
- STM32F407 RCC时钟配置
新上手项目需要使用STM32F407,在使用STM32F1系列时就喜欢自己用库函数设置系统时钟,所以F4也打算这么做,但是遇到了一些问题. 其中百度文库有篇文章关于RCC的文章将的不错,地址:http ...
- 如何更改nginx网站根目录 以及解析php
nginx默认网站根目录为/usr/local/nginx/html,如果想要将它改成/data/www 需配置 vim /usr/local/nginx/conf/nginx.conf 将其中的字段 ...
- Markdown learning
For details, please refer to Markdown # Markdown Learning #h1 ##h2 ###h3 ####h4 **Bond** *italic* Th ...
- System V进程间通信
一)Linux环境进程间通信(一)管道及有名管道http://www.ibm.com/developerworks/cn/linux/l-ipc/part1/二)Linux环境进程间通信(二): 信号 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- ANE接入平台心得记录(安卓)
开发环境:FlashBuilder4.7 AIR13.0 Eclipse 由于我懒得陪安卓的开发环境所以我下载了包含安卓SDK Manager的Eclipse,其实直接用FlashBuilder开发A ...