Leetcode37--->Sudoku Solver(填充数独)
题目: 给定一个不完整的数独,要求填充好数独;最初给出的数独是有效的,且假设一定有答案;
举例:
![]()
A sudoku puzzle...
![]()
解题思路:
该题与青蛙走迷宫问题很相似,都是用深度优先;
代码如下:
public class Solution {
public void solveSudoku(char[][] board) {
if(board == null || board.length < 9 || board[0].length < 9)
return;
solve(board, 0);
}
public boolean solve(char[][] board, int position)
{
if(position == 81) // position可以唯一确定一个坐标
return true;
int row = position / 9;
int col = position % 9;
if(board[row][col] == '.')
{
for(int i = 1; i <= 9; i++)
{
board[row][col] = (char)('0' + i);
if(checkValid(board, position)) // 检查将board[row][col]修改后,行列块是否有效
{
if(solve(board, position + 1)) // 深度搜索
return true;
}
board[row][col] = '.';
}
}
else
{
if(solve(board, position + 1))
return true;
}
return false;
}
public boolean checkValid(char[][] board, int position)
{
int row = position / 9;
int col = position % 9;
char target = board[row][col];
for(int j = 0; j < 9; j++)
{
if(j != col)
{
if(target == board[row][j]) // 判断除过col列,row行是否有taeget
return false;
}
if(j != row)
{
if(target == board[j][col]) // 判断除过row行,col列是否有target
return false;
}
}
int beginx = row / 3 * 3;
int beginy = col / 3 * 3;
for(int i = beginx; i < beginx + 3; i ++) // 块中是否有target
{
for(int j = beginy; j < beginy + 3; j ++)
{
if(i != row && j != col)
{
if(target == board[i][j])
return false;
}
}
}
return true;
}
}
Leetcode37--->Sudoku Solver(填充数独)的更多相关文章
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- Sudoku Solver, 求数独
问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...
- [LeetCode] 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 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode37 Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- LeetCode OJ:Sudoku Solver(数独游戏)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- rest_framework序列化组件
一.Django自带的序列化组件 ==>对象序列化成json格式的字符串 from django.core import serializers from django.core import ...
- 常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化
import android.content.Context; import android.content.res.AssetManager; import android.content.res. ...
- Writable和Comparable
WritableComparable接口相当于继承了上述两个接口的新接口 : Public interface WritableComparable<T>extends Writable, ...
- [神经网络]一步一步使用Mobile-Net完成视觉识别(一)
1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第一篇,环境配置篇. 先打开tensorflow object detection api 看看需要什 ...
- kubernetes监控-prometheus(十六)
监控方案 cAdvisor+Heapster+InfluxDB+Grafana Y 简单 容器监控 cAdvisor/exporter+Prometheus+Grafana Y 扩展性好 容器,应用, ...
- cocos2d-x中的字符串操作
1:循环体中字符串的构造. 通常用于多个有规律的文件的名字,诸如:[NSString stringWithFormat:@"filed.png",i].我们可以通过spr ...
- c++ 指针数组,输入4个季度的花费,计算出总花费
#include <iostream> #include <array> #include <string> const int Seasons = 4; cons ...
- 洛谷 P1228 【地毯填补问题】
事实上感觉四个的形状分别是这样: spj报错: 1:c 越界 2:x,y 越界 3:mp[x][y] 已被占用 4:mp[x][y] 从未被使用 题解: 初看这个问题,似乎无从下手,于是我们可以先考虑 ...
- [LUOGU] NOIP提高组模拟赛Day1
题外话:以Ingress为题材出的比赛好评,绿军好评 T1 考虑枚举第\(i\)个人作为左边必选的一个人,那左边剩余\(i-1\)个人,选法就是\(2^{i-1}\),也就是可以任意选或不选,右侧剩余 ...
- Go IO && bufio
IO IO包 是对数据流的操作.从哪里来, 怎么处理,再到哪里去. 图片来源 https://medium.com/learning-the-go-programming-language/strea ...