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 ...
随机推荐
- UIPickerView用法(左右比例,整体大小,字体大小)
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; pickerView.autoresizingM ...
- Ubuntu12.10硬盘安装
今天介绍如下如何在Win7环境下从硬盘安装Ubuntu(我使用的版本是12.10). 1.下载Ubuntu ISO镜像文件ubuntu-12.10-desktop-i386.iso. 2.使用压缩软件 ...
- PHP高效获取远程图片尺寸和大小(转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- UNIX标准化及实现之功能测试宏
在头文件中定义了很多POSIX.1和XSI的符号.但是除了POSIX.1和XSI的定义之外,大多数实现在这些头文件中也加上了它们自己的定义.如果在编译一个程序时,希望它只使用POSIX定义而不使用任何 ...
- android学习——popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. [java] view plaincopy private void showPopupWindow(View pare ...
- Python 基础【第七篇】集合
一.集合的概念: 不同元素的集合 二.集合的方法: 方法 用法 范例 set() 过滤掉重复 设置成为集合 >>> subset=set([1,1,2,3,4,4,6]) >& ...
- 关于Eclipse插件开发-----加入首选项(preferencePages)
选择主菜单"窗口---->首选项"命令打开"首选项"窗口.此窗口是Eclipse设置项的集中营, 修改plugin.xml文件,设置首选项的扩展点: pl ...
- 关于原生AJAX和jQueryAJAX的编程
1.回顾传统Ajax开发步骤 ①:创建xmlHttpRequest对象 var xmlHttp = creatHttpRequest(); ②:绑定回调函数 xmlHttp.onreadystatec ...
- Oracle报错:ORA-01747: user.table.column, table.column 或列说明无效
1.检查sql书写正确性 2.如果sql书写正确,则是由于数据库列名起的不好引起的,名字用到了数据库的关键字. 如果列很多,又不好确定是哪个列名使用了关键字,以下建议可供参考: 我用以下方法定位 se ...
- php中的匿名函数(Anonymous functions)和闭包函数(closures)
一:匿名函数 (在php5.3.0 或以上才能使用) php中的匿名函数(Anonymous functions), 也叫闭包函数(closures), 允许指定一个没有名称的函数.最常用的就是回调函 ...