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. 用caffe给图像的混乱程度打分

    Caffe应该是目前深度学习领域应用最广泛的几大框架之一了,尤其是视觉领域.绝大多数用Caffe的人,应该用的都是基于分类的网络,但有的时候也许会有基于回归的视觉应用的需要,查了一下Caffe官网,还 ...

  2. win10快捷键大全

    win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...

  3. $scope

    $scope.aaa = 3; $scope.bbb = 4; $scope.aaa = $scope.bbb; //这只是简单的赋值 $scope.bbb = 5; 输出 $scope.aaa为4 ...

  4. js监听键盘方向键事件

    <SCRIPT language=javascript> document.onkeydown = chang_page; function chang_page() { || ) loc ...

  5. Parade

    Parade time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  6. runtime基础知识

    看到一篇不错的runtime方面博客: 引言 相信很多同学都听过运行时,但是我相信还是有很多同学不了解什么是运行时,到底在项目开发中怎么用?什么时候适合使用?想想我们的项目中,到底在哪里使用过运行时呢 ...

  7. php 环信 接口的例子

    <?php class Hxcall{ private $app_key = 'yunjiankang#medical'; private $client_id = 'YXA6ARjBgDnxE ...

  8. jquery获得select的文本

    本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...

  9. Cloudsim 3.0在myclipse下的安装过程

    (1)下载cloudsim 3.0: http://code.google.com/p/cloudsim/downloads/list (2)下载flanaga.jar包  下载地址:http://w ...

  10. android 数据存储分配的一些事

    应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还 ...