Sudoku
2015-08-12 20:44
267 查看
Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15952 Accepted: 7791 Special Judge
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
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
Source
Southeastern Europe 2005
这个题要好好反思一下,因为我将题的思路想了一半,感觉有点不太可行,就放弃了这个方案,找的题解,以后要对自己有点信心,勇敢去敲
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15952 Accepted: 7791 Special Judge
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
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
Source
Southeastern Europe 2005
这个题要好好反思一下,因为我将题的思路想了一半,感觉有点不太可行,就放弃了这个方案,找的题解,以后要对自己有点信心,勇敢去敲
#include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int INF =0x3f3f3f3f; const int Max =120; char str[11][11]; bool a[11][11]; bool b[11][11]; bool c[11][11]; int Map[11][11]; bool DFS(int x,int y) { if(x==9) { return true; } bool flag=false; if(Map[x][y]) { if(y==8) { flag=DFS(x+1,0); } else { flag=DFS(x,y+1); } if(flag) { return true; } else { return false; } } else { int k=3*(x/3)+y/3; for(int i=1; i<=9; i++) { if(!a[x][i]&&!b[y][i]&&!c[k][i]) { Map[x][y]=i; a[x][i]=true; b[y][i]=true; c[k][i]=true; if(y==8) { flag=DFS(x+1,0); } else { flag=DFS(x,y+1); } if(!flag) { Map[x][y]=0; a[x][i]=false; b[y][i]=false; c[k][i]=false; } else { return true; } } } } return false; } int main() { int T; scanf("%d",&T); while(T--) { for(int i=0; i<9; i++) { scanf("%s",str[i]); } memset(a,false,sizeof(a)); memset(b,false,sizeof(b)); memset(c,false,sizeof(c)); memset(Map,0,sizeof(Map)); for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { Map[i][j]=str[i][j]-'0'; if(Map[i][j]) { a[i][Map[i][j]]=true; b[j][Map[i][j]]=true; int k=3*(i/3)+j/3; c[k][Map[i][j]]=true; } } } DFS(0,0); for(int i=0; i<9; i++) { for(int j=0; j<9; j++) cout<<Map[i][j]; cout<<endl; } } return 0; }
相关文章推荐
- 用proc实时管理 Linux
- vc++6对windows SEH扩展分析 一文拾遗
- ORM进阶之Hibernate 简介及框架搭
- Python+NLTK自然语言处理学习(一):环境搭建
- HDU5371——Manacher+set维护——Hotaru's problem
- Sudoku
- cloudstack delete zone
- Java日志框架——Logback的体系结构
- 蓝桥杯-基础练习-大数算法
- 小知识点 - 串口输出数
- ScrollView
- 搜索引擎核心读书心得2:暗网抓取
- Windows,C/C++_字符串转换_MultiByteToWideChar和WideCharToMultiByte的正确使用方法及参数详解
- PHP之面向对象学习
- java异或运算交换两个数的陷阱
- 生活小常识
- 如何编写NopCommerce插件(一)建立一个MVC的项目
- 三星HTC价格跳水 安卓旗舰会否崩塌?
- bell数
- theme style 闪屏