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 ...
随机推荐
- ubuntu 下GIT的安装
linux下软件的安装方式有多种,最简单的莫过于从软件中心直接安装了或者用命令行直接安装 sudo apt-get install git 但是这样的安装却使我们体会不到最新版本的功能,如果我们想要体 ...
- 管理TEMP数据
SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...
- 专注于HTTP的高性能高易用性网络库:Fslib.network库
博客列表页:http://blog.fishlee.net/tag/fslib-network/ 原创FSLib.Network库(目前专注于HTTP的高性能高易用性网络库) FSLib.Networ ...
- SQL语言的组成
在正式学习SQL语言之前,首先让我们对SQL语言有一个基本认识,介绍一下SQL语言的 组成: 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成, ...
- Computer Graphics Thinking–texture tiling
Here is one question: how to tile texture? One thing worth to notice: The DirectX and OpenGL stipula ...
- eucimage
- java 面试基础典型题及答案
1.switch能否作用在byte.int.long.String? 答案:switch能作用在byte.int.enum常量, 补充:jdk7可以作用在String上 2.short s = 1; ...
- hdu 4790 Just Random (思路+分类计算+数学)
Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with ...
- Android studio无法更新 提示网络连接失败
Android studio 更新时,提示网络问题 “Connection failed. Please check your network connection and try again” 在默 ...
- BNU10805:矩形神码的
我们都知道,矩形是由两条对角线的,没错吧?(谜之声:这不是显然么!)这两条线的长度也是相等的,没错吧?(谜之声:这不废话么!)然后我们给定一条对角线的起始点和终止点的坐标,然后给定另一个对角线和他的夹 ...