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 ...
随机推荐
- DataTables语言国际化
$('#example').DataTable({ language: { "sProcessing": "处理中...", ...
- iOS 的 APP 在系统中如何适配不同的屏幕的尺寸
iOS 的 APP 在系统中如何适配不同的屏幕的尺寸 标签: 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone ...
- 进程外组件通信之免注册com通信【原创】
最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
- PCB Layout 中的高频电路布线技巧
1.多层板布线 高频电路往往集成度较高,布线密度大,采用多层板既是布线所必须,也是降低干扰的有效手段.在PCB Layout阶段,合理的选择一定层数的印制板尺寸,能充分利用中间层来设置屏蔽,更好地实现 ...
- 简单计算器(Android)
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKC
- H264编码技术
H.264的目标应用涵盖了眼下大部分的视频服务,如有线电视远程监控.交互媒体.数字电视.视频会议.视频点播.流媒体服务等.H.264为解决不同应用中的网络传输的差异.定义了两层:视频编码层(VCL:V ...
- [Angular 2] WebStorm - Managing Imports
Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...
- public,private,protected的区别
一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等.protected: 受保护类型,用于本类和继承类调用.private: 私有类型,只有 ...
- VMware SphereESXi上安装虚拟机
VMware SphereESXi上安装虚拟机 创建新虚拟机 此处以CentOS为例 注意:配置上传的系统文件位置及启动项