Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26321 Accepted Submission(s): 9304
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.)
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.
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
介个题在什么时候救不出来真的难,判断了n个测试样例,经常出错,好像要用优先队列.........然后我就照葫芦画瓢,果然可以求出最短时间,但是什么时候救不出来,还是不好判断,最终 还是做完了,wa了好几十回!!!!
代码在下边(经过千修万改):
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <queue>
#define M 210
using namespace std;
struct node{
int x,y,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
//用时少的先出队列
}
};
node s,e;
int n,m,ans;
char map[M][M];
int vis[M][M];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
int BFS(node s)
{
vis[s.x][s.y]=0;
priority_queue<node> q;
q.push(s);
while(!q.empty())
{
node now=q.top();
q.pop();
for(int i=0;i<4;i++)
{
node next;
next.x=now.x+fx[i];
next.y=now.y+fy[i];
next.step=now.step;
if(vis[next.x][next.y]==1&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m)
{
if(map[next.x][next.y]=='x')
next.step+=1;
vis[next.x][next.y]=0;
next.step+=1;
q.push(next);
if(next.x==e.x&&next.y==e.y) return next.step;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='.'||map[i][j]=='x') vis[i][j]=1;
if(map[i][j]=='r'){s.x=i;s.y=j;vis[i][j]=0;}
if(map[i][j]=='a'){e.x=i;e.y=j;vis[i][j]=1;}
}
}
ans=0;
s.step=0;
ans=BFS(s);
if(ans) printf("%dn",ans);
else printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}
最后
以上就是忐忑毛巾最近收集整理的关于HDOJ-1242 RescueRescue的全部内容,更多相关HDOJ-1242内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复