Sudoku

Problem's Link:   http://poj.org/problem?id=2676


Mean:

analyse:

记录所有空位置,判断当前空位置是否可以填某个数,然后直接DFS,注意从后往前搜索,时间比正向搜快很多。16ms水过

Time complexity: O(n)

Source code: 

//  Memory   Time
// 1347K 0MS
// by : crazyacking
// 2015-04-10-14.30
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
struct Node
{
int x,y;
};
Node b[];
int g[][];
int idx;
bool flag;
void input()
{
char s[];
idx=;
flag=;
for(int i=;i<;++i)
{
scanf("%s",s);
for(int j=;j<;++j)
{
g[i][j]=s[j]-'';
if(g[i][j]==)
{
b[idx].x=i;
b[idx].y=j;
idx++;
}
}
}
idx--;
} bool Check(int x,int y,int num)
{
for(int i=;i<;++i)
{
if(g[x][i]==num) return false;
if(g[i][y]==num) return false;
}
int sta_x=x/*;
int sta_y=y/*;
for(int i=sta_x;i<sta_x+;++i)
{
for(int j=sta_y;j<sta_y+;++j)
{
if(g[i][j]==num) return false;
}
}
return true;
}
void DFS(int n)
{
if(n==-)
{
flag=;
return ;
}
int x=b[n].x;
int y=b[n].y;
for(int i=;i<=;++i)
{
if(Check(x,y,i))
{
g[x][y]=i;
DFS(n-);
if(flag) return ;
g[x][y]=;
}
}
return;
}
void output()
{
for(int i=;i<;++i)
{
for(int j=;j<;++j)
{
printf("%d",g[i][j]);
}
puts("");
}
}
int main()
{
int Cas;
scanf("%d",&Cas);
while(Cas--)
{
input();
DFS(idx);
output();
}
return ;
}

搜索 --- 数独求解 POJ 2676 Sudoku的更多相关文章

  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. POJ 2676 Sudoku (数独 DFS)

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

  4. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  5. 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 ...

  6. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

  7. DFS POJ 2676 Sudoku

    题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...

  8. poj 2676 Sudoku ( dfs )

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

  9. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

随机推荐

  1. 通过Web.config实现301重定向

    通过Web.config实现301重定向 IIS7以上可以通过修改Web.config实现IIS设置 现在我们通过Web.config实现301重定向 <system.webServer> ...

  2. 昨日尝试使用百度死链提交,使用lCGI规则提交

    本来打算去掉北盟网校的死链,但就算配了规则,提交百度,但是好像还是没有删除到 认真阅读了百度的死链工具 好像需要将死链返回404错误提示 检查北盟的代码,发现北盟做了404从定向 在程序里面404从定 ...

  3. OpenSSL命令---pkcs7

    用途: 用于处理DER或者PEM格式的pkcs#7文件. 用法: openssl pkcs7 [-inform PEM|DER] [-outform PEM|DER] [-in filename] [ ...

  4. 转:如何学习javascript

    ps:每过一段时间就会发觉自己懂的越来越少,打开编辑器的时候不知道从何入手.兴许是过于急于求成,总没有系统地去对这门语言进行学习,囫囵吞枣,只想着能够解决工作需求.是时候让自己重新出发,从零开始,一页 ...

  5. 如何导入大sql文件到虚拟主机mysql数据库

    大部分网站虚拟主机为了安全起见,都限制了通过命令或者phpMyAdmin导入大sql文件到mysql数据库,例如godaddy只允许站长通过phpMyAdmin上传不超过2m的sql文件,但实际上我们 ...

  6. Android 6.0 源代码编译实践

    http://www.judymax.com/archives/1087 Android 6.0 源代码编译实践 https://mirrors.tuna.tsinghua.edu.cn/help/A ...

  7. SQL语句删除所有表

    ) )     ) )     ) )     ) ) )  TABLE_NAME  CONSTRAINT_NAME  CONSTRAINT_NAME  TABLE_NAME ) ) )  TABLE ...

  8. Kafka - SQL 引擎分享

    1.概述 大多数情况下,我们使用 Kafka 只是作为消息处理.在有些情况下,我们需要多次读取 Kafka 集群中的数据.当然,我们可以通过调用 Kafka 的 API 来完成,但是针对不同的业务需求 ...

  9. (笔记)Linux内核学习(五)之中断推后处理机制

    一 中断 硬件通过中断与操作系统进行通信,通过对硬件驱动程序处注册中断处理程序,快速响应硬件的中断. 硬件中断优先级很高,打断当前正在执行的程序.有两种情况: 硬件中断在中断处理程序中处理 硬件中断延 ...

  10. 将数据库表导入到solr索引

    将数据库表导入到solr索引 编辑solrcofnig.xml添加处理器 <requestHandler name="/dataimport" class="org ...