37. Sudoku Solver
来源:互联网 发布:vscode 代码片段 编辑:程序博客网 时间:2024/05/18 00:43
1刷
题目也是很简单,判断用了上一题的代码,dfs一次ac!!!!主要是写多了一个小东西一直bug
Else 后面多了东西
还有map<>m[10]这个东西传参解决不了,大神说可以用vector存map,我后来是直接用了全局变量23333333
class Solution {public: bool isOK(vector<vector<char>>& board){ for(int i = 0; i < 9; ++ i){ unordered_map<char, bool> mm1; unordered_map<char, bool> mm2; unordered_map<char, bool> mm3; for(int j = 0; j < 9; ++ j){ if(mm1[board[i][j]] == true) return false; mm1[board[i][j]] = true; if(mm2[board[j][i]] == true) return false; mm2[board[j][i]] = true; if(mm3[board[i/3*3+j/3][i%3*3+j%3]] == true) return false; mm3[board[i/3*3+j/3][i%3*3+j%3]] = true; } } return true; } unordered_map<char, bool>m1[9]; unordered_map<char, bool>m2[9]; unordered_map<char, bool>m3[9]; bool numisOK(char t, int i, int j){ if(m1[i][t] != true && m2[j][t] != true && m3[i/3*3+j/3][t] != true) return true; return false; } int f = 1; void solveit(vector<vector<char>>& board, int a, int b){ if(!f) return; if(a == 9){ if(isOK(board)) f = 0; return; } if(board[a][b] != '.'){ if(b == 8) solveit(board, a + 1, 0); else solveit(board, a, b + 1); return; } for(int i = 1; i <= 9; ++ i){ char c = i + 48; if(numisOK(c, a, b)){ m1[a][c] = true; m2[b][c] = true; m3[a/3*3+b/3][c] = true; board[a][b] = c; if(b == 8) solveit(board, a + 1, 0); else solveit(board, a, b + 1); if(!f) return; m1[a][c] = false; m2[b][c] = false; m3[a/3*3+b/3][c] = false; board[a][b] = '.'; } else continue; } return; } void solveSudoku(vector<vector<char>>& board) { for(int i = 0; i < 9; ++ i){ for(int j = 0; j < 9; ++ j){ if(board[i][j] != '.'){ m1[i][board[i][j]] = true; m2[j][board[i][j]] = true; m3[i/3*3+j/3][board[i][j]] = true; } } } solveit(board, 0, 0); }};
2刷
没有2刷因为1刷一次就ac了,简单dfs,3刷可以看看一个题解为2ms的代码,自己的代码是116ms,,差了50倍!
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
- 33. Search in Rotated Sorted Array(无敌2分)
- 34. Search for a Range
- 怎么实现类似"今日头条"app
- 35. Search Insert Position
- 36. Valid Sudoku
- 37. Sudoku Solver
- 38. Count and Say
- [Algorithm]随机抖动整数拆分
- 爬虫配置必备:JQuery|querySelector|Cheerio DOM节点选择干货集
- 源码编译Asterisk系统环境要求
- 九度OJ-1176-
- 九度OJ-1201-二叉排序树
- fetch_array()与fetch_assoc()的用法
- ssh学习整理笔记