Rescue HDU杭电1242【广搜+优先队列】

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
 


#include
#include
#include
#include
using namespace std;
char map[220][220];
bool vis[220][220];
int m,n;
int ex,ey;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct rescue
{
    int x,y;
    int time;
    friend bool operator < (rescue a,rescue b)
    {
        return a.time>b.time;//小的先出队列 
    }
};

bool judge(rescue a)
{
    if(a.x>m||a.x<1||a.y>n||a.y<1||map[a.x][a.y]=='#'||vis[a.x][a.y]) return 0;

    return 1;
}
int BFS(int sx,int sy)
{
    rescue pos,next;
    priority_queueq;
    pos.x=sx;
    pos.y=sy;
    pos.time=0;
    vis[sx][sy]=1;
    q.push(pos);
    while(!q.empty())
    {
        pos=q.top();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=pos.x+dx[i];
            next.y=pos.y+dy[i];                                                    
            if(judge(next))
            {
                if(map[next.x][next.y]=='x') next.time = pos.time+2;
                else next.time = pos.time+1;
                if(next.x==ex && next.y==ey)
                {
                    return next.time;
                     
                }                
                vis[next.x][next.y]=1;
                q.push(next);
            }                                                                        
        }
    }    
    return -1;
}
int main()
{

    int x,y;
    while(~scanf("%d%d",&m,&n))
    {
        //int num=0;
        getchar();
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%c",map[i]+j);
                if(map[i][j]=='r')
                {
                    x=i,y=j;
                }
                else if(map[i][j]=='a')
                {
                    ex=i;ey=j;
                }
                //else if(map[i][j]=='x') ++num;
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        int min_time;
        min_time=BFS(x,y);
        if(min_time==-1)
        	printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
        	printf("%d\n",min_time);
    }
    return 0;
}


#include
#include
#include
#define INF 0xfffffff
using namespace std;
char map[220][220];
int m,n;
int ex,ey;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
int min_time;
struct rescue
{
    int x,y;
    int time;
    friend bool operator < (rescue a,rescue b)
    {
        return a.time>b.time;//小的先出队列 
    }
};

bool judge(rescue a)
{
    if(a.x>m||a.x<1||a.y>n||a.y<1||map[a.x][a.y]=='#') return 0;
//    if(a.time>=min_time) return 0;
    return 1;
}
int BFS(int sx,int sy)
{
    rescue pos,next;
    priority_queueq;
    pos.x=sx;
    pos.y=sy;
    pos.time=0;
    map[sx][sy]='#';
    q.push(pos);
    while(!q.empty())
    {
        pos=q.top();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=pos.x+dx[i];
            next.y=pos.y+dy[i];                                        
            if(map[next.x][next.y]=='x') next.time = pos.time+2;
            else next.time = pos.time+1;
            
            if(judge(next))
            {
                if(next.x==ex && next.y==ey)
                {
                    min_time=next.time;
                    return min_time;
                }
                map[next.x][next.y]='#';
                q.push(next);
            }                                                                        
        }
    }
	return -1;    
}
int main()
{

    int x,y;
    while(~scanf("%d%d",&m,&n))
    {
        //int num=0;
        getchar();
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%c",map[i]+j);
                if(map[i][j]=='r')
                {
                    x=i,y=j;
                }
                else if(map[i][j]=='a')
                {
                    ex=i;ey=j;
                }
                //else if(map[i][j]=='x') ++num;
            }
            getchar();
        }
        min_time = BFS(x,y);
        if(min_time==-1)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
        	printf("%d\n",min_time);
    }
    return 0;
}


你可能感兴趣的:(#,BFS,#,HDOJ)