Sudoku Solver
来源:互联网 发布:教人做甜品的软件 编辑:程序博客网 时间:2024/05/23 01:30
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.
Solution:
class Solution {public: void solveSudoku(vector<vector<char>>& board) { memset(row, true, sizeof(row)); memset(col, true, sizeof(col)); memset(block, true, sizeof(block)); for(int i = 0; i < 9; ++i) for(int j = 0; j < 9; ++j) { if(board[i][j] != '.') { int k = board[i][j] - '0'; row[i][k] = false; col[j][k] = false; block[i/3*3+j/3][k] = false; } } dfs(board, 0); } bool dfs(vector<vector<char> >&board, int index) { if(index > 80) return true; int i = index / 9, j = index % 9; if(board[i][j] != '.') return dfs(board, index + 1); for(int k = 1; k <= 9; ++k) { if(row[i][k] && col[j][k] && block[i/3*3+j/3][k]) { board[i][j] = k + '0'; row[i][k] = false; col[j][k] = false; block[i/3*3+j/3][k] = false; if(dfs(board, index + 1)) return true; row[i][k] = true; col[j][k] = true; block[i/3*3+j/3][k] = true; } } board[i][j] = '.'; return false; }private: bool row[9][10]; bool col[9][10]; bool block[9][10];};
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
- UVALIVE 6958 I 求某TSP路径长度恰为定值+meet in middle + 折半
- HDU 5383 Yu-Gi-Oh! 过程中最小费用流
- Codeforces 557E dp+字典树+字典序第k大
- POJ 3630 解题报告
- hdu 1394 Minimum Inversion Number || ZOJ Monthly, January 2003 || 线段树 + 逆序数
- Sudoku Solver
- 第一次写博客...
- 开源分享 一(StickerCamera + 仿微信多图选择)
- POJ1066Treasure Hunt【判断直线相交】
- POJ 1056 解题报告
- 云主机环境搭建教程之搭建全能主机
- ubuntu 64位下安装 texlive2015 并设置 ctex 中文套装
- c语言指针基本使用
- Leetcode Unique Binary Search Trees II