Rescue(HDU-1242)

本文介绍了一种基于广度优先搜索(BFS)的游戏算法,旨在营救被囚禁的天使。通过天使寻找朋友而非朋友寻找天使的方式简化了问题。文章详细展示了如何利用队列结构实现搜索过程,并对路径中可能出现的障碍进行了特别处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

    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可能不止一个,因此从朋友找天使的角度来说较为繁琐,故以天使找朋友的角度来搜索即可。

Source Program

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int n,m;
char maps[201][201];
int used[201][201];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//方向数组

struct node
{
    int x;
    int y;
    int step;
}start,endd;

bool judge(int x,int y)//越界判断
{
    if(x<0||y<0||x>=n||y>=m||used[x][y]||maps[x][y]=='#')
        return false;
    else
        return true;
}
void bfs()
{
    int i;
    queue <node> q;
    used[start.x][start.y]=1;
    q.push(start);//初始数据入队

    while(!q.empty())
    {
        start=q.front();
        q.pop();
        if(maps[start.x][start.y]=='r')//营救到天使,结束搜索
        {
            cout<<start.step<<endl;
            return;
        }
        if(maps[start.x][start.y]=='x')//遇到守卫
        {
            maps[start.x][start.y]='.';//进行标记,打败守卫
            start.step=start.step+1;//多花1个单位时间
            used[start.x][start.y]=1;
            q.push(start);//元素入队
        }
        for(i=0;i<4;i++)
        {
            endd.x=start.x+dir[i][0];
            endd.y=start.y+dir[i][1];
            endd.step=start.step+1;

            if(judge(endd.x,endd.y))//子节点满足条件
            {
                used[endd.x][endd.y]=1;//标记
                q.push(endd);//元素入队
            }
        }
    }
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        memset(used,0,sizeof(used));//地图清零
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>maps[i][j];
                if(maps[i][j]=='a')//记录起始位置
                {
                    start.x=i;
                    start.y=j;
                    start.step=0;
                }
            }
        }
        bfs();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值