poj2676 Sudoku
来源:互联网 发布:形容傻天真网络词 编辑:程序博客网 时间:2024/04/29 23:31
Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14100 Accepted: 6961 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
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127
题意:数独:玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复。
思路: 爆搜,枚举,设置一个二维数组x[i][k]表示第i行有木有出现过数字k,y[j][k]表示第j列有木有出现过数字k,xg[(i/3)*3+j/3][k]表示第(i/3)*3+j/3个宫格有木有出现过 数字k;
#include<iostream>
#include<cstdio>
#include<cstdlib>#include<string>#include<cstring>#include<list>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#include<cmath>#include<bitset>#include<climits>#define mem(a) memset(a,0,sizeof(a))#define MAXN 100000using namespace std;char vw[9][9];int x[9][9],y[9][9],xg[9][9];bool vis;int flag=0;void dfs(int a,int b){ int u=a*9+b+1;//这个公式对应下面的<span style="font-family: arial, 宋体, sans-serif;"> dfs(u/9,u%9),很精巧,自己想想为啥</span> if(a==9) { vis=true;//填好就标记直接返回 for(int i=0; i<9; i++) { for(int j=0; j<9; j++) printf("%d",vw[i][j]+1); printf("\n"); } } if(vis)<span style="font-family: arial, 宋体, sans-serif;">//填好就直接返回</span> return ; if(vw[a][b]!=-1) { dfs(u/9,u%9); return ; } for(int i=0; i<9&&!vis; i++) { if(!x[a][i]&&!y[b][i]&&!xg[(a/3)*3+b/3][i]) { x[a][i]=y[b][i]=xg[(a/3)*3+b/3][i]=1; vw[a][b]=i; dfs(u/9,u%9); x[a][i]=y[b][i]=xg[(a/3)*3+b/3][i]=0; vw[a][b]=-1; } }}int main(){ int i,j,k,t; char ch; cin>>t; while(t--) { mem(x); mem(y); mem(xg); mem(vw); for(i=0; i<9; i++) for(j=0; j<9; j++) { cin>>ch; k=vw[i][j]=ch-'1'; //printf("%d %d",k,vw[i][j]); if(ch-'0') { x[i][k]=y[j][k]=xg[(i/3)*3+j/3][k]=1; } } vis=false;//标记填没填好 dfs(0,0); } return 0;}
1 0
- poj2676(Sudoku)
- poj2676 - Sudoku
- POJ2676--Sudoku
- poj2676 Sudoku
- poj2676 Sudoku
- POJ2676 Sudoku
- POJ2676 Sudoku
- poj2676 Sudoku
- poj2676 Sudoku
- POJ2676-Sudoku
- poj2676 Sudoku 数独
- 北大ACM poj2676 Sudoku
- POJ2676:Sudoku(DFS)
- POJ2676 Sudoku [数独]
- POJ2676:Sudoku(DFS)
- POJ2676 Sudoku DFS
- 回溯-POJ2676-Sudoku
- POJ2676 2918 Sudoku 暴搜
- try catch finally 用法
- tomcat下jsp乱码原因(下)
- MyThread线程基本训练
- Machine Learning机器学习课堂笔记4(代价函数)
- DownLoadCall
- poj2676 Sudoku
- 以下是一个简单的多线程爬虫实现
- cisco 交换机设置时区、时间、同步日志本地时间等操作
- 论文阅读_基于CNN的图像二值化_Robust Binarization for Video Text Recognition
- Visual Basic 中判断指定名称的进程是否存在
- 中医文化的管理学启示
- ubuntu 下svn客户端使用
- Visual Basic 判断一个被Shell的程序进程是否结束
- 如何区分Oracle的数据库,实例,服务名,SID