杭电1026——Ignatius and the Princess I(BFS)

来源:互联网 发布:欧陆风云3编年史 mac 编辑:程序博客网 时间:2024/06/06 09:38

Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166’s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166’s room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.

Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

Output
For each test case, you should output “God please help our poor hero.” if Ignatius can’t reach the target position, or you should output “It takes n seconds to reach the target position, let me show you the way.”(n is the minimum seconds), and tell our hero the whole path. Output a line contains “FINISH” after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

Sample Input
5 6
.XX.1.
..X.2.
2…X.
…XX.
XXXXX.
5 6
.XX.1.
..X.2.
2…X.
…XX.
XXXXX1
5 6
.XX…
..XX1.
2…X.
…XX.
XXXXX.

Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH

主要算法:

该题的核心算法是宽度优先搜索,即BFS。DFS和BFS一样,都是从一个节点出发,按照某种特定的次序访问图中的其他节点。不同的是,BFS使用队列存放待扩展的节点。DFS使用栈存放待扩展的节点。
还记得二叉树的BFS吗?节点的访问顺序恰好是他们到根节点的距离从小到大的排序。所以,本题的一个关键点就是每次都选取距离最短的节点进行扩展。这里使用优先队列(优先队列设置成小顶堆(当然也可设置成大顶堆),即队列的顶部是值小的数据),每次从优先队列中弹出一个节点进行扩展。每个节点的扩展都是按照上下左右四个方向的进行扩展。对以及访问过或者‘X’节点不扩展。如果扩展的节点为‘.’,则其最短路径为“上一节点”的最短路径+1。如果扩展的节点为’1’~’9’。比如‘2’,则其最短路径为“上一节点”的最短路径+1+2。

案例讲解:

假定起点在左上角,我们就从左上角开始用BFS遍历迷宫,逐步计算出起点(左上角)到每个节点的距离,以及这些最短路径上每个节点的“前一个节点”。
这里写图片描述

这里写图片描述

图中的数字即为到达该节点的最短距离,红色箭头指向该节点的“上一节点”,即该节点是有哪个节点扩展出来的。黑色曲线即画出了使得到达终点距离最小的路线。

刚开始提交总是Runtime Error 。后来发现是数组溢出。最开始时记录路径的数组只开了105的大小。考虑路径最极端的情况下,就是S形走过每个节点,可能路过100*100个节点。所以将路径数组开到了10010,就AC了。

AC代码:

# include<iostream># include<string># include<queue># include<cstdio>using namespace std;#define MAX 110class Point{    public:        int x,y,dis;    friend bool operator < (Point p1,Point p2)    {        return p1.dis>p2.dis;    }};char lab[MAX][MAX];int dist[MAX][MAX];Point pa[MAX][MAX];int visited[MAX][MAX];Point path[10010];int N,M;int bfs();int main(){    int x,y,i,j,u,x1,y1;    priority_queue <Point> q;    while(scanf("%d%d",&N,&M)!=EOF)    {        int success=bfs();        if(success)        {            int second=1,k=0;            printf("It takes %d seconds to reach the target position, let me show you the way.\n",dist[N-1][M-1]);            x=N-1;            y=M-1;            while(pa[x][y].x!=-1&&pa[x][y].y!=-1)            {                path[k].x=x;                path[k++].y=y;                Point tp=pa[x][y];                x=tp.x;                y=tp.y;            }            path[k].x=0;            path[k].y=0;            for(i=k;i>0;i--)            {                x=path[i].x;                y=path[i].y;                x1=path[i-1].x;                y1=path[i-1].y;                printf("%ds:(%d,%d)->(%d,%d)\n",second++,x,y,x1,y1);                if(lab[x1][y1]!='.')                {                    for(j=0;j<lab[x1][y1]-'0';j++)                        printf("%ds:FIGHT AT (%d,%d)\n",second++,x1,y1);                }            }        }        else        {            printf("God please help our poor hero.\n");        }        printf("FINISH\n");    }    return 0;}int bfs(){        priority_queue <Point> q;        int x,y,u,success=0,k,i,j;        for(i=0;i<N;i++)        {            scanf("%s",lab[i]);            for(j=0;j<M;j++)            {                visited[i][j]=0;            }        }        Point tp;        tp.x=0;        tp.y=0;        tp.dis=0;        q.push(tp);        visited[0][0]=1;        dist[0][0]=0;        pa[0][0].x=-1;        pa[0][0].y=-1;        while(!(q.empty()))        {            tp=q.top();            x=tp.x;            y=tp.y;            q.pop();            if(x==N-1&&y==M-1)            {                success=1;                break;            }            int dx[]={-1,1,0,0},dy[]={0,0,-1,1};            for(k=0;k<4;k++)            {                int nx=x+dx[k],ny=y+dy[k];                if(nx>=0&&nx<N&&ny>=0&&ny<M&&!visited[nx][ny]&&lab[nx][ny]!='X')                {                    dist[nx][ny]=dist[x][y]+1;                    if(lab[nx][ny]>='1'&&lab[nx][ny]<='9')                        dist[nx][ny]=dist[nx][ny]+lab[nx][ny]-'0';                    pa[nx][ny].x=x;                    pa[nx][ny].y=y;                    Point np;                    np.x=nx;                    np.y=ny;                    np.dis=dist[nx][ny];                    q.push(np);                    visited[nx][ny]=1;                }            }        }        return success;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 如果你流落到荒岛上你会怎么办 手机迅雷下载版权方不给下载怎么办 白色有彩色花纹的衣服染色了怎么办 载兰花假如下雪和打霜怎么办 皇室战争你的队友离开了对战怎么办 海岛奇兵发现求救信号第三个怎么办 海岛奇兵勋章太多对手太强怎么办 鱼为什么换缸鱼翅黑了怎么办 鱼丸捕鱼大作战换手机了怎么办 2o岁j'j小怎么办 为什么小米5s指纹不能用怎么办 cs录屏软件运行内存太大了怎么办 可是没有他我怎么办啊是什么电视剧 可是没有他我怎么办啊是哪个电视剧 手机太卡了打字打不了了怎么办 梦幻西游的将军令没有电了怎么办 将军令全部的序列号都忘记了怎么办 船员证被公司压着想自己换证怎么办 电子录入系统中无法打开影像怎么办 火车票退票后说银行退款失败怎么办 苹果4s玩游戏闪退怎么办 买了二手房原房主不迁户口怎么办 苹果禁反忘记工id密码了怎么办 玩英雄联盟用腾讯游戏平台卡怎么办 游戏代练接单了没有给我账号怎么办 华为手机进入设置立即闪退怎么办 股东发现公司有做假账现象怎么办 中国在服役期间有纹身被发现怎么办 脚碰了肿了紫了怎么办 外阴出血了怎么办去医院检查没问题 三个半月宝宝体检脚有的紧怎么办 肛门被红枣核刺了一个洞怎么办 肛门里面有棉签上的棉花怎么办 孩子裤子沾屎怎么洗下来怎么办 做完痔疮手术后有点肛门狭窄怎么办 孕妇做b超宝宝不配合怎么办 怀孕产检医生问的尴尬怎么办 带着节育环做的核磁怎么办 便秘洗肠后最一周未排便怎么办 用了开塞露后肚子疼拉不出来怎么办 冰点脱毛当天用沐浴露洗澡了怎么办