杭电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;}
- 杭电1026——Ignatius and the Princess I(BFS)
- 杭电1026 Ignatius and the Princess I(BFS+路径记录)
- 杭电-1026Ignatius and the Princess I(BFS+记录路径)
- HDU1026——Ignatius and the Princess I(BFS)
- 【搜索之BFS + 优先队列】杭电 hdu 1026 Ignatius and the Princess I
- HDU杭电1026 Ignatius and the Princess I(迷宫问题bfs)
- 杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA
- 杭电ACM OJ 1026 Ignatius and the Princess I DFS+BFS
- 杭电 hdu 1026 Ignatius and the Princess I(BFS+优先队列+墨迹人的输出)
- HDU-1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I (BFS)
- HDU - 1026 - Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I(BFS)
- 杭电OJ 1026:Ignatius and the Princess I
- HDU1026——Ignatius and the Princess I (优先队列+bfs)
- mysql中utf-default collation
- opencv(5)
- 【Lua学习笔记】 --> 《类型与值、表达式与基本语法》
- 实现键盘弹出时,显示Emoji按钮的功能
- 记录imp导入用户导到sys用户删除
- 杭电1026——Ignatius and the Princess I(BFS)
- Swift 扩展(extension)
- 教你快速处理网络故障
- 石头剪子布
- 如何知道mysql中sql语句索引是否生效
- 数组名和指针
- 大数加法
- 互联网网间结算的全球趋势及启示
- node项目中的定时任务