Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18105   Accepted: 8772   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 暑假艾教教的数独,留个板子。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int sudoku[][];
int column[][];
int row[][];
int nine[][];
struct node
{
int x,y;
};
node ready[];
int T;
int num = ;
char s[][];
int cal(int x,int y)
{
return (x-)/*+(y-)/+;
}
bool cmp(node A,node B)
{
return A.y>B.y;
}
void put(int x,int y,int tt,int flag)
{
if(flag) sudoku[x][y] = tt;
row[x][tt] = flag;
column[y][tt] = flag;
nine[cal(x,y)][tt] = flag;
}
int ok = ;
int can(int x,int y,int tt)
{
if(row[x][tt]) return ;
if(column[y][tt]) return ;
if(nine[cal(x,y)][tt]) return ;
return ;
}
void dfs(int k)
{
if(ok) return;
if(k==num+)
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(j==) printf("%d\n",sudoku[i][j]);
else printf("%d",sudoku[i][j]);
}
}
ok = ;
return;
}
for(int nex=;nex<=;nex++)
{
if(can(ready[k].x,ready[k].y,nex))
{
put(ready[k].x,ready[k].y,nex,);
dfs(k+);
put(ready[k].x,ready[k].y,nex,);
}
}
}
int main()
{
cin>>T;
while(T--)
{
ok = ;
memset(sudoku,,sizeof(sudoku));
memset(ready,,sizeof(ready));
memset(column,,sizeof(column));
memset(row,,sizeof(row));
memset(nine,,sizeof(nine));
num = ;
for(int i=;i<=;i++)
{
scanf("%s",s[i]+);
for(int j=;j<=;j++)
{
if(s[i][j]=='')
{
num++;
ready[num].x = i;
ready[num].y = j;
}
else
{
put(i,j,s[i][j]-'',);
}
}
}
sort(ready+,ready+num+,cmp);
dfs();
}
return ;
}

 

POJ Sudoku 数独填数 DFS的更多相关文章

  1. 第七届C/C++B-方格填数 DFS

    方格填数 如下的10个格子    +--+--+--+    |  |  |  | +--+--+--+--+ |  |  |  |  | +--+--+--+--+ |  |  |  | +--+- ...

  2. 第七届 蓝桥杯 方格填数 dfs

    如下的10个格子  填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数. 注意:你提交的应该是一个整数,不要填写任何 ...

  3. 梦工厂实验室 蛇形填数 dfs

    问题 D: 蛇形填数 时间限制: 3 Sec  内存限制: 64 MB提交: 28  解决: 5[提交][状态][讨论版] 题目描述 在n*n方阵里填入1,2,...,n*n,要求填成蛇形.例如n=4 ...

  4. 蓝桥杯 方格填数 DFS 全排列 next_permutation用法

    如下的10个格子(参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数.注意:你提交的应该是一个 ...

  5. POJ 2676:Sudoku 数独

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15830   Accepted: 7737   Special ...

  6. DFS(深度优先搜索遍历求合格条件总数)--07--DFS--蓝桥杯方格填数

    此题方法多种,我用规范的DFS来求解 题目:方格填数 如下的10个格子,填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻)一共有多少种可能的填数方案?   输出 请填写表示 ...

  7. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...

  8. java实现第三届蓝桥杯方块填数

    方块填数 "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6的小格被分为6个部 ...

  9. nyoj 33 蛇形填数

    蛇形填数 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 ...

随机推荐

  1. A - 娜娜梦游仙境系列——诡异的钢琴

    A - 娜娜梦游仙境系列——诡异的钢琴 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...

  2. jquery checkbox 操作

    1.jquery 获取所有选中和未选中的checkbox 未选中 var unCheckedBoxs = $("input[name='myCheckbox']").not(&qu ...

  3. js判断当前时间前几天和格式校验

    addday天后的日期 function time(addday) { var now= new Date(); nowdate=now.getDate(); //alert(nowdate); no ...

  4. 如果更新包更新包现场,class文件更新过去,没有改变,及时删掉,照样能进那个模块的问题。

    这是打更新包需要注意的问题: 带$的同名文件也需要copy过来打更新包,不能只更新一个class文件,找了1天的错误,简直日乐购.

  5. Win32 SDK Combo Box

    如下图所示,显示了三种不同风格的Combo Box样式.当然,现在这样看不出第一种与第三种之间的区别,但是第二种与其他两种的区别是明显的,第二种的列表框始终是出于现实状态的. Combo Box: 一 ...

  6. LightOJ 1259 Goldbach`s Conjecture 素数打表

    题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...

  7. UIView的layoutSubviews,initWithFrame,initWithCoder方法

    ****************************layoutSubviews************************************ layoutSubviews是UIView ...

  8. Factory and AbstractFactory ——抽象与具体的分离

    Factory and AbstractFactory——抽象与具体的分离 面向对象标准关注于抽取一系列事物的共同行为,组建一个基类.行为再划分成两类: 1:现在及以后不太可能会变化的行为. 2:以后 ...

  9. HDU - 2502 Subway

    题目链接:http://poj.org/problem?id=2502 分析: 告诉一些地铁线路,从起点到终点,中途可以步行,可以坐地铁,找一条最短的路 主要是把图建立好,然后直接dijkstra或者 ...

  10. 用Visual Studio 2015 编写驱动之前一定要注意的问题!!!

    如果你确定要使用Visual Studio 2015 编写驱动,那么在你安装Visual Studio 2015 和WDK之前,一定一定要注意一件事情,那就是确保SDK和WDK版本保持一致,切记切记! ...