使用优先队列的广搜

来源:互联网 发布:淘宝上的代购是正品吗 编辑:程序博客网 时间:2024/04/28 00:53

poj  1649

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


不使用优先队列维护很容易TLE。。。

不使用优先队列的TLE的深搜代码,留着以后反思。

Time Limit Exceeded1649C++2001760广搜,用优先队列实现

#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>#define maxn 205using namespace std;bool visit[maxn][maxn];char map[maxn][maxn];int dir[4][2] = {{0,-1},{0,1},{1,0},{-1,0}}; int q,p,ans,sx,sy; int way[maxn][maxn]; int qx[maxn*maxn],qy[maxn*maxn]; void dfs(int x,int y){    int i,j,nx,ny;    if(map[x][y]=='a')    {     ans=min(ans, way[x][y]);     return;    }    for(i=0;i<4;i++)    {      nx=x+dir[i][0];      ny=y+dir[i][1];      if(nx>=0&&nx<p&&ny>=0&&ny<q&&map[nx][ny]!='#'&&!visit[nx][ny])      {          visit[nx][ny]=1;          if(map[nx][ny]=='x')          {             way[nx][ny]=way[x][y]+2;          }          else           way[nx][ny]=way[x][y]+1;          dfs(nx,ny);          visit[nx][ny]=0;      }    }}int main(){    int i,j,x,y;      while(scanf("%d%d",&p,&q)!=EOF)        {            for(i=0;i<p;i++)        for(j=0;j<q;j++)       {           cin>>map[i][j];           if(map[i][j]=='r')           {               sx=i;sy=j;           }       }       memset(visit,0,sizeof(visit));       memset(way,0,sizeof(way));       visit[sx][sy]=1;        ans=1000000000;        dfs(sx,sy);        if(ans==1000000000)        printf("Poor ANGEL has to stay in the prison all his life.\n");        printf("%d\n",ans);        }    return 0;}


使用优先队列的AC的代码:

Accepted1649C++10228

#include <iostream>#include <functional>#include <queue>#include <stdio.h>using namespace std;class Node{    public :        int x, y, time;        friend bool operator < (const Node &a, const Node &b)        {            return a.time > b.time;        }};priority_queue <Node> Q;Node first, next;char map[201][201];int n, m, a, b;int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};int bfs(){    first.x = a;    first.y = b;    first.time = 0;    Q.push(first);    int fx, fy;    while (!Q.empty())    {        first = Q.top();        Q.pop();        int i;        for (i = 0; i < 4; ++i)        {            fx = first.x + dir[i][0];            fy = first.y + dir[i][1];            if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '#')            {                if (map[fx][fy] == 'r')                {                    return first.time + 1;                }                next.x = fx;                next.y = fy;                if (map[fx][fy] == '.')                {                     map[fx][fy] = '#';                     next.time = first.time + 1;                     Q.push(next);                }                else if (map[fx][fy] == 'x')                {                    map[fx][fy] = '#';                    next.time = first.time + 2;                    Q.push(next);                }            }        }    }    return -1;}int main(){    int cnt;    while (scanf("%d %d", &n, &m) != EOF)    {        int i, j;        for (i = 0; i < n; ++i)        {            getchar();            for (j = 0; j < m; ++j)            {                scanf("%c", &map[i][j]);                if (map[i][j] == 'a')                {                    map[i][j] = '#';                    a = i;                    b = j;                }            }        }        while (!Q.empty())        {            Q.pop();        }        cnt = bfs();        if (cnt == -1)        {            printf("Poor ANGEL has to stay in the prison all his life.\n");        }        else        {            printf("%d\n", cnt);        }    }    return 0;}


原创粉丝点击