ACM-BFS之Rescue——hdu1242
2014-04-09 12:14
267 查看
Rescue
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
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.)
Input
First line contains two integers stand for N and M.
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.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
用到优先队列的广搜,题意就是从r出发到a结束:
'.'是路,'#'是墙,'x'是卫兵,遇到.花费1s,遇到x花费2s。
求最短步数,若无法到达,输出-1。
BFS题目,这里用到了优先队列,vis数组存的是到达某一点的最短步数,而不是是否访问过这一点。
现在都有点不懂:题目中 "r" stands for each of Angel's friend.
和Angel's friends want to save Angel. Their task is: approach Angel.
我一直以为一张地图里的r有多个。。
事实证明好像r是只有一个。。。o(╯□╰)o啊
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int n,m,f_x,f_y,dis[4][2]={0,1,0,-1,1,0,-1,0};
char mapp[201][201];
int vis[201][201];
struct Coordinate
{
int x,y,step;
friend bool operator<(Coordinate c1,Coordinate c2)
{
return c2.step<c1.step;
}
}coor[1001];
// 判断当前点是否越界或碰墙
bool judge(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m) return 0;
if(mapp[x][y]=='#') return 0;
return 1;
}
// bfs用优先队列来做
int bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
priority_queue <Coordinate> q;
Coordinate pre,lst;
int i;
pre.x=x;
pre.y=y;
pre.step=0;
q.push(pre);
while(!q.empty())
{
pre=q.top();
q.pop();
if(pre.x==f_x && pre.y==f_y) return pre.step;
for(i=0;i<4;++i)
{
lst.x=pre.x+dis[i][0];
lst.y=pre.y+dis[i][1];
if(judge(lst.x,lst.y))
{
// 判断当前点是否有卫兵,有则增加步数
if(mapp[lst.x][lst.y]=='x') lst.step=pre.step+2;
else lst.step=pre.step+1;
// vis存到达该点最短步数
if(vis[lst.x][lst.y]>=lst.step || vis[lst.x][lst.y]==0)
{
vis[lst.x][lst.y]=lst.step;
q.push(lst);
}
}
}
}
return -1;
}
int main()
{
int i,j,s_x,s_y;
int temp;
while(cin>>n>>m)
{
for(i=0;i<n;++i)
for(j=0;j<m;++j)
{
cin>>mapp[i][j];
if(mapp[i][j]=='r') {s_x=i;s_y=j;}
if(mapp[i][j]=='a') {f_x=i;f_y=j;}
}
temp=bfs(s_x,s_y);
if(temp==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<temp<<endl;
}
return 0;
}
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1242
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
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.)
Input
First line contains two integers stand for N and M.
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.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
用到优先队列的广搜,题意就是从r出发到a结束:
'.'是路,'#'是墙,'x'是卫兵,遇到.花费1s,遇到x花费2s。
求最短步数,若无法到达,输出-1。
BFS题目,这里用到了优先队列,vis数组存的是到达某一点的最短步数,而不是是否访问过这一点。
现在都有点不懂:题目中 "r" stands for each of Angel's friend.
和Angel's friends want to save Angel. Their task is: approach Angel.
我一直以为一张地图里的r有多个。。
事实证明好像r是只有一个。。。o(╯□╰)o啊
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int n,m,f_x,f_y,dis[4][2]={0,1,0,-1,1,0,-1,0};
char mapp[201][201];
int vis[201][201];
struct Coordinate
{
int x,y,step;
friend bool operator<(Coordinate c1,Coordinate c2)
{
return c2.step<c1.step;
}
}coor[1001];
// 判断当前点是否越界或碰墙
bool judge(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m) return 0;
if(mapp[x][y]=='#') return 0;
return 1;
}
// bfs用优先队列来做
int bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
priority_queue <Coordinate> q;
Coordinate pre,lst;
int i;
pre.x=x;
pre.y=y;
pre.step=0;
q.push(pre);
while(!q.empty())
{
pre=q.top();
q.pop();
if(pre.x==f_x && pre.y==f_y) return pre.step;
for(i=0;i<4;++i)
{
lst.x=pre.x+dis[i][0];
lst.y=pre.y+dis[i][1];
if(judge(lst.x,lst.y))
{
// 判断当前点是否有卫兵,有则增加步数
if(mapp[lst.x][lst.y]=='x') lst.step=pre.step+2;
else lst.step=pre.step+1;
// vis存到达该点最短步数
if(vis[lst.x][lst.y]>=lst.step || vis[lst.x][lst.y]==0)
{
vis[lst.x][lst.y]=lst.step;
q.push(lst);
}
}
}
}
return -1;
}
int main()
{
int i,j,s_x,s_y;
int temp;
while(cin>>n>>m)
{
for(i=0;i<n;++i)
for(j=0;j<m;++j)
{
cin>>mapp[i][j];
if(mapp[i][j]=='r') {s_x=i;s_y=j;}
if(mapp[i][j]=='a') {f_x=i;f_y=j;}
}
temp=bfs(s_x,s_y);
if(temp==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<temp<<endl;
}
return 0;
}
相关文章推荐
- Hdu1242 Rescue 【简单bfs】
- HDU1242 rescue 【BFS+优先队列】
- ACM--steps--4.2.1--Rescue(BFS+priority)
- HDU1242:Rescue bfs和dfs
- ZOJ1649/HDU1242_Rescue(BFS/优先队列BFS)
- HDU1242 Rescue(BFS松弛)
- hdu1242 Rescue BFS广搜 + 优先队列
- HDU1242 Rescue 【BFS】
- HDU 1242 - Rescue(BFS 优先队列)
- ZOJ第一题_1649Rescue(优先队列下的BFS)
- ZOJ-1649 Rescue BFS (HDU 1242)
- Rescue(逆向BFS)
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
- 1242 Rescue【bfs】
- HDU 4460 Friend Chains 第37届ACM/ICPC杭州赛区题目 (bfs求最短路,求两两之间最短路的最大值)
- 2016 ACM-ICPC Asia Regional B – Help the Princess!(bfs)
- ACM-BFS之knight moves——hdu1327
- 山东省第四届ACM大学生程序设计竞赛Rescue The Princess(一个点绕另一个点的旋转)
- hdu 1242 Rescue 搜索 bfs 优先队列