Leetcode | Valid Sudoku & Sudoku Solver

判断valid,没有更好的方法,只能brute force。


 1 class Solution {
2 public:
3 bool isValidSudoku(vector<vector<char> > &board) {
4
5 int n;
6 for (int i = 0; i < 9; ++i) {
7 vector<bool> contained(9, false);
8 for (int j = 0; j < 9; ++j) {
9 if (board[i][j] == ‘.‘) continue;
10 n = board[i][j] - ‘0‘ - 1;
11 if (contained[n]) return false;
12 contained[n] = true;
13 }
14 }
15
16 for (int i = 0; i < 9; ++i) {
17 vector<bool> contained(9, false);
18 for (int j = 0; j < 9; ++j) {
19 if (board[j][i] == ‘.‘) continue;
20 n = board[j][i] - ‘0‘ - 1;
21 if (contained[n]) return false;
22 contained[n] = true;
23 }
24 }
25
26 for (int i = 0; i < 3; ++i) {
27 for (int j = 0; j < 3; ++j) {
28 vector<bool> contained(9, false);
29 for (int k = 0; k < 3; ++k) {
30 for (int m = 0; m < 3; ++m) {
31 if (board[i*3+k][j*3+m] == ‘.‘) continue;
32 n = board[i*3+k][j*3+m] - ‘0‘ - 1;
33 if (contained[n]) return false;
34 contained[n] = true;
35 }
36 }
37 }
38 }
39 return true;
40 }
41 };

求解决方案也只有backtrack。


 1 class Solution {
2 public:
3 void solveSudoku(vector<vector<char> > &board) {
4 list<int> unsolved;
5 getUnsolved(board, unsolved);
6 recursive(board, unsolved);
7 }
8
9 bool recursive(vector<vector<char> > &board, list<int> &unsolved) {
10 if (unsolved.empty()) return true;
11 int loc = unsolved.front();
12 int row = loc / 9;
13 int col = loc % 9;
14
15 vector<bool> contained(9, false);
16 int n;
17 for (int i = 0; i < 9; ++i) {
18 if (board[row][i] != ‘.‘) {
19 contained[board[row][i] - ‘0‘ - 1] = true;
20 }
21 if (board[i][col] != ‘.‘) {
22 contained[board[i][col] - ‘0‘ - 1] = true;
23 }
24 }
25
26 row = row / 3; col = col / 3;
27 for (int i = 0; i < 3; ++i) {
28 for (int j = 0; j < 3; ++j) {
29 if (board[row * 3 + i][col * 3 + j] != ‘.‘) {
30 contained[board[row * 3 + i][col * 3 + j] - ‘0‘ - 1] = true;
31 }
32 }
33 }
34
35 row = loc / 9; col = loc % 9;
36 for (int i = 0; i < 9; ++i) {
37 if (!contained[i]) {
38 board[row][col] = i + 1 + ‘0‘;
39 unsolved.pop_front();
40 if (recursive(board, unsolved)) return true;
41 board[row][col] = ‘.‘;
42 unsolved.push_front(loc);
43 }
44 }
45
46 return false;
47 }
48
49 void getUnsolved(vector<vector<char> > &board, list<int> &unsolved) {
50 for (int i = 0; i < 9; i++) {
51 for (int j = 0; j < 9; ++j) {
52 if (board[i][j] == ‘.‘) {
53 unsolved.push_back(i * 9 + j);
54 }
55 }
56 }
57 }
58 };

用unsolved数组可以避免每次都需要从头扫到尾去找下一个元素。

用contained数组先保存了在该行该格该九宫格里已经存在的数字。这样就可以直接去试验剩下的数字,而不需要每次都再检查一遍插入的值是否合法。

backtrack是一个要有返回值,否则都不知道你backtrack到头了没,是否找到解决方案了。

Leetcode | Valid Sudoku & Sudoku Solver,码迷,mamicode.com

时间: 2024-10-05 11:21:21

Leetcode | Valid Sudoku & Sudoku Solver的相关文章

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

LeetCode: Valid Sudoku [035]

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (part

[leetcode]Valid Sudoku @ Python

原题地址:https://oj.leetcode.com/problems/valid-sudoku/ 题意: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudo

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Parentheses [020]

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

LeetCode: Valid Palindrome [125]

[题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider

LeetCode——Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

Leetcode: Valid Parentheses 有效的括号匹配

Valid Parentheses: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

leetcode第一刷_Sudoku Solver

这道题简直是耻辱啊,居然被吓得不敢做,终于开始写还犯下了各种低级错误,花了好久的时间. 其实如果想明白81*9其实是很小的规模的话,早就想到用回溯法了,这不是跟八皇后完全一样的嘛.每次填入的时候,验证一下合不合理,其中合不合理在上一个问题中已经讨论过了,对当前位置讨论更简单. 所的头头是道,你会问"那你是错在哪呢?"你猜啊.我在判断一个小方格时候合理时,走了很多弯路.一开始想的是用循环和减法,找到当前位置所在的小方格最左上角的位置.其实完全不用啊亲,当听到实验室的同学说3*(i/3)和