hdu 1026 Ignatius and the Princess I 优先队列 + bfs
来源:互联网 发布:java文件下载用什么流 编辑:程序博客网 时间:2024/05/17 18:04
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11911 Accepted Submission(s): 3749
Special Judge
Problem 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
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.
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.
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
#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <cstring>using namespace std;#define maxn 110struct Point{ int x,y; Point(){} Point(int a,int b) { x = a,y = b; }};struct Node{ int x,y; int time; vector<Point> path; Node (){} Node(int a,int b,int c){ x = a,y = b,time = c; path.clear(); path.push_back(Point(0,0)); } friend bool operator < (Node a,Node b) { return a.time > b.time; }};char g[maxn][maxn];bool vis[maxn][maxn];int n,m;int dx[]= {-1,1,0,0};int dy[]= {0,0,1,-1};Node ans;/////////////////int bfs(){ priority_queue<Node> que; que.push(Node(0,0,0)); memset(vis,false,sizeof vis); vis[0][0] = true; Node tmp; bool v1[maxn][maxn]; memset(v1,false,sizeof v1); while(!que.empty()) { tmp = que.top(),que.pop(); int x = tmp.x , y = tmp.y; if(g[x][y]!='.' && !v1[x][y]) { for(int i=0;i<g[x][y]-'0';i++) { tmp.path.push_back(Point(x,y)); tmp.time++; } v1[x][y] = true; que.push(tmp); continue; } if(x==n-1 && y==m-1) { ans = tmp; return tmp.time; } for(int i=0;i<4;i++) { int xx = x+dx[i],yy = y+dy[i]; if(xx>=0&&xx<n&&yy>=0&&yy<m && !vis[xx][yy] &&g[xx][yy]!='X') { vis[xx][yy] = true; Node t1 = tmp; t1.x = xx , t1.y = yy , t1.time = tmp.time+1; t1.path.push_back(Point(xx,yy)); que.push(t1); } } } return -1;}void solve(){ int t = bfs(); if(t==-1) printf("God please help our poor hero.\n"); else { int x; printf("It takes %d seconds to reach the target position, let me show you the way.\n",t); for(int i=0;i<ans.path.size()-1;i++) { if(g[ans.path[i].x][ans.path[i].y] >'0' && g[ans.path[i].x][ans.path[i].y] <='9') { printf("%ds:FIGHT AT (%d,%d)\n",i+1,ans.path[i].x,ans.path[i].y); g[ans.path[i].x][ans.path[i].y]--; } else { printf("%ds:(%d,%d)->(%d,%d)\n",i+1,ans.path[i].x,ans.path[i].y,ans.path[i+1].x,ans.path[i+1].y); } } } printf("FINISH\n");}int main(){ while(cin >> n >> m) { getchar(); for(int i=0;i<n;i++) gets(g[i]); solve(); } return 0;}
0 0
- 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(广度优先搜索))
- 【搜索之BFS + 优先队列】杭电 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(BFS+优先队列+路径存储)
- HDU 1026 Ignatius and the Princess I 【BFS(优先队列)+路径存储】
- hdu 1026 Ignatius and the Princess I(优先队列bfs+dfs输出)
- HDU ACM 1026 Ignatius and the Princess I -> BFS+优先队列+路径打印
- Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)
- 正则表达式入门
- 【家用带宽测试方法】10m宽带要多少下载速度才达标
- jquery AJAX 返回总是error XMLHttpRequest.status 为0
- mjpg-stream的使用
- 杂项
- hdu 1026 Ignatius and the Princess I 优先队列 + bfs
- linux c 进程 pipe 通信代码分析
- 动态规划学习的开始——01背包
- Freescale实纪:S-D5舵机相关问题
- vc++深入详解 1
- UNIX编程 - 基础
- WEB前端开发学习----1.HTML基础
- Freescale实纪——关于线性CCD TSL1401CL与曝光的一些心得
- 你会做Web上的用户登录功能吗?