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. mvc 母版页、用户自定义控件

    母版页(Master) 1.母版页是与Controller无关的,母版页只是一个View文件,而没有任何Controller与之相对应. 2.其实在ASP.NET MVC中View的aspx与母版页并 ...

  2. nefu 446 今年暑假不AC(贪心)

    Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会 ...

  3. php-fpm参数优化【转】

    转自 php-fpm参数优化 | Linux运维笔记https://blog.linuxeye.com/380.html php-fpm进程设置多少合适,设成动态还是静态? <lnmp一键安装包 ...

  4. 设置ubuntu 默认不启动图形界面

    设置ubuntu 默认不启动图形界面 一. 编辑文件/etc/X11/default-display-manager 如果值为/usr/sbin/gdm,则进入图形界面如果值为false,则进入控制台 ...

  5. Apache 的常见问题

    Apache "No services installed"问题的处理以及Apache提示 the requested operation has failed而无法启动 安装完 ...

  6. Learning Java IO indexes

    I/O Streams, it simplifies I/O operations, write a whole object out to stream & read back. File ...

  7. android 去掉listview之间的黑线

    方法1:listView.setDividerHeight(0);方法2:this.getListView().setDivider(null);方法3:android:divider="@ ...

  8. PAT (Advanced Level) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  9. 开源企业管理软件 ONES

    ONES 不是 ONS,基于AngularJS + ThnkPHP开发的企业管理系统平台,名字可以理解为ONES is a Niubility ERP System 或者 ONES Notonly a ...

  10. MySQL 5.7 for Windows 解压缩版配置安装

    从MYSQL5.7.6开始,安装MYSQL提示“请键入 NET HELPMSG 3534 以获得更多的帮助”的解决办法 今天安装MySQL提示如下错误: ----------------------- ...