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 ...
随机推荐
- IOS 客户端测试入门.pdf
IOS 客户端测试入门 http://www.open-open.com/doc/view/42d1257bf67946f595e843bfdbdfeabf
- gitosis使用笔记
gitosis是Git下的权限管理工具,通过一个特殊的仓库(gitosis-admin.git)对Git权限进行管理. 1:服务端安装并配置gitosis (1)通过以下方式获取到安装包 root@w ...
- HDU 4116 Fruit Ninja
http://acm.hdu.edu.cn/showproblem.php?pid=4116 题意:给N个圆,求一条直线最多能经过几个圆?(相切也算) 思路:枚举中心圆,将其他圆的切线按照极角排序,并 ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- Ruby中的Symbol与字符串
刚开始学Ruby,一下子搞不清其中的Symbol(变量需要加:)和字符串有什么区别,为这么要为语言设计这么一个东西.让我很迷惑. 首先,字符串对象,是不同的.比如"String" ...
- VC++如何在程序中用代码注册和卸载ocx控件(代码)
方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx //注册netshare.ocx控件regsvr ...
- bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明
Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's c ...
- 获取java类和方法名
String clazz = this.getClass().getName(); String method = Thread.currentThread() .getStackTrace()[1] ...
- WPF与输入法冲突研究之一:百度输入法会导致WPF程序的崩溃!
在学习和使用了WPF一段时间之后,有点感觉WPF是个不太成熟的框架,不知道是我学的太肤浅,还是WPF得BUG太多! >>>>>>>模拟场景<<&l ...
- 使用python监听、模拟鼠标键盘事件
最近守望职业选手疑似开挂事件挺热闹的,在下小菜一枚,并不能从视频中看出端倪.看了一些关于外挂的讨论,自动点射和压枪只需在鼠标驱动上做些改动即可,自瞄或其他高级功能则需要读内存或修改游戏文件,检测也更容 ...