Rescue--hdu1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21431 Accepted Submission(s): 7641
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
这个题是用广搜写的!但是这个题是有点特殊的,因为遇到X的时候时间是要再加一的!所以可以用优先队列来解决,但是还是可以用普通队列来解决的,方法就是遇到X先加一,然后标记走过,再后来再遇到X直接加一,不再往四周搜索!等于走了两步!详情如下
普通队列版:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ma 210
using namespace std;
char map[ma][ma];
int v[ma][ma],m,n;
int mov[][]={,,-,,,,,-};
struct node
{
int x,y,step,flag;
};
bool can(node x)
{
if(x.x>=&&x.x<m&&x.y>=&&x.y<n&&(map[x.x][x.y]!='#')&&!v[x.x][x.y])
return true ;
return false;
}
int bfs(int x,int y)
{
int i;
memset(v,,sizeof(v));
queue<node>q;
node ta,tb;
ta.x=x;
ta.y=y;
ta.step=;
ta.flag=;
q.push(ta);
while(!q.empty())
{
ta=q.front();
q.pop();
if(ta.flag==)
{
ta.step++;
ta.flag=;
q.push(ta);
continue;
}//第二次遇到时,flag已经等于1,这时候不能走了,步数要加一,而且flag要重新标记为0
if(map[ta.x][ta.y]=='a')
return ta.step;
for(i=;i<;i++)
{
tb.x=ta.x+mov[i][];
tb.y=ta.y+mov[i][];
tb.step=ta.step+;
if(can(tb))
{ if(map[tb.x][tb.y]=='x')
tb.flag=;
else
tb.flag=;//第一次遇到X就标记为flag=1,否则为0!
v[tb.x][tb.y]=;
q.push(tb);
}
}
}
return -;
}
int main()
{
int i,j,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
getchar();
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
{
a=i;
b=j;
}
}
getchar();
}
v[a][b]=;
int rr=bfs(a,b);
if(rr==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",rr);
}
return ;
}
优先队列版:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 210
using namespace std;
int m,n,v[maxn][maxn],mov[][]={,,-,,,,,-};
char map[maxn][maxn]; struct node
{
int x,y,step;
friend bool operator <(node x,node y)
{
return x.step>y.step;
}
};
priority_queue<node>q;
bool can(node x)
{
if(!v[x.x][x.y]&&x.x>=&&x.x<m&&x.y>=&&x.y<n&&map[x.x][x.y]!='#')//(map[x.x][x.y]=='.'||map[x.x][x.y]=='x'))
return true;
return false;
}
int bfs(int x,int y)
{
int i;
node ta,tb;
ta.x=x;
ta.y=y;
ta.step=;
q.push(ta);
while(!q.empty())
{
ta=q.top();
q.pop();
if(map[ta.x][ta.y]=='a')
return ta.step;//到终点就返回队首,用的优先队列所以队首的步数最少!
for(i=;i<;i++)
{
tb.x=ta.x+mov[i][];
tb.y=ta.y+mov[i][];
if(can(tb))
{
if(map[tb.x][tb.y]=='x')
tb.step=ta.step+;//遇到X一定要加2
else
tb.step=ta.step+;//其他加1
v[tb.x][tb.y]=;
q.push(tb);
} }
}
return -;//找不到就返回-1
}
int main()
{
int i,j,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
getchar();
memset(v,,sizeof(v));
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
a=i,b=j;
}
getchar();
}
v[a][b]=;
int rr=bfs(a,b);
if(rr==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",rr);
}
return ;
}
Rescue--hdu1242的更多相关文章
- Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- HDU1242 Rescue
Rescue Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Description A ...
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu1242 Rescue DFS(路径探索题)
这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...
- 搜索专题: HDU1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)
本节我们讨论 Suspend/Resume 和 Rescue/Unrescue 这两组操作. Suspend/Resume 有时需要长时间暂停 instance,可以通过 Suspend 操作将 in ...
- HDU1242 BFS+优先队列
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU-4057 Rescue the Rabbit(AC自动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- C语言计算机器运行时间
//计算机器运行时间 long i = 10000000L;clock_t start, finish;double duration;//测量一个事件持续的时间printf( "Time ...
- sqlachemy 使用实例
sqlachemy 是python中关于sql的ORM,他的存在可以消除底层sql引擎的差异,同事也避免了复杂繁琐的sql语句,因此我们在比较大的应用时常使用它,下面是我写的一个例子 #!/usr/b ...
- 测试一下PHP官方的新一代PHP加速插件ZendOpcache的性能及配置
过程不表,都比较顺利 参考如下URL: http://www.lvtao.net/server/ZendOpcache.html 大家知道目前PHP的缓存插件一般有三个:APC.eAccelerato ...
- 使IE6同样支持圆角效果
之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...
- 基于Bootstrap 3.x的免费高级管理控制面板主题:AdminLTE
AdminLTE 是一个基于Bootstrap 3.x的免费高级管理控制面板主题.AdminLTE - 是一个完全响应式管理模板.基于Bootstrap3框架.高度可定制的,易于使用.适合从小型移动设 ...
- <php>过时方法连接数据库代码
<?php //1.生成链接 $db_connect = mysql_connect("localhost","root","20982239& ...
- lucene 使用教程
原文转自:http://cloudera.iteye.com/blog/656459 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像 ...
- qt动态更新界面的菜鸟代码,请指出
qt简单界面更新代码(菜鸟级)(部分代码) self.timers_1=QtCore.QTimer(self) self.timers_1.timeout.connect(self.min_1) se ...
- C/C++基础概念
1.类占用的内存大小: 1)在不同位数的操作系统下,各种数据类型所占用的内存大小:32位和64位操作系统 http://blog.csdn.net/b_zhang/article/details/68 ...
- java.lang.NoSuchFieldError: deferredExpression解决
java.lang.NoSuchFieldError: deferredExpression这个问题的出现是在的lib下面有多个版本的jstl.jar包,解决办法很简单,只留下一个版本的jstl ...