Sudoku Solver
来源:互联网 发布:自拍换装软件 编辑:程序博客网 时间:2024/05/21 10:52
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...
...and its solution numbers marked in red.
和valid sudoku差不多,每行、列、块不能有重复。DFS+回溯可以解决。
bool isValid(vector<vector<char> > &board, int x, int y){ int i, j; for (i = 0; i < 9; i++) if (i != x && board[i][y] == board[x][y]) return false; for (j = 0; j < 9; j++) if (j != y && board[x][j] == board[x][y]) return false; for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++) for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++) if (i != x && j != y && board[i][j] == board[x][y]) return false; return true;}bool solveSudoku(vector<vector<char> > &board){ for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j) { if (board[i][j]=='.') { for (int k = 1; k <= 9; ++k) { board[i][j] = '0' + k; if (isValid(board, i, j) && solveSudoku(board)) return true; board[i][j] = '.'; } return false; } } return true;}
0 0
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- 黑马程序员——C语言指针
- 每日算法之十一:Integer to Roman
- java安全套接层SSL示例
- DEX文件格式
- Leetcode OJ 刷题
- Sudoku Solver
- time模块学习
- LR杂记-用LoadRunner编写socket应用的测试脚本
- web安全学习笔记之-文件上传漏洞
- 正则表达式
- Scons入门1
- LR杂记 - loadrunner结果各种指标分析
- 【外包】如何和外包公司打交道以提高项目成功率(保质保量按时交付)
- servlet单实例多线程模式