4.2.3--广搜--Rescue

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
题意:地图中起点为’r’,终点为’a’,’#’是不能走的,经过’.’花1分钟,经过’x’花2分钟,问从起点最少多少时间到终点,注意对守卫的处理,虽然用深搜思路比较简单,但会超时,

代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y,step;
};
char a[200][201];
int n,m,cnt,bx,by;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool can[200][201];
void bfs()
{
    queue<node> Q;
    node p,t;
    p.x=bx; p.y=by; p.step=0;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(a[p.x][p.y]=='a') {cnt=p.step; return;}
        else if(a[p.x][p.y]=='x')
        {
            a[p.x][p.y]='.';
            t.x=p.x; t.y=p.y;
            t.step=p.step+1;
            Q.push(t);
            continue;  //因为干掉守卫需要一个战斗时间,
           //故要把搜索步数延迟一步,让其他更优的搜索先搜,以防出错。
        }
        for(int k=0;k<4;k++)
        {
            t.x=p.x+dx[k];
            t.y=p.y+dy[k];
            if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m){
            if(can[t.x][t.y]==false&&a[t.x][t.y]!='#')
            {
                can[t.x][t.y]=true;
                t.step=p.step+1;
                Q.push(t);
            }}
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='r')
                {
                    bx=i; by=j;
                }
                can[i][j]=false;
            }
        }
        can[bx][by]=true; cnt=-1;
        bfs();
        if(cnt!=-1) printf("%d\n",cnt);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

深搜代码:

#include<bits/stdc++.h>
using namespace std;
#define maxx 0x7fffffff
char a[200][201];
int n,m,cnt;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool can[200][201];
bool ok(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m) return true;
    return false;
}
void bfs(int x,int y,int step)
{
    if(a[x][y]=='a') {if(cnt>step) cnt=step; return;}
    for(int k=0;k<4;k++)
    {
        int xx=x+dx[k];
        int yy=y+dy[k];
        if(ok(xx,yy)==false) continue;
        if(a[xx][yy]!='#'&&can[xx][yy]==false)
        {
            can[xx][yy]=true;
            if(a[xx][yy]=='x')
                bfs(xx,yy,step+2);
            else bfs(xx,yy,step+1);
            can[xx][yy]=false;

        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int i,j,x,y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='r')
                {
                    x=i; y=j;
                }
                can[i][j]=false;
            }
        }
        can[x][y]=true; cnt=maxx;
        bfs(x,y,0);
        if(cnt!=maxx) printf("%d\n",cnt);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值