poj Sudoku(数独) DFS
| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 13665 | Accepted: 6767 | Special Judge | ||
Description

Input
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
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
讲解:游戏都玩过,但就是基本上见过没做出来过,哈哈、、、挺有意思的代码,题意都知道,关键就是如何进行搜索,其实就是不断地进行枚举,如果满足所有的条件就跳出来;
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
int Map[N][N], flag;
char MAp[N][N];
int check(int ans,int key)
{
int x = (key-)/+;
int y = key-(x-)*;
for(int i=; i<=; i++)//行是否冲突
if(Map[x][i] == ans)
return ;
for(int i=; i<=; i++) //列是否冲突
if(Map[i][y] == ans)
return ;
if(x<=)x = ; //x所在小九格起点
if(x> && x<)x=;
if(x>=) x=;
if(y<=) y = ; //y所在小九格起点
if(y> && y<) y=;
if(y>=) y=;
for(int i=; i<; i++) //小的九格是否冲突
for(int j=; j<; j++)
if(Map[x+i][y+j] == ans)
return ;
return ;//以上条件都不符合,返回1;
}
int dfs(int key)
{
int x = (key-)/+;
int y = key-(x-)*;
if(key>)
{
flag = ;
return ;
}
if(Map[x][y]!=)//不是0,直接跳过去
{
dfs(key+);
}
else //否者,在这个位置枚举九个数,进行递归搜索
{
for(int k=; k<=; k++)
if(check(k,key))
{
Map[x][y] = k;
dfs(key+);
if(flag == ) return ;
Map[x][y] = ;
}
}
return ;
}
int main()
{
int T,i,j;
// freopen("in2.txt","r",stdin);
// freopen("out2.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
memset(Map,,sizeof(Map));
for(i=; i<=; i++)
for(j=; j<=; j++)
{
cin>>MAp[i][j];
Map[i][j] = MAp[i][j]-'';
}
flag = ;
dfs();
for(i=; i<=; i++)
{
for(j=; j<=; j++)
printf("%d",Map[i][j]);
printf("\n");
}
}
return ;
}
poj Sudoku(数独) DFS的更多相关文章
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- POJ Sudoku 数独填数 DFS
题目链接:Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18105 Accepted: 8772 Sp ...
- POJ 2676 数独(DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21612 Accepted: 10274 Specia ...
- POJ 2676 数独+dfs深搜
数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...
- 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 ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
随机推荐
- 将图片转换为Base64字符串公共类抽取
public class ImageToBase64 { //图片转化成base64字符串 public static String GetImageStr(String path,int width ...
- [Java基础] Java线程复习笔记
先说说线程和进程,现代操作系统几乎无一例外地采用进程的概念,进程之间基本上可以认为是相互独立的,共享的资源非常少.线程可以认为是轻量级的进 程,充分地利用线程可以使得同一个进程中执行多种任务.Java ...
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
经过二次整合 重新放出MFC框架下的坦克大战 采用小窗口 多线程 双缓冲 动画帧化 碰撞检测 接口封装 混音 事件延迟等 力求做到代码与美工的双向化 开场动画 界面一 界面二 游戏界面 结束动画 零积 ...
- (转)PV、UV、IP之间的区别与联系
PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量.对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量,如同一个来访者 ...
- Cocos2d-x中点九图(Scale9Sprite)创建图片按钮
1. 配置引擎根目录支持 C/C++->常规->附加包含目录->增加->$(EngineRoot) 2. 引入头文件 #include "cocos-ext.h&qu ...
- js中,三元运算的简单应用(?:)
js中,三元运算的简单应用: var sinOrMul = ""; sinOrMul =(subType=="single")?("<span ...
- tornado ThreadPoolExecutor
import os import sys import time import tornado.httpserver import tornado.ioloop import tornado.opti ...
- xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code
xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cyc ...
- has been modified since the precompiled header地图错误
添加高德地图的一个错误 has been modified since the precompiled header 解决方案: xcode---product----clean 清理重新编译一下就 ...
- 【转】Struts2 和 Spring MVC对比
1. 实现机制 struts2框架是类级别的拦截,每次来了请求就创建一个controller中对应的Action,然后调用setter getter方法把request中的数据注入 .struts2实 ...