LeetCode(37) Sudoku Solver
来源:互联网 发布:googlenet tensorflow 编辑:程序博客网 时间:2024/05/14 02:25
题目如下:
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
分析如下:
这道题目基本上和N-QueenI,N-Queen II 是一个套路,套用DFS + Backtracking的思路就可以,代码也是一个风格的。
不过这两题有个区别,需要注意一下。
在N-Queen I 中,返回所有正确的解,N-Queen II中,返回所有正确的解的数量,无论是N-Queen I 还是N-Queen II,都需要找到所有的解。
换句话说,需要把search tree全部搜索一遍,才能得到答案,如果只搜到某一个答案就返回,得到的结果是错误的。
在Sudoku中,只需要把找到的第一个解(并且题目的输入保证了将只产生一个解)返回就可以了,如果不立刻返回而是继续在搜索树(搜索树的定义见这里的图1,图2)中遍历的话,将得到错误的答案。
Version 1:
//77msclass Solution {private: bool isOver; //是否已经找到一个正确解。public: void dfs (vector<vector<char> > &board, int i, int j, int n ) { //if (this->isOver) return; if (j >= n) { dfs(board, i+1, 0, n); } else if (i == n){ this->isOver = true; return ; } else if (board[i][j] != '.'){ dfs(board, i, j+1, n); } else { for (int k = 1; k <= n; ++k){ board[i][j] = (char)('0' + k); if (isValid(board, i, j, n)){ dfs(board, i, j+1 , n); } if (this->isOver) return; //找到一个解救跳出,防止遍历整个search tree board[i][j] = '.'; } } return ; } bool isValid(vector<vector<char>> &board, int i , int j, int n) { for (int index = 0; index < n; ++index){ if (index != j && board[i][index] == board[i][j]) { return false; } } for (int index = 0; index < n; ++index){ if (index != i && board[index][j] == board[i][j]) { return false; } } int index_i = i / 3; int index_j = j / 3; for (int x = index_i * 3; x < index_i * 3 + 3; ++x) { for (int y = index_j * 3; y < index_j * 3 + 3; ++y) { if ((x!=i || y != j) && board[x][y] == board[i][j]) { //注意这里逻辑是"或"||,不是"与" return false; } } } return true; } void solveSudoku(vector<vector<char> > &board) { if (board.size() == 0 || board[0].size() == 0) return; //简洁的方式去验证输入合法性 this->isOver = false; dfs(board, 0, 0, 9); }};
Version 2:
// 69msclass Solution {public: bool dfs (vector<vector<char> > &board, int i, int j, int n ) { //函数写成return bool型,这样也能帮助在找到第一个解的时候return. if (j >= n) { return dfs(board, i+1, 0, n); } else if (i == n){ return true; } else if (board[i][j] != '.'){ return dfs(board, i, j+1, n); } else { for (int k = 1; k <= n; ++k){ board[i][j] = (char)('0' + k); if (isValid(board, i, j, n)){ if (dfs(board, i, j+1 , n)) // 如果找到第一个解,就及时return return true; } board[i][j] = '.'; } } return false; } bool isValid(vector<vector<char>> &board, int i , int j, int n) { for (int index = 0; index < n; ++index){ if (index != j && board[i][index] == board[i][j]) { return false; } } for (int index = 0; index < n; ++index){ if (index != i && board[index][j] == board[i][j]) { return false; } } int index_i = i / 3; int index_j = j / 3; for (int x = index_i * 3; x < index_i * 3 + 3; ++x) { for (int y = index_j * 3; y < index_j * 3 + 3; ++y) { if ((x!=i || y != j) && board[x][y] == board[i][j]) { //注意这里逻辑是"或"||,不是"与" return false; } } } return true; } void solveSudoku(vector<vector<char> > &board) { if (board.size() == 0 || board[0].size() == 0) return; //简洁的方式去验证输入合法性 dfs(board, 0, 0, 9); }};
0 0
- Leetcode 37 Sudoku Solver
- Leetcode 37: Sudoku solver
- [leetcode 37] Sudoku Solver
- LeetCode(37) Sudoku Solver
- leetcode 37:Sudoku Solver
- leetcode 37: Sudoku Solver
- Leetcode #37 Sudoku Solver
- leetcode-37 Sudoku Solver
- leetcode 37:Sudoku Solver
- LeetCode 37: Sudoku Solver
- LeetCode 37 - Sudoku Solver
- LeetCode(37)-Sudoku Solver
- leetcode 37 Sudoku Solver
- LeetCode 37 Sudoku Solver
- LeetCode 37 SudoKu Solver
- LeetCode 37 Sudoku Solver
- leetcode-37-sudoku solver
- Leetcode (37) Sudoku Solver
- 私网安装Ambari实现Hadoop、Hbase等自动化安装(离线 本地源)
- HotSpot运行时概览#1
- week2【项目2】长方体类
- java tcnative-1.dll 各个版本的地址
- linux进程编程-替换进程映-exec系列函数(execl,execlp,execle,execv,execvp)使用
- LeetCode(37) Sudoku Solver
- 盎既靠晨酵掌栽谮么适构厦缸坪创儋偈忧什蹦
- http://www.52kk.com/space.php
- 历届试题 打印十字图 Java蓝桥杯
- 数据库第二次作业
- 使用VBA将一个EXCEL大表自动分成一系列小表(保留首行)
- Introduction to the t Distribution (non-technical)
- 数据库锁
- 【009】【JVM——类加载机制】