当前位置:编程学习 > C/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];  
int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//R,L,D,U  
void BFS(int xa,int ya)  
{  
    int x0,y0,j,p,q;  
    queue<int> x;queue<int> 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)  
                    {  
                        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);  
        for(i=1;i<=m;i++)  
            for(j=1;j<=n;j++)  www.zzzyk.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;  
}  
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,