您的位置:首页 > 产品设计 > UI/UE

Rescue

2016-04-24 21:41 323 查看
[align=left]Problem Description[/align]
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.<br><br>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.<br><br>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.)<br>
 

[align=left]Input[/align]
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the
file.<br>
 

[align=left]Output[/align]
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." <br>
 

[align=left]Sample Input[/align]

7 8<br>#.#####.<br>#.a#..r.<br>#..#x...<br>..#..#.#<br>#...##..<br>.#......<br>........<br>

 

[align=left]Sample Output[/align]

13<br>

 

[align=left]Author[/align]
CHEN, Xue
 

[align=left]Source[/align]
ZOJ Monthly, October 2003
 
简单题意:

 天使被关在一个N*M的矩阵,现在天使的朋友需要拯救天使,假设移动上、下、左、右需要花费一个单位的时间,并且杀害一名警卫也需要一个单位的时间。现在需要编写一个程序,求出营救天使最少的时间。

解题思路形成过程:

  这是BFS优先队列的题型。确实没有太大的难度。

感想:

  课上的例题,对做题确实有很大的帮助。

AC代码:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

using namespace std;

struct node

{

    int x,y,step;

    friend bool operator<(node n1,node n2)

    {

        return n2.step<n1.step;

    }

};

int n,m,vis[205][205];

char map[205][205];

int x1,x2,y1,y2;

int to[4][2] = {1,0,-1,0,0,1,0,-1};

int check(int x,int y)

{

    if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || map[x][y] == '#')

        return 1;

    return 0;

}

int bfs()

{

    int i;

    priority_queue<node> Q;

    node a,next;

    a.x = x1;

    a.y = y1;

    a.step = 0;

    Q.push(a);

    vis[x1][y1] = 0;

    while(!Q.empty())

    {

        a = Q.top();

        Q.pop();

        if(a.x == x2 && a.y == y2)

            return a.step;

        for(i = 0; i<4; i++)

        {

            next = a;

            next.x+=to[i][0];

            next.y+=to[i][1];

            if(check(next.x,next.y))

                continue;

            next.step++;

            if(map[next.x][next.y] == 'x')

                next.step++;

            if(vis[next.x][next.y]>=next.step)

            {

                vis[next.x][next.y] = next.step;

                Q.push(next);

            }

        }

    }

    return 0;

}

int main()

{

    int i,j;

    while(~scanf("%d%d",&n,&m))

    {

        for(i = 0; i<n; i++)

        {

            scanf("%s",map[i]);

            for(j = 0; map[i][j]; 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));

        int ans = 0;

        ans = bfs();

        if(ans)

            printf("%d\n",ans);

        else

            printf("Poor ANGEL has to stay in the prison all his life.\n");

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm