LeetCode Sudoku Solver
来源:互联网 发布:雪球小小辛巴 知乎 编辑:程序博客网 时间:2024/06/07 11:02
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:bool check(vector<vector<char> > &board, int position) {int x = position / 9;int y = position % 9;for (int i = 0; i < 9; i++) if (i != x && board[i][y] == board[x][y]) return false; for (int j = 0; j < 9; j++) if (j != y && board[x][j] == board[x][y]) return false; for (int i = x / 3 * 3; i < (x / 3 + 1) * 3; i++) for (int j = y / 3 * 3; j < (y / 3 + 1) * 3; j++) if (i != x && j != y && board[i][j] == board[x][y]) return false; return true; }bool solve(vector<vector<char> > &board, int position) {if (position == 81)return true;int row = position / 9;int col = position % 9;if (board[row][col] == '.') {for (int i = 1; i <= 9; i++) {board[row][col] = i + '0';if (check(board, position) && solve(board, position+1))return true;board[row][col] = '.';}}else {if (solve(board, position + 1))return true;}return false;}void solveSudoku(vector<vector<char> > &board) {solve(board, 0);}};
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)
- Service的onStartCommand方法的疑问
- python操作xml文件--elementTree用法
- Unity3D Shader指南
- ArrayList和数组间的相互转换
- web.xml详细介绍
- LeetCode Sudoku Solver
- 用Java让内存溢出的方式
- shell--传入参数的处理
- ModelAndView详解
- extjs总结
- pkg-config
- C++构造函数中调用虚函数
- Mongoose源码分析之--简单的服务器搭建(C语言)
- 解决ADO通过指定IP地址连接到SQLServer数据库时总是失败问题