HDU1242 Rescue-BFS,优先队列

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


因为没有给friend的数目,所以要从a找r。


虽说是优先队列题,但是不这么做也能A

非优先:

#include 
#include 

#define inf 40010

int min;
char map[210][210];
int z[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int find(int i,int j,int time)
{
    if(map[i][j]=='X'||time>min) return 0;
    if(map[i][j]=='r')
    {
        if(min>time) min=time;
        return 0;
    }
    if(map[i][j]=='x')
    for(int x=0;x<4;++x)
    {
        map[i][j]='#';
        find(i+z[x][0],j+z[x][1],time+2);
        map[i][j]='x';
    }
    if(map[i][j]=='.')
    for(int x=0;x<4;++x)
    {
        map[i][j]='#';
        find(i+z[x][0],j+z[x][1],time+1);
        map[i][j]='.';
    }
    return 0;
}

int main()
{
    int si,sj,n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        memset(map,'#',sizeof(map));
        for(int i=2;i	//一定要从a找r;
            }
            getchar();
        }
 //       printf("%d\n",min);
        min=inf;
        find(si,sj,0);
        if(min==inf) puts("Poor ANGEL has to stay in the prison all his life.");
        else printf("%d\n",min);
    }
    return 0;
}



优先队列:


#include
#include
#include

#define inf 400010
using namespace std;

char map[210][210];
int mark[210][210];
int z[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

struct node
{
	int x,y,time;
} ;
struct cmp
{
	bool operator()(const node &a,const node &b)
	{
		return a.time>b.time;
	}
};
int find(node first)
{
    node now,next;
    priority_queue,cmp> q;
    q.push(first);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=now.x+z[i][0];
            next.y=now.y+z[i][1];
            if(map[next.x][next.y]=='#'||mark[next.x][next.y]) continue;
            mark[next.x][next.y]=1;		//这里不知为何,用map[next.x][next.y]='#';标记会错
            next.time=now.time+1;
            if(map[next.x][next.y]=='r') return next.time;
            if(map[next.x][next.y]=='x') ++next.time;
            q.push(next);
        }
    }
    return inf;
}

int main()
{
    int si,sj,n,m,min;
    node first;
//    freopen("F://cs.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        memset(map,'#',sizeof(map));
        memset(mark,0,sizeof(mark));
        for(int i=2;i


你可能感兴趣的:(搜索,HDU)