我是靠谱客的博主 土豪短靴,最近开发中收集的这篇文章主要介绍hdu 1242 Rescue(bfs+优先队列)Rescue,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Rescue

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


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
ZOJ Monthly, October 2003

Recommend
Eddy
 
 
 
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int vis[205][205];
char maps[205][205];   //char啊!!!
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int ax,ay;
int ans;
int m,n;

struct node
{
    int x;
    int y;
    int value;
    bool operator <(const node &a)const    //所需时间大的优先级小
    {
        return value>a.value;
    }
};

bool isok(node &a)
{
    if(a.x<m&&a.x>=0&&a.y>=0&&a.y<n&&maps[a.x][a.y]!='#')
        return true;
    return false;
}

void bfs()
{
    node cur,tmp;
    cur.x=ax;
    cur.y=ay;
	cur.value=0;
    vis[ax][ay]=1;
    priority_queue<node> q;
    //while(!q.empty())
        //q.pop();
    q.push(cur);
    while(!q.empty())
    {
       cur=q.top();
       q.pop();
       for(int i=0;i<4;i++)
       {
		   tmp=cur;
           int xx=tmp.x+dx[i];
           int yy=tmp.y+dy[i];
           if(!isok(tmp)||vis[xx][yy])
                continue;
           else
           {
			    tmp.x=xx;
				tmp.y=yy;
				vis[xx][yy]=1;
                if(maps[xx][yy]=='x')
					tmp.value+=2;
                else /*if(maps[tmp.x][tmp.y]=='.')*/
					tmp.value+=1;
				if(maps[tmp.x][tmp.y]=='r')
				{
                    printf("%dn",tmp.value);
					return;
				}
                q.push(tmp);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.n");
}

int main()
{
    int i,j;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
		getchar();
		memset(vis,0,sizeof(vis));
		memset(maps,0,sizeof(maps));
        for(i=0;i<m;i++)
            scanf("%s",maps[i]);
        for(i=0;i<m;i++)
		{
            for(j=0;j<n;j++)
            {
                if(maps[i][j]=='a')
                {
                    ax=i;
                    ay=j;
                }
            }
			//getchar();
		}
		
        bfs();
    }
    return 0;
}

//队列在子函数中定义不需要清空

 
题意:
天使被关在牢里,要她的朋友来拯救她。。。牢房里有路,有看守。碰到看守要花一分钟的时间杀掉,每走一步又要花一分钟。
问她的朋友救到她所需的最小时间。   注意:她可能有多个朋友。
 
分析:
由于可能有多个朋友,所以从天使着手倒着搜,让她去找她的朋友。碰到看守value+=2,否则就+1。找到以后就输出value。
 
注意:
1、如果是直接用scanf("%c",&a);读入数据,两重循环中每一列读完了要用getchar()过滤掉回车。
      但是,如果是先用scanf("%s",a[])读入以后再处理就不用getchar()。

最后

以上就是土豪短靴为你收集整理的hdu 1242 Rescue(bfs+优先队列)Rescue的全部内容,希望文章能够帮你解决hdu 1242 Rescue(bfs+优先队列)Rescue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部