HDU 1026 Ignatius and the Princess I
2015-05-09 16:39
351 查看
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1026Problem DescriptionThe 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. InputThe 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. OutputFor 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 Input5 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 OutputIt 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优先队列dfs逆向搜索
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. InputThe 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. OutputFor 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 Input5 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 OutputIt 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优先队列dfs逆向搜索
#include #include #include using namespace std; struct st{ friend bool operator <(st a,st b){ return a.t>b.t;//优先队列的优先级 } int x,y; int t; int p_x,p_y; }mm[1000][1000]; int record[1000000]; int dis[1000][1000]; char maze[1000][1000]; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int time=0; void dfs(int i,int j){ if(i==1 && j==1){ return ; } dfs(mm[i][j].p_x,mm[i][j].p_y); if(maze[i][j]!='.'){ printf("%ds:(%d,%d)->(%d,%d)\n",time++,mm[i][j].p_x-1,mm[i][j].p_y-1,i-1,j-1); int k=0; while(k++>n>>m){ for(i=1;i<=n;i++) for(j=1;j<=m;j++){ cin>>maze[i][j]; dis[i][j]=10000; } priority_queueQ; mm[1][1].x=1;mm[1][1].y=1; if(maze[1][1]!='.'){ mm[1][1].t=maze[1][1]-'0'; dis[1][1]=maze[1][1]-'0'; }else{ mm[1][1].t=0; dis[1][1]=0; } Q.push(mm[1][1]); while(Q.size()){ st p=Q.top(); if(p.x==n && p.y==m){ break; } Q.pop(); for(i=0;i<4 i="" nx="p.x+dx[i];" ny="p.y+dy[i];" if="" nx="">=1 && nx<=n && ny>=1 && ny<=m && maze[nx][ny]!='X')) continue; if(maze[nx][ny]=='.'){ if(dis[p.x][p.y]+1>=dis[nx][ny]) continue; }else{ if(dis[p.x][p.y]+1+maze[nx][ny]-'0'>=dis[nx][ny]) continue; } mm[nx][ny].x=nx;mm[nx][ny].y=ny; mm[nx][ny].p_x=p.x;mm[nx][ny].p_y=p.y; if(maze[nx][ny]=='.') mm[nx][ny].t=p.t+1; else mm[nx][ny].t=p.t+1+maze[nx][ny]-'0'; dis[nx][ny]=mm[nx][ny].t; Q.push(mm[nx][ny]); } } time=1; if(!Q.size()){ 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",dis [m]); int k=0; while(k++<maze[1][1]-'0'){ printf("%ds:FIGHT AT (0,0)\n",time++); } dfs(n,m); printf("FINISH\n"); } } return 0; } 4>
相关文章推荐
- hdu 1026 Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I(bfs +记录路径)
- hdu1026——Ignatius and the Princess I
- HDU1026 Ignatius and the Princess I【优先搜索】
- HDU-1026-Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I (基本算法-BFS)
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
- hdu1026 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
- HDU 1026 Ignatius and the Princess I
- hdu - 1026 - Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)
- HDU 1026 Ignatius and the Princess I(广搜+路径记录+优先队列)
- HDU 1026 Ignatius and the Princess I (bfs+存储路径)
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)