leetcode37——Sudoku Solver

原创 2018年04月16日 17:36:04

题目大意:填写数独

分析:dfs。注意得到第一个解就要及时返回,否则就会出错。

代码:JAVA转载自https://blog.csdn.net/zdavb/article/details/48165059

public class Solution {
public void solveSudoku(char[][] board) {
solve(board);
}
private boolean solve(char[][] board) {
for (int row = 0;row<9;row++) {
for (int col = 0;col<9;col++) {
if (board[row][col] == '.') {
for (char i = '1';i <= '9';i++) {
board[row][col] = i;
if (isValid(board, row, col) && solve(board))
return true;
board[row][col] = '.';
}
return false;
}
}
}
return true;
}
private boolean isValid(char[][] board, int row, int col) {
for (int i = 0;i<9;i++) {
if (i != col && board[row][i] == board[row][col])
return false;
}
for (int i = 0;i<9;i++) {
if (i != row && board[i][col] == board[row][col])
return false;
}
int beginRow = 3 * (row / 3);
int beginCol = 3 * (col / 3);
for (int i = beginRow;i<beginRow + 3;i++) {
for (int j = beginCol;j<beginCol + 3;j++) {
if (i != row && j != col && board[i][j] == board[row][col])
return false;
}
}
return true;
}
}

[leetcode-37]Sudoku Solver(java)

问题描述: Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated...
  • zdavb
  • zdavb
  • 2015-09-01 22:20:17
  • 1109

Leetcode 37 Sudoku Solver

做了这道题,对backtracking的理解又加深了一点点。 1 每个backtracking的题目,最好都有独立判断isValid的程序,这样架构清楚。同时,valid判断函数在这里可以稍微研究一...
  • zxzxy1988
  • zxzxy1988
  • 2013-02-17 21:53:25
  • 7881

leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Su...
  • wangyaninglm
  • wangyaninglm
  • 2016-05-04 22:07:52
  • 1241

LeetCode37. Sudoku Solver

题意:解9*9数独做法:回溯 对每一个还没填充的格子,尝试1~9这9个数字,如果是合法的,则继续填充下一个格子,否则回溯。 判断合法:只需要判断对应行、对应列、对应3*3,有没有矛盾。设未被填充的...
  • wmdshhz0404
  • wmdshhz0404
  • 2016-12-10 14:50:23
  • 217

Sudoku Solver -- LeetCode

原题链接: http://oj.leetcode.com/problems/sudoku-solver/  这道题的方法就是用在N-Queens中介绍的常见套路。简单地说思路就是循环处理子问题,对于...
  • linhuanmars
  • linhuanmars
  • 2014-03-10 03:56:50
  • 20489

60. leetCode37: Sudoku Solver

【题目】:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated b...
  • wjx_sky
  • wjx_sky
  • 2018-03-27 19:47:21
  • 6

python实现 LeetCode37——Sudoku Solver

数独游戏,用了dfs的深度优先树。25行和26行的else将该位置改为点,是在一次dfs中的,相当于在for循环中不断尝试c值中的赋值。如果所有的c值都不对的时候会进入27行的return false...
  • xiaoling_000666
  • xiaoling_000666
  • 2018-03-28 16:55:19
  • 10

Leetcode37 Sudoku Solver

Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cell...
  • u011513853
  • u011513853
  • 2015-07-29 14:25:59
  • 272

LeetCode37 - Sudoku Solver

Sudoku Solver题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty ce...
  • yanqinghe123
  • yanqinghe123
  • 2017-09-17 18:28:31
  • 88

LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ar...
  • runningtortoises
  • runningtortoises
  • 2015-05-19 13:11:05
  • 2121
收藏助手
不良信息举报
您举报文章:leetcode37——Sudoku Solver
举报原因:
原因补充:

(最多只允许输入30个字)