搜索函数 数独
2015-07-11 17:00
387 查看
B - Tudoku
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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
Sample Output
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int Smap[10][10];
bool row[10][10];
bool col[10][10];
bool grid[10][10];
bool DFS(int x,int y){
if(x==10) return true;
bool flag=false;
if(Smap[x][y]){
if(y==9){
flag=DFS(x+1,1);
}else{
flag=DFS(x,y+1);
}
if(flag){
return true;
}else{
return false;
}
}
else{
int k=3*((x-1)/3)+(y-1)/3+1;
for(int i=1;i<=9;i++){
if(!row[x][i]&&!col[y][i]&&!grid[k][i]){
Smap[x][y]=i;
row[x][i]=true;
col[y][i]=true;
grid[k][i]=true;
if(y==9){
flag=DFS(x+1,1);
}else{
flag=DFS(x,y+1);
}
if(flag){
return true;
}
else{
Smap[x][y]=0;
row[x][i]=false;
col[y][i]=false;
grid[k][i]=false;
}
}
}
}
return false;
}
int main(){
char MAP[10][10];
int T,k=0,z;
cin>>T;
z=T;
while(T--){
k++;
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++) {
cin>>MAP[i][j];
Smap[i][j]=MAP[i][j]-'0';
if(Smap[i][j]){
int k=3*((i-1)/3)+(j-1)/3+1;
row[i][ Smap[i][j] ]=true;
col[j][ Smap[i][j] ]=true;
grid[k][ Smap[i][j] ]=true;
}
}
getchar();
getchar();
DFS(1,1);
if(k>1)cout<<endl;
cout<<"Scenario #"<<k<<":"<<endl;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++) {
cout<<Smap[i][j];
}
cout<<endl;
}
}
return 0;
}
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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.
7 | 5 | 3 | 2 | 8 | 4 | 6 | 9 | 1 |
4 | 8 | 2 | 9 | 1 | 6 | 5 | 3 | 7 |
1 | 9 | 6 | 7 | 5 | 3 | 8 | 4 | 2 |
9 | 3 | 1 | 6 | 4 | 2 | 5 | ||
2 | 7 | 5 | 4 | 9 | 1 | 3 | 8 | 6 |
6 | 4 | 8 | 3 | 2 | 1 | 7 | 9 | |
5 | 6 | 7 | 3 | 4 | 9 | 2 | 1 | 8 |
8 | 2 | 4 | 1 | 7 | 5 | 9 | 6 | 3 |
3 | 1 | 9 | 6 | 2 | 8 | 7 | 5 | 4 |
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
2 000000000 817965430 652743190 175439820 308102950 294856370 581697240 903504610 746321580 781654392 962837154 543219786 439182675 158976423 627543918 316728549 895461237 274395861
Sample Output
Scenario #1: 439218765 817965432 652743198 175439826 368172954 294856371 581697243 923584617 746321589 Scenario #2: 781654392 962837154 543219786 439182675 158976423 627543918 316728549 895461237 274395861
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int Smap[10][10];
bool row[10][10];
bool col[10][10];
bool grid[10][10];
bool DFS(int x,int y){
if(x==10) return true;
bool flag=false;
if(Smap[x][y]){
if(y==9){
flag=DFS(x+1,1);
}else{
flag=DFS(x,y+1);
}
if(flag){
return true;
}else{
return false;
}
}
else{
int k=3*((x-1)/3)+(y-1)/3+1;
for(int i=1;i<=9;i++){
if(!row[x][i]&&!col[y][i]&&!grid[k][i]){
Smap[x][y]=i;
row[x][i]=true;
col[y][i]=true;
grid[k][i]=true;
if(y==9){
flag=DFS(x+1,1);
}else{
flag=DFS(x,y+1);
}
if(flag){
return true;
}
else{
Smap[x][y]=0;
row[x][i]=false;
col[y][i]=false;
grid[k][i]=false;
}
}
}
}
return false;
}
int main(){
char MAP[10][10];
int T,k=0,z;
cin>>T;
z=T;
while(T--){
k++;
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++) {
cin>>MAP[i][j];
Smap[i][j]=MAP[i][j]-'0';
if(Smap[i][j]){
int k=3*((i-1)/3)+(j-1)/3+1;
row[i][ Smap[i][j] ]=true;
col[j][ Smap[i][j] ]=true;
grid[k][ Smap[i][j] ]=true;
}
}
getchar();
getchar();
DFS(1,1);
if(k>1)cout<<endl;
cout<<"Scenario #"<<k<<":"<<endl;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++) {
cout<<Smap[i][j];
}
cout<<endl;
}
}
return 0;
}
相关文章推荐
- php扩展库
- [转载]Android开发必备的21个免费资源和工具
- [LeetCode] Populating Next Right Pointers in Each Node
- 两个字符串合并、排序和转换
- Android控件——TextView、EditText、ImageView、Button、ImageButton
- 二叉树遍历(图解)
- [Linux] - CentOS IP设置方法
- login incorrect
- GCD 综合使用示例
- 【Hadoop系列】第一章:Hadoop生态系统概述以及版本演化
- PHP 文件创建/写入
- iOS中对时间的处理(二)——获取时间及星期
- Android反编译工具Apktool使用
- 数据结构基础 图的遍历(二) 之 BFS
- 深入理解Spring Redis的使用 (一)、Spring Redis基本使用
- shell中的点命令与source命令
- openwrt之vim
- 数据库中的一部分运算符
- 谈谈Linux应用程序 ABI兼容性
- Matlab spline