【HDOJ】1072 Nightmare
bfs,使用ttl进行剪枝。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXNUM 10 int map[MAXNUM][MAXNUM], ttls[MAXNUM][MAXNUM], n, m;
int direct[][] = {{-,},{,},{,-},{,}}; typedef struct node_st {
int ttl;
int t;
int x, y;
node_st() {}
node_st(int ttll, int tt, int xx, int yy) {
ttl = ttll;
t = tt;
x = xx;
y = yy;
}
} node_st; int bfs(int begx, int begy) {
int val = -;
int x=begx, y=begy, ttl=, t=;
int newx, newy, newt, newttl;
queue<node_st> nodes;
node_st node; memset(ttls, , sizeof(ttls));
nodes.push(node_st(ttl,t,x,y));
map[x][y] = ; while ( !nodes.empty() ) {
node = nodes.front();
nodes.pop();
x = node.x;
y = node.y;
ttl = node.ttl;
t = node.t;
if (map[x][y]== && ttl) {
val = node.t;
break;
}
--ttl; ++t;
if (ttl == )
continue;
for (int i=; i<; ++i) {
newx = x + direct[i][];
newy = y + direct[i][];
newttl = ttl;
newt = t;
if (newx< || newx>=n || newy< || newy>=m)
continue;
if (map[newx][newy] == )
continue;
if (map[newx][newy] == ) {
newttl = ;
map[newx][newy] = ;
}
if (newttl > ttls[newx][newy]) {
ttls[newx][newy] = newttl;
nodes.push(node_st(newttl, newt, newx, newy));
}
}
} return val;
} int main() {
int t;
int i, j, begx, begy; scanf("%d", &t); while (t--) {
scanf("%d %d", &n, &m);
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
scanf("%d", &map[i][j]);
if (map[i][j] == ) {
begx = i;
begy = j;
}
}
}
i = bfs(begx, begy);
printf("%d\n", i);
} return ;
}
【HDOJ】1072 Nightmare的更多相关文章
- 【HDOJ】3085 Nightmare Ⅱ
双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...
- 【BZOJ】1072: [SCOI2007]排列perm(状压dp+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1072 首先无限膜拜题解orz表示只会暴力orz 数据那么小我竟然想不到状压! orz 这种题可以取模 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
随机推荐
- Android 讯飞语音之语音合成(在线有声朗读)
原文:http://www.cnblogs.com/scetopcsa/p/3845427.html 在线语音合成的使用方法: 首先下载相关的sdk,这个网址里有多种版本,我选择的Android. h ...
- 【转】【SQLServer】SQL Server 2008“阻止保存要求重新创建表的更改”
不是很理解为什么在2008中会加入阻止保存要求重新创建表的更改这个选项.症状表现为修改表结构的时候会"阻止"你.而且我遇到的情况是居然有的时候阻止你,有的时候不阻止你,摸不到头脑. ...
- gdal中文路径无法打开问题
在C#中使用OGR读写矢量数据时,需要引用“using OSGeo.OGR;”. 同时为了处理中文路径和中文字段,需要在开始设置下面两个属性,代码如下: //为了支持中文路径,请添加下面这句代码(大多 ...
- /etc/sysconfig/目录详解
/etc/sysconfig/目录包括了在红帽企业LINUX下各种系统配置文件,以下是在/etc/sysconfig/目录下的文件列表如图: 500)this.width=500;"> ...
- jQuery $.each的使用方法
通过jQuery $.each,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, func ...
- iOS 图片按比例压缩,指定大小压缩
使用系统方法UIImageJPEGRepresentation(UIimage *image,CGFloat quality)进行图片质量压缩,暂且叫参数quality为压缩比吧,取值范围为0-1. ...
- HDU 2809 God of War(DP + 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
- 微软AJAX解决方案
-------- 微软AJAX解决方案 (*) --------ASP.Net中内置的简化AJAX开发的控件UpdatePanel 放入ScriptManager,将要实现AJAX效果的控件放到Upd ...
- HTML教程:link标记
开发php语言的网站,<head>里link标签这样:<link href="xmlrpc.php?rsd=1" title="rsd" ty ...