HDU 1242 - Rescue(BFS+优先队列)

来源:互联网 发布:lol域名不能备案 编辑:程序博客网 时间:2024/06/05 13:25

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26677 Accepted Submission(s): 9446

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

题意:
a的起点,r是终点,#是墙, . 是路.走路单位长度1个单位时间,路过x两个单位时间.
求从起点到终点到终点的最短时间.

解题思路:
在结构体上操作,结构体包含坐标和时间,在BFS的时候将结构体进优先队列.

AC代码

#include<stdio.h>#include<memory.h>#include<queue>#define maxn 205using namespace std;struct node{    int x,y,step;    friend bool operator<(node n1,node n2)    {        return n2.step<n1.step;    }};int n;int m;char qdu[maxn][maxn];int visit[maxn][maxn];int x1;int x2;int y1;int y2;int dir[4][2] = {-1,0,1,0,0,1,0,-1};int check(int x,int y){    if(x < 0 || x >= n || y < 0 || y >= m || !visit[x][y] || qdu[x][y] == '#')        return 0;    return 1;}int bfs(){    priority_queue<node>Q;    node a,next;    a.x = x1;    a.y = y1;    a.step = 0;    Q.push(a);    visit[x1][y1] = 0;    while(!Q.empty())    {        a = Q.top();        Q.pop();        if(a.x == x2 && a.y == y2)            return a.step;        for(int i = 0;i < 4;i++)        {            next = a;            next.x = a.x + dir[i][0];            next.y = a.y + dir[i][1];            if(!check(next.x,next.y))                continue;            next.step = a.step+1;            if(qdu[next.x][next.y] == 'x')                next.step++;            if(visit[next.x][next.y] > next.step)            {                visit[next.x][next.y] = next.step;                Q.push(next);            }        }    }    return 0;}int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i = 0;i < n;i++)            {                scanf("%s",qdu[i]);                for(int j = 0;j < m;j++)                {                    if(qdu[i][j] == 'r')                    {                        x1 = i;                        y1 = j;                    }                    else if(qdu[i][j] == 'a')                    {                        x2 = i;                        y2 = j;                    }                }            }        memset(visit,1,sizeof(visit));        int ans = bfs();        if(ans)            printf("%d\n",ans);        else            printf("Poor ANGEL has to stay in the prison all his life.\n");    }    return 0;}
0 0
原创粉丝点击