广度优先搜索C++练习题HDU 1242 Rescue

Rescue
Time Limit: 2000/1000 MS ( Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10194    Accepted Submission(s): 3728
 
 
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
解题思路:记忆化的BFS,利用一个数组来维护到固定点的最小值,但是这一题有一个坑,就是主人公的朋友可能有多个,这样我们必须从主人公出发去寻找他到所有朋友处的最小值,就是答案。
[cpp]  
#include<iostream>  
#include<cstring>  
#include<queue>  
using namespace std;  
#define INF 99999  
char maze[205][205];          //迷宫的二维数组 
long len[205][205];           //从a点到任意可行点的最短距离
int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//R,L,D,U,四种走法      
void BFS(int xa,int ya)       //以(xa,ya)为根节点开始进行BFS
{  
    int x0,y0,j,p,q;          //(x0,y0)存储每次的可扩展点坐标
    queue<int> x;queue<int> y;  //x,y队列分别保存可扩展点的x,y坐标
    x.push(xa);y.push(ya);      //第一步先把根节点放入扩展队列中
    while(!x.empty()&&!y.empty())   //可扩展点表为空时,搜索结束
    {  
        x0=x.front();y0=y.front();    //每次取出可扩展点,准备进行扩展
        x.pop();y.pop();  
        for(j=0;j<4;j++)  
        {  
            //以下带有一种剪枝,即只在有更短路径的时候将这一路径加入队列进行搜索 
            //如果搜索到的这一点的原先路径比从搜索点走更远,或者该点尚未搜索到,就更改到这一点的最短距离  
            p=x0+mov[j][0];q=y0+mov[j][1];  
            if(maze[p][q]=='.'||maze[p][q]=='r'||maze[p][q]=='x')  
            {  
                if(maze[p][q]=='x')//遇到守卫,直接干掉看守比绕路更快
                {  
                    if(len[x0][y0]+2<len[p][q]||len[p][q]==0)    //若按照当前走到(p,q)的方式的耗散值比原先到达(p,q)的耗散值低,(p,q)还没被设置耗散值
                    {  
                        len[p][q]=len[x0][y0]+2;  
                        x.push(p);y.push(q);  
                    }  
                }  
                else if(len[x0][y0]+1<len[p][q]||len[p][q]==0)
                {  
                    len[p][q]=len[x0][y0]+1;  
                    x.push(p);y.push(q);  
                }  
            }  
        }  
    }  
}  
int main()  
{  
    int m,n,i,j,a,b;  
    long ans;  
    while(cin>>m>>n)  
    {  
        ans=INF;  
        memset(maze,'*',sizeof(maze));  
        memset(len,0,sizeof(len));  
        for(i=1;i<=m;i++)  
            for(j=1;j<=n;j++)  
            {  
                cin>>maze[i][j];  
                if(maze[i][j]=='a')  
                {  
                    a=i;b=j;  
                }  
            }  
        BFS(a,b);  

	//选择这样的点:值r,且到这个点的最小距离len[i][j]最小
	//该点的len值为答案
        for(i=1;i<=m;i++)  
            for(j=1;j<=n;j++)  www.2cto.com
                if(maze[i][j]=='r'&&ans>len[i][j]&&len[i][j])//len为0说明走不到这一点  
                    ans=len[i][j];  
        if(ans==INF)//ans未能更新说明主人公碰不到他的朋友(们)  
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;  
        else  
            cout<<ans<<endl;  
    }  
    return 0;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值