HDU1242:Rescue bfs和dfs

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
 
       题意:求从r到a的最短路径。x表示要走两步。
       这题用dfs和bfs都能写。个人建议用bfs,因为dfs要剪枝,太麻烦了。不管用什么方法都应从a搜到r,因为r可能有多个。
  
 
    dfs
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,mn;
int v[205][205];
char map[205][205];
int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int k)
{
    if (k>=mn) return ;
    int i,j,rx,ry;
    for (i=0;i<4;i++)
    {
        rx=x+yi[i][0];
        ry=y+yi[i][1];
        if (map[rx][ry]=='r')
        {
            mn=min(mn,k+1);
            return ;
        }
        if (rx>=1&&rx<=n&&ry>=1&&ry<=m&&map[rx][ry]!='#'&&!v[rx][ry])
        {
            v[rx][ry]=1;
            if (map[rx][ry]=='x') dfs(rx,ry,k+2);
            else dfs(rx,ry,k+1);
            v[rx][ry]=0;
        }
    }
}
int main()
{
    int i,j,x,y;
    while (~scanf("%d%d",&n,&m))
    {
        for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
        {
            scanf(" %c",&map[i][j]);
            if (map[i][j]=='a')
            {
                x=i;
                y=j;
            }
        }
        mn=100000;
        memset(v,0,sizeof(v));
        dfs(x,y,0);
        if (mn==100000) printf("Poor ANGEL has to stay in the prison all his life.
");
        else printf("%d
",mn);
    }
}



bfs
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,ans;
char map[205][205];
int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct f
{
    int x,y;
    int t;
};f s,r;
int bfs(int x,int y)
{
    int i,j;
    queue<f>q;
    s.x=x;
    s.y=y;
    s.t=0;
    map[x][y]='#';
    q.push(s);
    while (!q.empty())
    {
        s=q.front();
        q.pop();
        for (i=0;i<4;i++)
        {
            r.x=s.x+yi[i][0];
            r.y=s.y+yi[i][1];
            if (map[r.x][r.y]=='r') return s.t+1;
            if (r.x>=0 && r.x<n && r.y>=0 && r.y<m && map[r.x][r.y]!='#')
            {
                if (map[r.x][r.y]=='x') r.t=s.t+2;
                else r.t=s.t+1;
                map[r.x][r.y]='#';
                q.push(r);
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,x,y;
    while (~scanf("%d%d",&n,&m))
    {
        if (n==0&&m==0) break;
        ans=0;
        for (i=0;i<n;i++)
        for (j=0;j<m;j++)
        {
            scanf(" %c",&map[i][j]);
            if (map[i][j]=='a') {x=i;y=j;}
        }
        ans=bfs(x,y);
        if (ans==-1) printf("Poor ANGEL has to stay in the prison all his life.
");
        else printf("%d
",ans);
    }
    return 0;
}

      dfs,上面同学看不懂,就加了下面这种,哎,写着么好,居然看不懂

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,mn;
int v[205][205];
int v1[205][205];
char map[205][205];
int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int k)
{
     if (x<1||x>n||y<1||y>m||map[x][y]=='#'||v[x][y]) return ;
    if (k>=mn) return ;   //剪枝,加这句怎么增加了30多ms
    if (v1[x][y]&&k>=v1[x][y]) return ;//剪枝,加这句能减少60多ms.
    else v1[x][y]=k;                   //
    if (map[x][y]=='r') mn=k;
    v[x][y]=1;
    if (map[x][y]=='x') k+=2;
    else k++;
    dfs(x+1,y,k);
    dfs(x-1,y,k);
    dfs(x,y+1,k);
    dfs(x,y-1,k);
    v[x][y]=0;

}
int main()
{
    int i,j,x,y;
    while (~scanf("%d%d",&n,&m))
    {
        for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
        {
            scanf(" %c",&map[i][j]);
            if (map[i][j]=='a')
            {
                x=i;
                y=j;
            }
        }
        mn=100000;
        memset(v,0,sizeof(v));
        memset(v1,0,sizeof(v1));
        dfs(x,y,0);
        if (mn==100000) printf("Poor ANGEL has to stay in the prison all his life.
");
        else printf("%d
",mn);
    }
}

原文地址:https://www.cnblogs.com/pblr/p/4702274.html