37. Sudoku Solver
来源:互联网 发布:特比尔定位软件 编辑:程序博客网 时间:2024/06/05 07:10
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…
…and its solution numbers marked in red.
思路;
DFS + BackTracking
class Solution { public void solveSudoku(char[][] board) { if(board == null || board.length == 0) return; solve(board); } public boolean solve(char[][] board){ for(int i=0; i<board.length; i++){ for(int j=0; j<board[0].length; j++){ if(board[i][j] == '.'){ for(char c = '1'; c <= '9'; c++){ if(isValid(board, i, j, c)){ board[i][j] = c; if(solve(board)) return true; else board[i][j] = '.'; } } return false; } } } return true; } private boolean isValid(char[][] board, int row, int col, char c){ for(int i = 0; i < 9; i++){ if(board[row][i] == c || board[i][col] == c) return false; if(board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] == c) return false; } return true; }}
阅读全文
0 0
- LeetCode --- 37. Sudoku Solver
- LeetCode 37.Sudoku Solver
- [Leetcode] 37. Sudoku Solver
- 37. Sudoku Solver
- 37. Sudoku Solver(Hard)
- [leetcode] 37. Sudoku Solver
- Leetcode 37. Sudoku Solver
- 37. Sudoku Solver
- leetcode 37. Sudoku Solver
- 37. Sudoku Solver
- 37. Sudoku Solver
- LeetCode 37. Sudoku Solver
- 37. Sudoku Solver
- leetcode.37. Sudoku Solver
- LeetCode-37.Sudoku Solver
- leetcode 37. Sudoku Solver
- (Leetcode)37. Sudoku Solver
- 37. Sudoku Solver
- C 数据结构 笔记(五) —— 循环队列
- Jquery 获取选中的Select的Text文本值和Value值。
- 关于angular。js+ionic框架解析HTML结构的问题
- redis常用指令总结以及功能介绍
- hadoop之安装(一)
- 37. Sudoku Solver
- vue---使用vue编写插件,基于vue2.x编写常用toast loading插件,一键复用组件
- 使用 Yocto Project 构建自定义嵌入式 Linux 发行版
- (OK) Android Binder设计与实现
- 使用node实现歌词滚动输出
- 02.替换空格
- Cassandra的数据模型的理解
- 数组统计不及格人数
- 浅谈 MVP in Android