Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)
来源:互联网 发布:淘宝刷一个皇冠多少钱 编辑:程序博客网 时间:2024/05/21 22:52
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14624 Accepted Submission(s): 4634
Special Judge
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.
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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH
Author
Ignatius.L
Recommend
We have carefully selected several similar problems for you: 1072 1175 1180 1240 1241
题意:给出n*m的地图,求从(0,0)走到(n-1,m-1)最短时间,方格上的数字表示杀怪所要的时间。
思路:优先队列+bfs,输出路径的时候用一个path数组记录,path[i][j]=d表示(i,j)位置是由d方向过来的。
代码:
#include <iostream>#include <functional>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 105;const int MAXN = 2005;const int MAXM = 200010;const int N = 1005;struct Node{ int x,y,step; bool operator<(const Node &a)const { return step>a.step; }};int n,m,cnt;int dir[4][2]={1,0,0,1,0,-1,-1,0};char mp[maxn][maxn];int vis[maxn][maxn];int path[maxn][maxn];bool isok(int x,int y){ if (x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='X') return true; return false;}void out(int x,int y){ if (path[x][y]==-1) return ; int dx=x-dir[path[x][y]][0]; int dy=y-dir[path[x][y]][1]; out(dx,dy); if (isdigit(mp[dx][dy])) { for (int i=0;i<mp[dx][dy]-'0';i++) printf("%ds:FIGHT AT (%d,%d)\n",cnt++,dx,dy); } printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,dx,dy,x,y);}int bfs(){ Node st,now; mem(path,-1); mem(vis,0); st.x=st.y=st.step=0; vis[0][0]=1; priority_queue<Node>Q; Q.push(st); while (!Q.empty()) { st=Q.top(); Q.pop(); if (st.x==n-1&&st.y==m-1) return st.step; for (int i=0;i<4;i++) { now.x=st.x+dir[i][0]; now.y=st.y+dir[i][1]; if (isok(now.x,now.y)&&!vis[now.x][now.y]) { vis[now.x][now.y]=1; now.step=st.step+1; if (isdigit(mp[now.x][now.y])) now.step=now.step+mp[now.x][now.y]-'0'; path[now.x][now.y]=i; Q.push(now); } } } return 0;}int main(){#ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);#endif int i,j; while (~scanf("%d%d",&n,&m)) { for (i=0;i<n;i++) scanf("%s",mp[i]); int ans=bfs(); if (ans==0) printf("God please help our poor hero.\nFINISH\n"); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); cnt=1; out(n-1,m-1); if (isdigit(mp[n-1][m-1])) for (i=0;i<mp[n-1][m-1]-'0';i++) printf("%ds:FIGHT AT (%d,%d)\n",cnt++,n-1,m-1); printf("FINISH\n"); } } return 0;}
1 0
- Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
- Ignatius and the Princess I (hdu 1026 优先队列+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优先队列
- HDU 1026 Ignatius and the Princess I BFS优先队列
- HDU 1026 Ignatius and the Princess I BFS 优先队列
- HDOJ 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 ACM 1026 Ignatius and the Princess I -> BFS+优先队列+路径打印
- hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录
- EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient
- 算法导论——(2)二叉查找树的实现
- Linux Kernel Parameter config for Oracle
- android Service
- 1395 - Slim Span (最小生成树)
- Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)
- java was started but returned exit code=13
- Mac OS X安装redis-php扩展
- Python对xml的操作
- 【php】基于Redis的js、css缓存类
- 机房收费系统之上下机
- 20个你可能不知道的 Linux 网络工具
- 你好,世界!
- 实现一个函数来调整该数组中数字的顺序实例