HDU 1242(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

题意:Angel(a)被关在监狱,Angel的朋友(r)要去解救他,遇到警卫(x)需要花费2s才能过去,问解救他花费最少的时间数,如果不能解救的话输出 Poor ANGEL has to stay in the prison all his life.
思路:倒着想,让Angel去找离他最近的那个朋友就行,用bfs和dfs都行,因为地图最大200×200,所以dfs不会超时,bfs的话得用优先队列,因为那个警卫的步数是两步,优先队列排序的话和平时用的排序正好是相反的,return a.step>b.step 是按从小到大的顺序排的

dfs

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,mov[4][2]={0,-1,0,1,-1,0,1,0};
int mark,vis[205][205];
char v[205][205];
void dfs(int x,int y,int step)
{
    if(step>mark)
        return ;
    if(v[x][y]=='r')
    {
        if(step<mark)
        {
            mark=step;
        }
        return ;
    }
    for(int a=0;a<4;a++)
    {
        int i=x+mov[a][0];
        int j=y+mov[a][1];
        if(v[i][j]=='#'||v[i][j]=='a'||i<0||j<0||i>n-1||j>m-1||vis[i][j]==1)
         continue;
        vis[i][j]=1;
         if(v[i][j]=='x')
         {
             dfs(i,j,step+2); 
         }
         else
         {
             dfs(i,j,step+1);
         }
         vis[i][j]=0; //回溯
    }
}
int main()
{
   while(scanf("%d%d",&n,&m)!=EOF)
   {
       int sx,sy;
       for(int a=0;a<n;a++)
   {
       scanf("%s",v[a]);
   }
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           if(v[i][j]=='a')
           {
               sx=i,sy=j;
               break;
           }
       }
   }
   mark=800000;
   memset(vis,0,sizeof(vis));
    dfs(sx,sy,0);
   if(mark==800000)
   {
       printf("Poor ANGEL has to stay in the prison all his life.\n");
   }
   else
   {
       printf("%d\n",mark);
   }
   }
    return 0;
}

BFS

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,sx,sy,mark,mov[4][2]={0,-1,0,1,-1,0,1,0},vis[205][205];
char mapp[205][205];
struct lsz
{
    int x,y;
    int step;
};
 bool operator<(const lsz &a,const lsz &b)  //优先队列排序
    {
        return a.step>b.step;
    }
void bfs(int x,int y,int step)
{
    priority_queue<lsz>q;
    lsz j,k;
    j.x=x,j.y=y,j.step=step;
    q.push(j);
    while(!q.empty())
    {
        j=q.top();
        q.pop();
        if(mapp[j.x][j.y]=='r')
        {
            mark=j.step;
            return ;
        }
        for(int a=0;a<4;a++)
        {
            k.x=j.x+mov[a][0];
            k.y=j.y+mov[a][1];
            if(k.x<0||k.y<0||k.x>=n||k.y>=m||mapp[k.x][k.y]=='#'||mapp[k.x][k.y]=='a'||vis[k.x][k.y]==1)
                continue;
                vis[k.x][k.y]=1;
            if(mapp[k.x][k.y]=='x')
            {
                k.step=j.step+2;
                q.push(k);
            }
            else
            {
                k.step=j.step+1;
                q.push(k);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        for(int a=0;a<n;a++)
        {
            scanf("%s",mapp[a]);
            for(int b=0;b<m;b++)
            {
                if(mapp[a][b]=='a')
                {
                    sx=a,sy=b;
                    break;
                }
            }
        }
        mark=800000;
        vis[sx][sy]=1;
        bfs(sx,sy,0);
        if(mark==800000)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",mark);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值