HDU 1242【DFS/BFS】

Rescue

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


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


题解:题目其实很简单,但是容易出错。从Angel的位置开始找,四个方向,每次从一个方向搜到底,如果没找到就换一个方向找,直到找完四个方向,若是还是没有找到就输出“Poor ANGEL has to stay in the prison all his life.” 。找到了注意更新最短的距离,因为可能存在多条路径,另外本题还可用BFS做,网上大部分的题解都是BFS加优先队列做的,我就不贴了。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define INF 99999999
using namespace std;
const int maxn=205;
char s[maxn][maxn];
int vis[maxn][maxn];
int minx=INF;//刚开始置为无穷大
int n,m;
int dir[4][2]={0,1,-1,0,0,-1,1,0};//四个方向
void dfs(int x,int y,int step){
    if(s[x][y]=='x') step++;//打死保安花费一分钟
    if(s[x][y]=='r'){//找到她的朋友之后还要判断当前路径是否是耗时最短的
        if(step<minx)
            minx=step;
        return ;
    }
    for(int i=0;i<4;i++){
        int tx=x+dir[i][0];
        int ty=y+dir[i][1];
        if(tx<0||tx>=n||ty<0||ty>=m)//边界判断
            continue;
        if(!vis[tx][ty]&&s[tx][ty]!='#'){
            vis[tx][ty]=1;
            dfs(tx,ty,step+1);
            vis[tx][ty]=0;//此路不通或者找到之后要从其他方向尝试看能不能找到或者看能不能耗时更短。因此需要将以前走过的路标记为没有走过。
        }
    }
    return;
}
int main(){
    while(~scanf("%d %d",&n,&m)){
        minx=INF;
        memset(vis,0,sizeof vis);
        int sx,sy;
        for(int i=0;i<n;i++){
            scanf("%s",s[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='a'){
                    sx=i,sy=j;
                    break;
                }
            }
        }
        vis[sx][sy]=1;
        dfs(sx,sy,0);
        if(minx!=INF) printf("%d
",minx);
        else printf("Poor ANGEL has to stay in the prison all his life.
");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kzbin/p/9205233.html