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: ------- ...
- android常见错误-
将library中的报错项删除,然后点击[add]正确的appcompat
- HDU 5002 Tree LCT 区间更新
Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- HDU 5478 Can you find it 随机化 数学
Can you find it Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 三、Socket之UDP异步传输文件-多文件传输和文件MD5校验
本文接着上一篇文章二.Socket之UDP异步传输文件,在上一篇文章的基础上实现多文件的传输和文件传输完成后进行完整性校验. 要实现多文件的传输,必须要对文(2)中发送文件的数据格式进行改进,必须加入 ...
- highcharts js报表工具(报表插件)
highcharts报表工具(报表插件.图表工具) highcharts效果在线演示(可查看源代码): http://www.hcharts.cn/demo/index.php?p=56 Highc ...
- com.code.servlet
package com.code.servlet; import java.io.IOException; import java.util.LinkedHashMap; import java.ut ...
- Windows系统下用命令行编译C/C++程序过程总结
转自:http://www.cnblogs.com/caikehe/archive/2013/01/12/2858017.html (1)先用记事本编写如下所示的代码,并另存为hello.cpp,假设 ...
- OC中-方法到底是如何使用的?
方法:方法是Objective-C独有的一种结构,只能在Objective-C中声明.定义和使用,C语言不能声明.定义和使用. 1.类方法以+号开头,对象方法以-号开头+ (void) init; ...
- Unix 网络编程(2)——TCP API
TCP C/S套接口函数一般调用过程及基本函数 如上图所示的TCP连接的基本过程.一般来说,服务器先于客户端运行,服务器程序运行的基本过程是: socket()函数创建服务器段socket. bind ...