Rescue HDU - 1242

Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31959 Accepted Submission(s): 11164

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

1.BFS 扩展x时特殊处理,相当于原地踏一步。

#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f
using namespace std;
int book[204][204];
char ma[204][204];
struct node
{
    int x,y,s;
    int f;
};
int n,m,ans;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int bfs(int x,int y)
{
    node now,cur,p;
    queueq;
    p.x = x; p.y = y; p.s =0;
    book[x][y] = 1;
    q.push(p);
    while(!q.empty())
    {
        cur = q.front();
        q.pop();
        if(ma[cur.x][cur.y]=='a')
            {
                return cur.s;
            }
        if(ma[cur.x][cur.y] == 'x' && cur.f==1)
        {
            now.f=2;
            now.s = cur.s +1;
            now.x = cur.x;
            now.y = cur.y;
            q.push(now);
        }
        else
        for(int i=0;i<4;i++)
        {
            int tx = cur.x +dx[i];
            int ty = cur.y +dy[i];
            int ts = cur.s +1;
            if(tx<0||ty<0||tx>n-1||ty>m-1) continue;
            if(!book[tx][ty]&&ma[tx][ty]!='#')
            {
                book[tx][ty] =  1;
                if(ma[tx][ty]== 'x') now.f = 1;
                now.s = ts; now.x = tx; now.y = ty;
                q.push(now);
            }
        }
    }
    return inf;
}
int main()
{
    while(cin>>n>>m)
    {
        ans = inf;
        for(int i=0;iscanf("%s",ma[i]);
        for(int i=0;ifor(int j=0;jif(ma[i][j]=='r')
                {
                    memset(book,0,sizeof(book));
                    ans  = min(bfs(i,j), ans);
                }
            }
        }
        if(ans!=inf)
        cout<else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

2.优先队列

#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f
using namespace std;
int book[204][204];
char ma[204][204];
typedef struct node
{
    int x,y,s;
    bool operator <(const node &a)const
    {
        return s>a.s;//步数少的优先  
    }
}Point;
int n,m,ans;
int dx[4]= {1,0,-1,0};
int dy[4]= {0,1,0,-1};
int bfs(int x,int y)
{
    Point now,cur,p;
    priority_queueq;
    p.x = x;
    p.y = y;
    p.s =0;
    book[x][y] = 1;
    q.push(p);
    while(!q.empty())
    {
        cur = q.top();
        q.pop();
        if(ma[cur.x][cur.y]=='a')
        {
            return cur.s;
        }
        for(int i=0; i<4; i++)
        {
            int tx = cur.x +dx[i];
            int ty = cur.y +dy[i];
            int ts = cur.s +1;
            if(tx<0||ty<0||tx>n-1||ty>m-1) continue;
            if(!book[tx][ty]&&ma[tx][ty]!='#')
            {
                book[tx][ty] =  1;
                if(ma[tx][ty]== 'x')
                    now.s = ts+1;
                else now.s = ts;
                now.x = tx;
                now.y = ty;
                q.push(now);
            }
        }
    }
    return inf;
}
int main()
{
    while(cin>>n>>m)
    {
        ans = inf;
        for(int i=0; iscanf("%s",ma[i]);
        for(int i=0; ifor(int j=0; jif(ma[i][j]=='r')
                {
                    memset(book,0,sizeof(book));
                    ans  = min(bfs(i,j), ans);
                }
            }
        }
        if(ans!=inf)
            cout<else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

你可能感兴趣的:(#,BFS,赛后思过,数据结构----------)