Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24108   Accepted: 11259   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

 
 
 
题意:数独游戏,   规则------->>>>>    1.每行每列都包含1到9,且数字不重复。    每个3*3的小矩阵中也包含数字1到9,数字不重复。
 
 
思路:  做标记,    3个标记,分别行,列和小矩阵,                              数字1到9,用过标记,true,否则   false。
 
ac代码:
 
  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
const int MAX=15;
int map[MAX][MAX];
char temp[MAX];
bool row[MAX][MAX],line[MAX][MAX],rl[MAX][MAX],flag=false;
void dfs(int x,int y)
{
if (x==10){
flag=true;
return ;
}
if (map[x][y]){
if (y==9)
dfs(x+1,1);
else
dfs(x,y+1);
if (flag)
return ;
}
else{
int k=3*((x-1)/3)+(y-1)/3+1;
for (int i=1;i<10;i++){
if (!row[x][i]&&!line[y][i]&&!rl[k][i]){
map[x][y]=i;
row[x][i]=line[y][i]=rl[k][i]=true;
if (y==9)
dfs(x+1,1);
else
dfs(x,y+1);
if(flag)
return ;
map[x][y]=0;
row[x][i]=line[y][i]=rl[k][i]=false;
}
}
}
}
int main()
{
int t;
cin>>t;
while (t--){
flag=false;
memset(map,0,sizeof(map));
memset(row,false,sizeof(row));
memset(line,false,sizeof(line));
memset(rl,false,sizeof(rl));
for (int i=1;i<10;i++){
cin>>temp+1;
for (int j=1;j<10;j++){
map[i][j]=temp[j]-'0';
if (map[i][j]){
int k=3*((i-1)/3)+(j-1)/3+1;
row[i][map[i][j]]=line[j][map[i][j]]=rl[k][map[i][j]]=true;
}
}
}
dfs(1,1);
for (int i=1;i<10;i++){
for (int j=1;j<10;j++){
printf("%d",map[i][j]);
}
printf("\n");
}
}
return 0;
}

poj2676 (dfs+回溯)的更多相关文章

  1. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  2. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. February 13 2017 Week 7 Monday

    Don't trouble trouble until trouble troubles you. 不要自寻烦恼. It is the best way to live an easy life if ...

  2. ue-edit设置显示函数列表

    UltraEdit的函数列表竟然不显示函数,那这功能要它何用,应该如何才能让函数显示出来呢? 公司编程基本上都在UltraEdit中进行,俺刚来公司还不熟悉,今天装了个UltraEdit,可是看着别人 ...

  3. 017random模块

    import  randomprint(random.random())print(random.randint(1,8))            #包括8         print(random. ...

  4. Python函数(入门6)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407970.html 本文出自:[Edwin博客园] Python函数 1. Python之调用函数 pri ...

  5. WMIC_2

  6. 华为交换机SSH配置

    设备:S5700 一.在本地设备服务端生成秘钥对 [Huawei]rsa local-key-pair create 二.配置VTY [Huawei]user-interface vty 0 4进入虚 ...

  7. vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证)

    vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证): html: <el-form-item label="活动时间" required> & ...

  8. php编程零基础如何快速入门。门头沟编程

    昨天遇到一个人,说知道thinktphp,不过几年前的事了. 我先跟他讲了下,xyhcms后台功能,各个版块,以及数据库都介绍了一下. 跟他说了一个功能现场实现,说了实现方法. 然后上机操作,发现他表 ...

  9. javascript入门教程 (1)

    对于刚刚接触前端开发或者刚开始学习javascript的同学来说,js能用来做些什么,它是如何诞生的,它的组成结构是怎么的,在这些问题上可能都只有一些模糊的概念, js的入门篇 就是希望可以从0开始深 ...

  10. 大数据框架-HDFS

    HDFS:分布式文件系统,运行文件通过网络在多台主机分享的文件系统,分块写入(128M),适用于一次写入多次查询,不支持并发写(只能一块一块写),小文件不合适. nameNode(主节点,单个): 保 ...