[Leetcode]Sudoku Solver
来源:互联网 发布:淘宝跳转链接代码 编辑:程序博客网 时间:2024/05/29 04:17
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...
class Solution {public: /*algorithm: DFS */ void solveSudoku(vector<vector<char> > &board) { dfs(board, 0, 0); } bool dfs(vector<vector<char> > &board, int x, int y) { if (x == 9) return true; if (y >= 9) return dfs(board, x + 1, 0); if (board[x][y] == '.') { for (int k = 1; k <= 9; ++k) { board[x][y] = k + '0'; if (isValid(board, x , y)) { if (dfs(board, x, y + 1)) return true; } board[x][y] = '.'; } } else { return dfs(board, x, y + 1); } return false; } bool isValid(vector<vector<char> > &board, int x, int y) { char c = board[x][y]; for (int col = 0; col < 9; ++col) { if (col != y && c == board[x][col]) return false; } for (int row = 0; row < 9; ++row) { if (row != x && c == board[row][y]) return false; } int topx = x/3*3,topy=y/3*3; for (int row = topx; row < topx+3; ++row) { for (int col = topy; col < topy+3; ++col) { if ((row != x || col != y) && c == board[row][col]) return false; } } return true; }};
0 0
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- Leetcode 37 Sudoku Solver
- [Leetcode] Sudoku Solver
- Leetcode 37: Sudoku solver
- LeetCode Sudoku Solver
- [LeetCode]Sudoku Solver
- Leetcode Sudoku Solver
- LeetCode-Sudoku Solver
- LeetCode - Sudoku Solver
- LeetCode:Sudoku Solver
- 【leetcode】Sudoku Solver
- Leetcode: Sudoku Solver
- leetcode Sudoku Solver
- LeetCode | Sudoku Solver
- LeetCode Sudoku Solver
- [Leetcode] Sudoku Solver (Java)
- DM8168外挂IC卡读写异常
- 深入理解JavaScript中的this关键字
- FZU 2103 Bin & Jing in wonderland
- 第二周项目4-用循环求
- 框架中的单例模式
- [Leetcode]Sudoku Solver
- Visual C++中 #include "stdafx.h" 头文件的用法
- 排序算法总结 java实现
- 并查集-poj-1611-The Suspectst
- 用信号量解决进程的同步与互斥
- android5.0中图片跳转的动画效果
- vs2010程序运行出错 link : fatal error lnk1123: 转换到 coff 期间失败: 文件无效或损坏
- 第二周项目5-用循环求.2
- 读书笔记-《统计思维 程序员数学之概率统计》