hdu 1424 Rescue

Rescue

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


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
 

Author
CHEN, Xue
 

Source
 

题意:求从a到r用的最短时间

思路: ①bfs+优先队列,保证每次出队的都是到源点最短的时间。至于一般的队列也可以过,那是因为数据太水了...

②bfs+每个点可以入队多次,对队首的点进行上下左右搜索,若符合条件(代码中说明)就更新,看不在队列中,则加入队列

思路①明显要比②优,对于①只需要搜到终点就可以了,但是对于②则需要访问到无法访问为止

①网上大牛的代码

#include <iostream>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
char map[205][205];
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1}; 
bool visit[205][205];
int n,m;
struct point
{
	int x;
	int y;
	int step;
	friend bool operator < (point a, point b)
	{
		return a.step > b.step;
	}
}a,b;

int bfs()
{
	int flag = 0;
	memset(visit,false,sizeof(visit));
	visit[a.x][a.y] = true;
	priority_queue<point> q;
	q.push(a);
	while(!q.empty())
	{
		
		b = q.top();
		q.pop();
		if(map[b.x][b.y] == 'r')
		{
			flag = b.step;
			break;
		}	
		for(int i = 0; i < 4; i++)
		{
			a.x = b.x + xx[i];
			a.y = b.y + yy[i];
			a.step = b.step + 1;
			if(!visit[a.x][a.y]&&map[a.x][a.y]!='#')
			{
				visit[a.x][a.y] = true;
				if(map[a.x][a.y]=='x')
					a.step = a.step + 1;
				q.push(a);
			}
		}
	}
	return flag;
}
int main()
{
	while(cin>>n>>m)
	{
		for(int i=0;i<=n+1;++i)
            map[i][0]=map[i][m+1]='#';
        for(int i=0;i<=m+1;++i)
            map[0][i]=map[n+1][i]='#';
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= m; j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='a')
				{
					a.x = i;
					a.y = j;
					a.step = 0;
				}
			}
		}
		int step = bfs();
		if(step)
		{
			cout<<step<<endl;;
		}	
		else
		{
			cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;;
		}
	}
	return 0;
	
}
/*
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
*/

②我的代码

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
int G[250][250];
struct qq
{
    int x,y;
}q[40010];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int dis[250][250];
int inq[250][250];
int rx,ry;
int bfs(int x,int y)
{
    memset(dis,0x3f,sizeof(dis));
    memset(inq,0,sizeof(inq));
    int front=0,rear=1;
    q[0].x=x,q[0].y=y;
    inq[x][y]=1;
    dis[x][y]=0;
    while(front<rear)
    {
        qq temp=q[front++];
        for(int i=0;i<4;i++)
            if(G[temp.x+dx[i]][temp.y+dy[i]])//判断是否可达
            {
                if(dis[temp.x+dx[i]][temp.y+dy[i]]>dis[temp.x][temp.y]+G[temp.x+dx[i]][temp.y+dy[i]])//判断是否更新
                {
                    dis[temp.x+dx[i]][temp.y+dy[i]]=dis[temp.x][temp.y]+G[temp.x+dx[i]][temp.y+dy[i]];
                    if(!inq[temp.x+dx[i]][temp.y+dy[i]])//判断是否在队列中
                    {
                        q[rear].x=temp.x+dx[i];
                        q[rear++].y=temp.y+dy[i];
                        inq[temp.x+dx[i]][temp.y+dy[i]]=1;//入队
                    }
                }
            }
        inq[temp.x][temp.y]=0;//把状态改为不在队列
    }
    if(dis[rx][ry]<INF)
        return dis[rx][ry];
    return -1;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        int ax,ay;
        for(i=0;i<=n+1;i++)
            G[i][0]=G[i][m+1]=0;
        for(j=0;j<=m+1;j++)
            G[0][j]=G[n+1][j]=0;
        for(i=1;i<=n;i++)
        {
            char s[210];
            scanf("%s",s);
            for(j=0;j<m;j++)
            {
                G[i][j+1]=(s[j]=='#'?0:s[j]=='.'?1:2);
                if(s[j]=='a')
                {
                    ax=i;
                    ay=j+1;
                    G[i][j+1]=0;
                }
                if(s[j]=='r')
                {
                    rx=i;
                    ry=j+1;
                    G[i][j+1]=1;
                }
            }
        }
        int ans=bfs(ax,ay);
        if(ans<0)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值