ACM--steps--4.2.1--Rescue(BFS+priority)

来源:互联网 发布:js url encode gbk 编辑:程序博客网 时间:2024/05/16 01:15

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 932 Accepted Submission(s): 390 
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
 
Author
CHEN, Xue
 
Source
ZOJ Monthly, October 2003
 
Recommend
Eddy


#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;struct dyx{    int x,y,time;//x,y表示坐标,time表示所用的时间,    friend bool operator<(dyx a,dyx b)    {        return b.time<a.time;//time小的优先级大。    }};char map[209][209];//输入地图;//dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。int n,m,vis[209][209];//n,m分别为行列数,访问过的状态。int x1,x2,y1,y2;//表示起始目标和终止目标;int dx[4]={1,0,-1,0};//表示方向向量。int dy[4]={0,1,0,-1};//检查当前的状态。int wyx(int x,int y){    if(x<0||y<0||x>=n||y>=m||map[x][y]=='#'||!vis[x][y])    return 1;    return 0;}int bfs(){    dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。    int i;    priority_queue<dyx> que;    now.x=x1;    now.y=y1;    now.time=0;    que.push(now);    vis[x1][y1]=0;    while(!que.empty())    {        now=que.top();        que.pop();        if(now.x==x2&&now.y==y2)        return now.time;        for(i=0;i<4;i++)        {            next=now;            next.x+=dx[i];            next.y+=dy[i];            if(wyx(next.x,next.y))            continue;            next.time++;            if(map[next.x][next.y]=='x')            //在卫兵的地方要达到卫兵所以要多花费1秒。            next.time++;            if(vis[next.x][next.y]>=next.time)            {//最小时间,                vis[next.x][next.y]=next.time;                que.push(next);            }        }    }    return 0;}int main(){    int i,j;    while(~scanf("%d%d",&n,&m))    {        for(i=0;i<n;i++)        for(j=0;j<m;j++)        {            cin>>map[i][j];            if(map[i][j]=='r')            {                x1=i;                y1=j;            }            else if(map[i][j]=='a')            {                x2=i;                y2=j;            }        }        memset(vis,1,sizeof(vis));//1表示未经过的状态。        int ans=0;        ans=bfs();    if(ans)    cout<<ans<<endl;    else    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;    }    return 0;}


 
0 0
原创粉丝点击