[置顶] HDOJ 1026 Ignatius and the Princess I (BFS+优先队列+记录路径)
2017-07-18 11:00
423 查看
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19458 Accepted Submission(s): 6303
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.
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
题意:输入line,colume表示 l * c 的二维迷宫,下标从0(我是从1)开始求从(0,0)到(l-1,c-1)的最短路长及路径,图中“.”为平地,“X”为墙,“数字”代表击杀该处的怪物所需时间。代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<set> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; //HDOJ 1026 const int M = 101; int va[10] = {1,-1,0,0}; int vb[10] = {0,0,1,-1}; int l, c, flag[M][M], ans; char maz[M][M]; int ax[1000],ay[1000]; int dire[M][M]; struct point{ int px; int py; int step; friend bool operator < (point a,point b){ return a.step > b.step; } }; bool success; stack<int> tx; stack<int> ty; void bfs() { priority_queue<point> q; point tp,temp; tp.px = 1; tp.py = 1; tp.step = 0; q.push(tp); int i; while(!q.empty()){ tp = q.top(); q.pop(); if(tp.px==l&&tp.py==c){ ans = tp.step; success = true; break; } for(i=0;i<4;i++){ temp.px = tp.px + va[i]; temp.py = tp.py + vb[i]; temp.step = tp.step + 1; if(temp.px>=1&&temp.px<=l&&temp.py>=1&&temp.py<=c) if(!flag[temp.px][temp.py]&&maz[temp.px][temp.py]!='X'){ if(maz[temp.px][temp.py]=='.'){ dire[temp.px][temp.py] = i; q.push(temp); flag[temp.px][temp.py] = 1; }else{ temp.step += maz[temp.px][temp.py] - '0'; dire[temp.px][temp.py] = i; q.push(temp); flag[temp.px][temp.py] = 1; } } } } } void printpath() { int ti, tj; int time = 0; int prex,prey; int i, num = 0, j; int x, y; ti = l; tj = c; while(true){ tx.push(ti); ty.push(tj); if(ti==tj&&ti==1) break; if(dire[ti][tj]==0){ ti--; }else if(dire[ti][tj]==1){ ti++; }else if(dire[ti][tj]==2){ tj--; }else{ tj++; } } bool flag0 = true; if(maz[1][1]!='.'){ num = maz[1][1] - 48; ans += num; time -= 1; } cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl; while(!tx.empty()){ x = tx.top(); y = ty.top(); tx.pop(); ty.pop(); if(maz[x][y]=='.'){ time++; if(flag0){ flag0 = false; time--; }else printf("%ds:(%d,%d)->(%d,%d)\n",time,prex-1,prey-1,x-1,y-1); }else{ time++; if(flag0){ num = int(maz[x][y]) - 48; maz[x][y] = '.'; flag0 = false; for(j=0;j<num;j++){ time++; // printf("%ds:FIGHT AT (%d,%d)\n",time,x-1,y-1); } }else{ printf("%ds:(%d,%d)->(%d,%d)\n",time,prex-1,prey-1,x-1,y-1); num = int(maz[x][y]) - 48; for(j=0;j<num;j++){ time++; // printf("%ds:FIGHT AT (%d,%d)\n",time,x-1,y-1); } } } prex = x; prey = y; } } int main() { int i,j; while(scanf("%d%d",&l,&c)!=EOF){ ans = 0; getchar(); for(i=1;i<=l;i++){ for(j=1;j<=c;j++) scanf("%c",&maz[i][j]); getchar(); } memset(flag,0,sizeof(flag)); success = false; bfs(); if(success){ printpath(); }else{ cout<<"God please help our poor hero."<<endl; } cout<<"FINISH"<<endl; } } /* 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 */
相关文章推荐
- hdoj 1026 Ignatius and the Princess I 【bfs + 路径记录 【好题】 】
- HDU 1026 Ignatius and the Princess I(BFS+优先队列+路径记录)
- hdoj 1026 Ignatius and the Princess I 【BFS + 优先队列 + stack路径记录】 【经典题目】
- 我爱崔老师系列之 BFS+优先队列+路径记录 HDU 1026 Ignatius and the Princess I(BFS)
- 杭电1026 Ignatius and the Princess I(BFS+路径记录)
- HDU--1026:Ignatius and the Princess I (BFS+优先队列+打印路径)
- 杭电-1026Ignatius and the Princess I(BFS+记录路径)
- HDU OJ 1026 Ignatius and the Princess I 【搜索+记录路径】
- HDU 1026 Ignatius and the Princess I(广搜+路径记录+优先队列)
- HDOJ 1026 Ignatius and the Princess I(BFS+优先队列)
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
- HDU 1026 Ignatius and the Princess I(广搜记录路径)
- HDU 1026 Ignatius and the Princess I 记录 路径广搜
- HDU 1026Ignatius and the Princess I(bfs+记录路径)
- HDU1026--Ignatius and the Princess I(BFS记录路径)
- hdoj 1026 Ignatius and the Princess I【bfs&&路径输出】
- HDU-#1026 Ignatius and the Princess I(BFS+优先队列+路径存储)
- HDOJ 1026 Ignatius and the Princess I 搜索最短路径
- hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)