HDU1242——Rescue【BFS】

本文深入探讨了广度优先搜索(BFS)算法的基本原理及其在解决复杂路径寻找问题中的应用。通过一个具体的囚犯救援问题,展示了如何利用BFS算法高效地找到从起点到终点的最短路径。文章不仅提供了详细的算法步骤,还附带了完整的代码实现,帮助读者理解并掌握BFS算法的实际操作。

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

Rescue

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


 

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

 

 

Author

CHEN, Xue

 

 

Source

ZOJ Monthly, October 2003

 

 

Recommend

Eddy   |   We have carefully selected several similar problems for you:  1240 1016 1010 1072 1241 

 

 

Statistic | Submit | Discuss | Note

BFS裸题,直接上模版套就可以了。

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define INF 1000000
using namespace std;
const int MAXN=200+10;
int sx,sy,ex,ey;//起始点,终点坐标
int n,m;
char mp[MAXN][MAXN];
int d[MAXN][MAXN];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
void BFS(){
    queue<P> q;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++) d[i][j]=INF;
    }
    q.push(P(sx,sy));
    d[sx][sy]=0;
    while(!q.empty()){
        P p=q.front();q.pop();
        if(p.first==ex&&p.second==ey) break;
        for(int i=0;i<4;i++){
            int nx=p.first+dx[i],ny=p.second+dy[i];
            if(nx>=0&&nx<n&&ny>=0&&ny<m&&mp[nx][ny]!='#'&&d[nx][ny]==INF){
                if(mp[nx][ny]=='.'||mp[nx][ny]=='r') d[nx][ny]=d[p.first][p.second]+1;
                else d[nx][ny]=d[p.first][p.second]+2;
                q.push(P(nx,ny));
            }
        }
    }
    if(d[ex][ey]==INF) printf("Poor ANGEL has to stay in the prison all his life.\n");
    else printf("%d\n",d[ex][ey]);
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<n;i++) scanf("%s",mp[i]);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(mp[i][j]=='a'){
                    sx=i;
                    sy=j;
                }
                if(mp[i][j]=='r'){
                    ex=i;
                    ey=j;
                }
            }
        }
        BFS();
    }
    return 0;
}

 

内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值