zoj 1649 Rescue (bfs+队列)

Rescue


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

队列广搜,用一个数组记录走到当前位置的最少时间,以此判断是否把当前状态入队。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 205
#define inf 100000000
int ei,ej,n,m;  //目标位置和行列范围
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int mark[N][N];  //记录走到当前位置所用最少时间
char g[N][N];
struct node
{
    int x,y,t;
};
int bfs(int x,int y)
{
    int i;
    queue<node >q;
    node cur,next;
    cur.x=x;
    cur.y=y;
    cur.t=0;
    q.push(cur);
    mark[x][y]=0;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            next.x=x=dir[i][0]+cur.x;
            next.y=y=dir[i][1]+cur.y;
            if(x<0||x>=n||y<0||y>=m||g[x][y]=='#')
                continue;
            next.t=cur.t+1;
            if(g[x][y]=='x')
                next.t++;
            if(next.t<mark[x][y])
            {
                mark[x][y]=next.t;
                q.push(next);
            }
        }
    }     //因为没用优先队列,故只有队列空之后才能得到最优解
    if(mark[ei][ej]!=inf)
        return mark[ei][ej];
    return -1;
}
int main()
{
    int i,j,si,sj;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",g[i]);
            for(j=0;j<n;j++)
            {
                mark[i][j]=inf;  //初始化为无限大
                if(g[i][j]=='a')
                {
                    si=i;sj=j;
                }
                if(g[i][j]=='r')
                {
                    ei=i;ej=j;
                }
            }
        }
        int t=bfs(si,sj);
        if(t==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",t);
    }
    return 0;
}

zoj 1649 Rescue (bfs+队列)

时间: 2024-10-12 12:53:19

zoj 1649 Rescue (bfs+队列)的相关文章

zoj 1649 Rescue

Rescue 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

ZOJ 1649 Rescue(有敌人迷宫BFS)

题意 求迷宫中从a的位置到r的位置需要的最少时间  经过'.'方格需要1s  经过'x'方格需要两秒  '#'表示墙 由于有1s和2s两种情况  需要在基础迷宫bfs上加些判断 令到达每个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描完全图就得到答案咯 #include<cstdio> #include<cstring> #include<queue> using namespace std; const

HDU 1242 &amp;&amp; ZOJ 1649( BFS (队列 || 优先队列)).

~~~~ 突然发现一篇搜索的题目都有写.昨天发现道bfs题目,HDU上AC, ZOJ上WA.不得不说HDU上的数据之水.. 今天早起刷题有了思路,并用队列和单调队列都写了一遍,0MS飘过~~ ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 ~~~~ 首先有坑的地方是friends,对嘛,朋友有很多,ang

ZOJ 1649 &amp;&amp; HDU 1242 Rescue (BFS + 优先队列)

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

ZOJ 1649 &amp;&amp; HDU 1242 Rescue

Rescue 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

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

zoj 1649

//hnldyhy(303882171) 11:12:46// zoj 1649 //bfs +优先队列 #include <stdio.h>#include <iostream>#include <queue>using namespace std;struct node{ int x; int y; int step;}; bool operator<(const node &a,const node &b){ if (a.step>b.

ZOJ 2724 Windows 消息队列 (优先队列)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text cha

poj 1465 &amp; zoj 1136 Multiple (BFS+余数重判)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6177   Accepted: 1346 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small