POJ 2676 Sudoku
2015-07-28 19:56
429 查看
F - Sudoku
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
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.
![](http://7xjob4.com1.z0.glb.clouddn.com/56e7e6182093bbca488d6c73ba833af6)
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
Sample Output
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
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
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[20][20],b[20][20],c[5][5][20],m[20][20],h;//a,b,c数组分别用来判断行、列、九宫数字互异性 int ex(int x){ if (x%3==0) return x/3; else return x/3+1; } void dfs(int x,int y){//以坐标为参数搜索 int i,j,nx=x,ny=y; if (h) return; if (x>9){ h=1; for (i=1;i<=9;i++){ for (j=1;j<=9;j++) cout<<m[i][j]; cout<<endl; } } else { ny++;if (ny>9) {ny=1;nx++;} if (m[x][y]!=0) dfs(nx,ny); else { int xx=ex(x),yy=ex(y); for (i=1;i<=9;i++){ if (!a[x][i] && !b[y][i] && !c[xx][yy][i]){ m[x][y]=i; a[x][i]++;b[y][i]++;c[xx][yy][i]++; dfs(nx,ny); a[x][i]--;b[y][i]--;c[xx][yy][i]--; m[x][y]=0; if (h) return; } } } } } int main() { char ch; int t; cin>>t; while (t--){ int i,j,x,y; h=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for (i=1;i<=9;i++){ for (j=1;j<=9;j++) { cin>>ch; m[i][j]=ch-'0'; x=ex(i);y=ex(j); a[i][ch-'0']++; b[j][ch-'0']++; c[x][y][ch-'0']++; } } dfs(1,1); } return 0; }
相关文章推荐
- [转] Oracle analyze table 使用总结
- hdu 1506/poj 2559 Largest Rectangle in a Histogram
- The 3n + 1 problem(POJ--1207
- ubuntu常用软件大全
- UVA - 1374 Power Calcul
- Android官网中关于service 介绍的重要记录笔记
- Java集合排序及java集合类详解--(Collection, List, Set, Map)
- Java集合排序及java集合类详解--(Collection, List, Set, Map)
- 暑假集训第三周 STL D - 排列
- CentOS 最小化安装后初始化网络
- POJ 1784 Charlie's Change 记录路径的多重背包
- Python学习笔记-协程
- 1~9个数字组成的前i个数能被i组成
- MySQL导入.sql文件及常用命令
- [转] Oracle analyze 命令分析
- 暴力求解——最大乘积 Maximum Product,UVa 11059
- 【整理】WDK 和 DDK异同
- iOS开发实现页面间的数据传递
- zookeeper配置
- Bundle saveInstanceState的作用与重写onSaveInstanceState方法