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

搜索时要对行,列,每个小数独阵进行判断,判断该数字是否在这三个中出现过,是否出现用标识符标记,处理时i,j从下标0开始,处理行标,列标比较方便。思路较简单,看代码吧。

 #include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 12
using namespace std;
int num[MAXN][MAXN];
int row[MAXN][MAXN];//用来标记每一行九个数是否已被使用,使用记为1
int col[MAXN][MAXN];//列
int mar[MAXN][MAXN];//用0-8标识9个小数独矩阵
char chr[MAXN][MAXN];
bool flag=false;
void dfs(int m,int n)
{
int s,i,j; if(m==&&n==)
{
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
cout<<num[i][j];
}
cout<<endl;
}
flag=true;
}
else
{
if(num[m][n]!=)
{
if(n<=)
dfs(m,n+);
else
dfs(m+,);
}
else
{
for(i=;i<=;i++)
{
if(row[m][i]==&&col[n][i]==&&mar[m/*+n/][i]==)//根据i,j标定它所属的行,列,和所在的小矩阵
{
s=m/*+n/;
num[m][n]=i;
row[m][i]=;
col[n][i]=;
mar[s][i]=;
if(n<=)
dfs(m,n+);
else
dfs(m+,);
if(flag)
return ;
num[m][n]=;//一旦回溯过来,必须重新置0,因为说明此数字对于后面的搜索产生了不满足,故将其置0.
row[m][i]=;
col[n][i]=;
mar[s][i]=;
}
}
}
}
}
int main()
{
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int i,j,s,t;
cin>>t;
while(t--)
{
memset(row,,sizeof(row));
memset(col,,sizeof(col));
memset(mar,,sizeof(mar));
for(i=;i<;i++)
scanf("%s",chr[i]);
for(i=;i<;i++)
for(j=;j<;j++)
{
num[i][j]=chr[i][j]-'';
s=num[i][j];
if(s!=)
{
row[i][s]=;
col[j][s]=;
mar[i/*+j/][s]=;//初始,一旦有数字,则要将它所在的行列及小矩阵标为1
}
}
flag=false;
dfs(,);
}
return ;
}

蓝桥杯校内选拔赛/POJ 数独(深搜)的更多相关文章

  1. 2013 蓝桥杯校内选拔赛 java本科B组(题目+答案)

    一.标题:正则表示     正则表达式表示了串的某种规则或规律.恰当地使用正则表达式,可以使得代码简洁.事半功倍.java的很多API都支持正则表达式作为参数.其中的String.split就是这样. ...

  2. 记 2019蓝桥杯校内预选赛(JAVA组) 赛后总结

    引言 好像博客好久没更新了 哈哈哈哈哈 趁现在有空更新一波 不知道还有没有人看 确实该记录一下每天做了什么了 不然感觉有些浑浑噩噩了 比赛介绍 全称: 蓝桥杯全国软件和信息技术专业人才大赛 蓝桥杯 实 ...

  3. POJ 1190(深搜)

    http://poj.org/problem?id=1190 又有好久没做搜索的题了,没想到做一个卡了我那么久,想哭啊. 一个中文题,思路呢也就是搜索呗,一层一层往上面搜,不过这里有两个比较重要的地方 ...

  4. [NOIP2009]靶形数独 深搜+枝杈优化

    这道题,又是一位玄学搜索...... 我是用的蜗牛序搜的(顾名思义,@,这么搜),我正着搜80然后一反转比原来快了几十倍........一下AC....... 我的思路是这样的话我们可以从内到外或者从 ...

  5. 第五届蓝桥杯校内选拔第六题_(dfs)

    你一定听说过“数独”游戏.如[图1.png],玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独的答案都是唯一的,所以 ...

  6. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  7. POJ 2531 深搜剪枝

    题意:全局最大割. 分析:有相应的算法,数据量很小,可以枚举源点,汇点,最大流. 这里用DFS,状态定义:分成两个集合,刚开始S集合全部点,然后一个一个放,这是一个回溯的过程. 没剪枝也过了. 剪枝技 ...

  8. 第五届蓝桥杯校内选拔第七题_(树型dp)

    G将军有一支训练有素的军队,这个军队除开G将军外,每名士兵都有一个直接上级(可能是其他士兵,也可能是G将军).现在G将军将接受一个特别的任务,需要派遣一部分士兵(至少一个)组成一个敢死队,为了增加敢死 ...

  9. POJ 1321 深搜dfs

    思路其实挺简单的,为什么我想不到呢!!! 原因分析:(1)题目还是做少了 (2)做题目的时候在放音乐 (3)最近脑袋都不愿意想思路总是想一些无用的 改进:(1)以后做题目坚决不开音乐,QQ直接挂隐身 ...

随机推荐

  1. Word Ladder(找出start——end的最短长度)——bfs

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  2. Solaris主机间的信任关系机制

    解决问题: 管理员经常在其他服务器之间登录,是否需要密码切换. 知识点:主机间信任关系.R 命令集 /etc/hosts/equiv 文件 R服务是不加密的,别人可以破解. 主机名 + 用户名. + ...

  3. Android的logger机制分析

    分析安卓的Logger机制 一.概述 Logger机制是在Android系统中提供的一个轻量级的日志系统,这个日志系统是以驱动程序的形式在内核空间实现的,在用户空间分别提供了Java接口和C/C++接 ...

  4. HAProxy简单使用

    一.HAProxy简介及定位         HAProxy 是一款基于TCP和HTTP应用的具备高可用行且负载均衡的代理软件.HAProxy是完全免费的,借助HAProxy可以快速.可靠地提供基于T ...

  5. VueJS表达式支持:逻辑控制或运算

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. javascript 高级编程系列 - 基本数据类型

    javascript中的基本数据类型包括: Undefined, Null, Boolean, Number, String 5种数据类型 1. Undefined 类型 (只有一个值 undefin ...

  7. 软考考点---CPU

    软考考点---CPU         中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Un ...

  8. inline-block的使用

    inline-block是什么 inline和block是css中元素display属性的两个选项,而inline-block可以说是介于两者之间的属性值. inline使元素成为内联元素(inlin ...

  9. Spring Data MongoDB 四:基本文档改动(update)(一)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 二:MongoDB加入.删除.改动 一.简单介绍 Spring Data  Mo ...

  10. Creating Tabbed Applications

    新建一个空工程,如图 新建类 using System; using UIKit; namespace TabbedApplication { public class TabController : ...