leetcode problem 37 -- 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...
思路:
搜索加剪枝。
首先设立3个辅助二维数组:rows, columns, grids来维护目前的数独状态,rows[i][k](1<=i, k<=9)表示第i行是否占用了数字k,同理colums[i][k]表示第i列是否占用了数字k,grid[i][k]表示第i个格子是否占用了数字k,
然后第一次读入二维数组,更新rows, columns和grids.
第二次读二维数组,对每个为'.'的元素根据rows,columns和grid来枚举可能的值。如果没有可以枚举的值,直接剪枝返回。
代码如下:
Runtime: 5 ms
static int columns[][];
static int rows[][];
static int grids[][]; int search(char *board[], int startI, int startJ) { for(int i = startI; i < ; ++i) {
for(int j = i == startI ? startJ : ; j < ; ++j) {
if (board[i][j] == '.') {
for (int k = ; k <= ; ++k) {
if (!rows[i][k] && !columns[j][k] && !grids[i/ + j/ *][k]) {
rows[i][k] = ;
columns[j][k] = ;
grids[i/ + j/ *][k] = ; if (search(board, i, j+) == ) {
board[i][j] = k + '';
return ;
}
rows[i][k] = ;
columns[j][k] = ;
grids[i/ + j/ *][k] = ;
}
}
return ;
}
}
}
return ;
} void solveSudoku(char * board[]) {
memset(columns, , sizeof(columns));
memset(rows, , sizeof(rows));
memset(grids, , sizeof(grids)); for(int i = ; i < ; ++i) {
for(int j = ; j < ; ++j) {
if (board[i][j] != '.') {
rows[i][board[i][j] - ''] = ;
columns[j][board[i][j] - ''] = ; int tmp = i/ + j/ * ;
grids[tmp][board[i][j] - ''] = ;
}
}
}
search(board, , );
}
leetcode problem 37 -- Sudoku Solver的更多相关文章
- [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 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
随机推荐
- Android Studio Error2
ECLIPSE ANDROID PROJECT IMPORT SUMMARY ====================================== Ignored Files: ------- ...
- 汽车行业的DMS系统 IT不变应万变
DMS是针对4S店的整车销售.零配件仓库.售后维修服务(含车间管理).客服服务涵盖4S店业务进行完整管理的系统,是对销售.维修.客户关系进行一系列的整合.其中,服务的预约.进店接待都有着跟踪处理,客户 ...
- 【分享】4412开发板-嵌入式Linux开发须要掌握的基础知识和技能
本文转自迅为电子论坛:http://www.topeetboard.com 1.Linux 基础 安装Linux操作系统 Linux文件系统 Linux经常使用命令 Linux启动过程具体解释 熟悉L ...
- js 数组排序要注意的问题,返回的值最好为 -1, 0, 1之间的值
var test10Elements = [7, 6, 5, 4, 3, 2, 1, 0, 8, 9]; var comparefn = function (x, y) { return x - y; ...
- 你真的会用 SDWebImage?
SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析如何合理使用SDWebImage. 使用场景:自定义的UITableViewCell上有 ...
- Objective-C,复合类,Composition
复合类 5.复合类现实中,复杂的对象都是由较小和较为简单的对象构成:由简单对象创建复杂对象的过程称作合成.合成通常使用在有has-a关系的对象:通常的基本数据类型可以满足构造简单和小的对象.为了从小 ...
- 关于Http协议的解析
HTTP概述 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. HTTP就是一个通信规则,通信规则规定 ...
- Redis常见问题及处理方法
一.Redis状态检查 唯一标记一个redis实例的是ip和端口,前端是用tcp方式来访问redis的,我们提供给应用访问的是一个ip+63379(一般使用63379) 端口.因此我们执行如下命令检查 ...
- windows共享文件夹如何让CentOS 6.5读取
http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...
- [转载]ubuntu Atheros Communications Device 1083 驱动
Ubuntu 版本: Ubuntu server 10.10 在2016-03-26 上午时,拆开公司一台server电脑的CPU风扇不转,电源都烧掉了(潮湿的原因)... 在2016-03-28 打 ...