我是靠谱客的博主 顺心香烟,最近开发中收集的这篇文章主要介绍ZOJ-1649,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

--------------------------------------------------------------------------------

Time Limit: 2 Seconds      Memory Limit: 65536 KB

--------------------------------------------------------------------------------

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

 ACM网站崩了 没有去试能不能通过 测试结果表示可以

#include <iostream>
#include <queue>
using namespace std;
struct node
{
int x;
int y;
int time;
};
bool operator <(node n1,node n2)
{
return n1.time>n2.time;
}
int main()
{
node first,ending,temp,temp_2;
int line,row,temp_x,temp_y;
char prison[201][201];
int repeat[201][201];
int X[4]={1,-1,0,0};
int Y[4]={0,0,1,-1};
priority_queue <node> p;
//使用优先队列
while(1)
{
while(!p.empty())
//清空队列
p.pop();
cin>>line>>row;
for(int i=0;i<line;i++)
for(int j=0;j<row;j++)
{
cin>>prison[i][j];
//输入数据
repeat[i][j]=0;
}
for(int i=0;i<line;i++)
for(int j=0;j<row;j++)
{
if(prison[i][j]=='r')
//找到起点
{
first.x=i;
first.y=j;
first.time=0;
repeat[i][j]=1;
p.push(first);
}
if(prison[i][j]=='a')
//找到终点
{
ending.x=i;
ending.y=j;
}
}
while(!p.empty())
{
if(repeat[ending.x][ending.y]==1)
break;
temp=p.top();
p.pop();
for(int i=0;i<4;i++)
{
temp_x=temp.x+X[i];
temp_y=temp.y+Y[i];
if(temp_x>=0&&temp_x<line&&temp_y>=0&&temp_y<row&&repeat[temp_x][temp_y]==0)
{
if(prison[temp_x][temp_y]=='a')
{
repeat[temp_x][temp_y]=1;
cout<<temp.time+1<<endl;
break;
}
if(prison[temp_x][temp_y]=='.')
{
repeat[temp_x][temp_y]=1;
temp_2.x=temp_x;
temp_2.y=temp_y;
temp_2.time=temp.time+1;
p.push(temp_2);
}
if(prison[temp_x][temp_y]=='x')
{
repeat[temp_x][temp_y]=1;
temp_2.x=temp_x;
temp_2.y=temp_y;
temp_2.time=temp.time+2;
p.push(temp_2);
}
}
}
}
if(repeat[ending.x][ending.y]==0)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
return 0;
}

超时了无法通过

最后

以上就是顺心香烟为你收集整理的ZOJ-1649的全部内容,希望文章能够帮你解决ZOJ-1649所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部