POJ Sudoku 数独填数(深搜)
来源:互联网 发布:fugue免流源码 编辑:程序博客网 时间:2024/05/17 07:00
题目:http://poj.org/problem?id=2676
Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
Input
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
Sample Input
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127
思路:见代码:
#include <iostream>#include <cstring>#include <fstream>#include <cstdio>using namespace std;int sudoku[9][9];bool square[9][10]; // 标记每个小方格出现的数字 bool checkrow[9][10]; //标记没行出现的数字 bool checkcol[9][10]; //标记没列出现的数字 bool isdone ; void dfs(int i, int j){if(i == 9) {isdone = true;for(int i=0; i<9; i++){for(int j=0; j<9; j++)cout<<sudoku[i][j];cout<<endl;}return ;}if(isdone) return ; if(sudoku[i][j]){if(j == 8) dfs(i+1,0);else dfs(i,j+1);}else{for(int num=1; num<10; num++){int k = 3*(i/3)+j/3; // 该点位于哪个小方格 if(!checkrow[i][num] && !checkcol[j][num] && !square[k][num]){sudoku[i][j] = num;checkrow[i][num] = 1; checkcol[j][num] = 1;square[k][num] = 1; if( j == 8) dfs(i+1,0);else dfs(i,j+1);sudoku[i][j] = 0;checkrow[i][num] = 0; checkcol[j][num] = 0;square[k][num] = 0; }}}}int main(){int t;//ifstream fin;//fin.open("input.txt");//cout<<fin.is_open()<<endl;cin>>t;while(t--){memset(square,0,sizeof(square));memset(checkcol,0,sizeof(checkcol));memset(checkrow,0,sizeof(checkrow));for(int i=0; i<9; i++){char tmp[9];cin>>tmp;for(int j=0; j<9; j++){sudoku[i][j] = tmp[j]-'0';if(sudoku[i][j]){int k = 3*(i/3)+j/3;checkrow[i][sudoku[i][j]] = 1;checkcol[j][sudoku[i][j]] = 1;square[k][sudoku[i][j]] = 1; }}}isdone = false; dfs(0,0);}return 0;}
- POJ Sudoku 数独填数(深搜)
- POJ 2676 Sudoku 深搜
- poj 2676 Sudoku (dfs)
- POJ 2676-Sudoku(DFS)
- POJ 2676 Sudoku(dfs)
- POJ 2676 Sudoku(DFS)
- POJ 2676 Sudoku (DFS)
- poj 2676 Sudoku (dfs)
- poj Sudoku
- Sudoku POJ
- POJ 2676 Sudoku(数独)__深搜
- POJ 3074 Sudoku (Dancing Links)
- POJ 2676 Sudoku (数独 搜索)
- POJ 2676Sudoku(数独)
- poj 3704 Sudoku(Dancing Links)
- poj 3076 Sudoku(Dancing Links)
- poj 2676 Sudoku (基础DFS)
- poj 2676 Sudoku(数独)
- 移动电子工单、维修工单解决方案
- VS2010下访问oracle数据库的OCCI配置
- 如何自己编译wireless tool
- 进程间通信
- JavaSwing图形界面编程之应用(二)
- POJ Sudoku 数独填数(深搜)
- poj2234 Matches Game
- Unity3d脚本入门
- hdu 4462 Scaring the Birds 状压枚举 (或dfs)
- 颜色对话框
- OCP-1Z0-052-V8.02-18题
- 特征向量与特征值
- JavaSwing图形界面编程之JTable
- Basic Spring Step by Step Hello World Example