搜索 --- 数独求解 POJ 2676 Sudoku
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的更多相关文章
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- 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
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12005 Accepted: 5984 Special ...
- DFS POJ 2676 Sudoku
题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku(深搜)
Sudoku Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submi ...
随机推荐
- Excel 二级下拉菜单
http://jingyan.baidu.com/article/cd4c2979f31967756f6e6066.html http://hi.baidu.com/chenshake/item/e1 ...
- nodejs express 框架解密4-路由
本文档是基于express3.4.6 express 的路由是自己去实现的,没有使用connect中的路由中间件模块. 1.在如何创建一个app那篇中,我们提到了路由, //router //路由 t ...
- Mac OSX系统下SVN客户端SCPlugin问题
装上SCPlugin后,Checkout老是出现这种错误: Server certificate verification failed: certificate issued for a diffe ...
- C# Use Pop3Client to read gmail
host = "pop.gmail.com" user = "xxxxx@gmail.com" password = "xxxx" port ...
- Unity中Mesh分解与边缘高亮加上深度检测
一个比较简单的需求,不过遇到些坑,记录下. 房间有多个模型,每个模型可能多个SubMesh,点击后,需要能具体到是那个SubMesh,并且在这个SubMesh上显示边缘高光,以及能个性这单个SubMe ...
- 项目管理知识体系指南(PMBOOK指南)(第5版) 阅读摘要
1.7.2 项目经理的人际技能 领导力: 团队建设: 激励: 沟通: 影响力: 决策能力: 政治和文化意识: 谈判: 建立信任: 冲突管理: 教练技术: 3.4 规划过程组 在制定项目管理计划和项目文 ...
- jQuery插件实现图片展开效果,jquery.gallery。仿腾讯QQ空间说说图片展示效果。
公司的项目http://www.umfun.com/,有个说说的页面(和腾讯QQ空间说说一样),里面有个发表图片功能,上传完图片,需要点击展开的效果. 当时手里面事情比较多(公司就我一个前端),忙不过 ...
- .Net基础
标题 状态 内容 NET应用程序是如何执行的? http://www.cnblogs.com/kingmoon/archive/2012/07/16/2594459.html ...
- apache2.4 以上设定gzip压缩
国内很多文章都是抄来抄去,用不成,用我这个就可以了 编辑 http.conf 文件 去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释 ...
- SQL Server 2008 R2 开启数据库远程连接
今天要测试一个.net系统~因为配置的数据库是SQL Server~我就不得不安装SQL Server 2008 R2~现在我们就一起来看看SQL Server 2008 R2是如何打开远程连接端口1 ...