POJ 2918 Tudoku 【DFS】
来源:互联网 发布:今日热点是什么软件 编辑:程序博客网 时间:2024/06/08 16:42
Description
Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at our university and teaches newcomers like Jim. In the first lesson he explained the game of Tudoku to Jim. Tudoku is a straight-forward variant of Sudoku, because it consists of a board where almost all the numbers are already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of being too lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom left for him.
Sudoku is played on a 9 × 9 board that is divided into nine different 3 × 3 blocks. Initially, it contains only a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 × 3 block contains every number from 1 to 9. This can be quite hard but remember that Tom already filled most cells. A resulting Tudoku board can be solved using the following rule repeatedly: if some row, column or 3 × 3 block contains exactly eight numbers, fill in the remaining one.
In the following example, three cells are still missing. The upper left one cannot be determined directly because neither in its row, column, or block, there are eight numbers present. The missing number for the right cell can be determined using the above rule, however, because its column contains exactly eight numbers. Similarly, the number for the lower-most free cell can be determined by examining its row. Finally, the last free cell can be filled by either looking at its row, column or block.
Input
The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digits each. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenario is terminated by an empty line.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for the input, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with a blank line.
Sample Input
2000000000817965430652743190175439820308102950294856370581697240903504610746321580781654392962837154543219786439182675158976423627543918316728549895461237274395861
Sample Output
Scenario #1:439218765817965432652743198175439826368172954294856371581697243923584617746321589Scenario #2:781654392962837154543219786439182675158976423627543918316728549895461237274395861
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))const int M=1e2+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;int i,j,k,n,m;int mp[M][M];int ans[M][M];bool flag;bool ok(int n,int cur){ int r=n/9; int c=n%9; for(int j=0;j<9;j++) if(mp[r][j]==cur) return false; for(int i=0;i<9;i++) if(mp[i][c]==cur) return false; int x=r/3*3; int y=c/3*3; for(int i=x;i<x+3;i++) for(int j=y;j<y+3;j++){ if(mp[i][j]==cur) return false; } return true;}void dfs(int n){ if(n>80){ flag=1; return ; } if(mp[n/9][n%9]){ dfs(n+1); if(flag)return; } else { for(int i=1;i<=9;i++){ if(ok(n,i)){ mp[n/9][n%9]=i; dfs(n+1); if(flag)return; mp[n/9][n%9]=0; } } }}int main(){ int T; int cas=1; scanf("%d",&T); while(T--){ for(int i=0;i<9;i++){ char s[100]; scanf("%s",s); for(int j=0;j<9;j++) mp[i][j]=s[j]-'0'; } flag=0; dfs(0); printf("Scenario #%d:\n",cas++); for(int i=0;i<9;i++) for(int j=0;j<9;j++){ printf("%d",mp[i][j]); if(j==8)printf("\n"); } printf("\n"); } return 0;}
- POJ 2918 Tudoku 【DFS】
- poj 2918 Tudoku 数独dfs
- poj 2918 Tudoku
- poj 2918Tudoku(暴力。。)
- poj 2918 Tudoku
- POJ 2918 Tudoku [搜索]
- POJ 2918 Tudoku
- POJ 2918 Tudoku 笔记
- poj2918 Tudoku---dfs
- Tudoku
- POJ 2676 && POJ 2918 数独 DFS
- DFS POJ
- POJ DFS
- POJ 2676/2918 数独(dfs)
- fjnu 1937 Tudoku
- poj 3083 DFS+DFS+BFS
- POJ 1753 ID+DFS
- poj 1950 数字dfs
- Past duration 0.666664 too large
- mongoTemplate聚合操作
- DNS协议详解及报文格式分析
- DataTables warning (table id = 'IDTableName'): Cannot reinitialise DataTable
- yii2.0高级框架配置时打开init.bat秒退的解决方法
- POJ 2918 Tudoku 【DFS】
- gitingnore忽略提交文件配置
- JSTL标签
- python通过testrpc访问以太坊数据入门
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
- c++输入函数小结
- 牛客网-华为在线编程-009-提取不重复的整数(字符串)
- Thread源码分析和小结
- 案例 | 巴别鸟为弘睿构建企业知识库