题目链接:http://poj.org/problem?id=2676

Time Limit: 2000MS Memory Limit: 65536K

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

题意:

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

数独的答案都是唯一的,所以,多个解也称为无解。

本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。

本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。

格式要求:
输入9行,每行9个数字,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。

题解:

从[1,1]到[9,9]地进行DFS

AC代码:

#include<cstdio>
#include<cstring>
using namespace std; int num[][];
bool rowVis[][],colVis[][],matVis[][]; struct Pos{
int row,col;
Pos nextpos()
{
if(col==) return (Pos){row+,};
else return (Pos){row,col+};
}
int MatID(){return (row-)/* + (col-)/+;}
bool ok(int x)
{
if(!rowVis[row][x] && !colVis[col][x] && !matVis[MatID()][x]) return ;
else return ;
}
}; void mark(Pos pos,int x,bool val)
{
rowVis[pos.row][x] = val;
colVis[pos.col][x] = val;
matVis[pos.MatID()][x] = val;
} bool dfs(Pos pos)
{
int row=pos.row,col=pos.col; if(row==) return ;
if(num[row][col]) return dfs(pos.nextpos()); for(int i=;i<=;i++)
{
if(pos.ok(i))
{
num[row][col]=i; mark(pos,i,);
if(dfs(pos.nextpos())) return ;
num[row][col]=; mark(pos,i,);
}
}
return ;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(rowVis,,sizeof(rowVis));
memset(colVis,,sizeof(colVis));
memset(matVis,,sizeof(matVis));
for(int row=;row<=;row++)
{
char tmp[]; scanf("%s",tmp);
for(int col=;col<=;col++)
{
num[row][col]=tmp[col-]-'';
if(num[row][col]) mark((Pos){row,col},num[row][col],);
}
} dfs((Pos){,}); for(int row=;row<=;row++)
{
for(int col=;col<=;col++) printf("%d",num[row][col]);
printf("\n");
}
}
}

POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  2. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

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

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

  4. 蓝桥杯 带分数 DFS应用

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...

  5. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  6. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  7. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  8. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  9. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

随机推荐

  1. Nexus5 破解电信关键步骤

    5儿子终于摔坏了,送去保养之后,发现之前已破解的电信3G竟然无效了,心碎!!!!!!!!!!!!!!!!!! 尝试恢复efs --还好有备份,备份万岁!!! 不行!继续尝试恢复!还是不行!再试!... ...

  2. Android ViewDragHelper全然解析 自己定义ViewGroup神器

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46858663. 本文出自:[张鸿洋的博客] 一.概述 在自己定义ViewGro ...

  3. OpenGl 知识一

    写在前面 啦啦啦,搞了很久的Unity Shaders,越学越觉得基础知识很重要.学Unity Shader的时候,总会想,shader到底是什么呢?shader的pipeline是什么呢?它们是怎么 ...

  4. Bypass 护卫神SQL注入防御(多姿势)

    0x00 前言 ​ 护卫神一直专注服务器安全领域, 其中有一款产品,护卫神·入侵防护系统 ,提供了一些网站安全防护的功能,在IIS加固模块中有一个SQL防注入功能. 这边主要分享一下几种思路,Bypa ...

  5. Nginx SSL配置

    一.SSL 原理 ① 客户端( 浏览器 )发送一个 https 请求给服务器② 服务器要有一套证书,其实就是公钥和私钥,这套证书可以自己生成,也可以向组织申请,服务器会把公钥传输给客户端③ 客户端收到 ...

  6. Linux下chkconfig命令详解转载

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...

  7. myeclipse使用小技巧

    1 如何关掉maven自动更新,去掉前面的勾 2 myeclipse中如何设置不校验js

  8. 游戏服务器学习笔记 4———— master 模块介绍

    (模块的介绍方法都是先说大体功能,在捡一些细节详细讨论.) master 类很简单,就3个函数,一个init,设置配置信息,并调用masterapp,然后还有一个循环启动子进程的start函数. 这里 ...

  9. windows下使用git管理代码,其中出现的问题的解决办法

    和朋友共同开发一个小项目,所以就涉及到了代码管理这块,刚开始想到的是使用svn,但是外网访问svn的时候需要使用花生壳来弄一个动态的域名,中间出了很多错误,感觉有点麻烦,所以就想到看看还有别的管理代码 ...

  10. 查看iOS沙盒(SanBox)文件

    转载:http://www.2cto.com/kf/201211/169212.html 每一个iOS程序都一个自己的文件系统,这个文件系统叫应用程序沙盒(SanBox),它存放这代码以外的文件,其他 ...