HDU - 1026 Ignatius and the Princess I
2015-11-24 06:47
316 查看
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Special Judge
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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.
[align=left]Sample Output[/align]
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
[align=left]Author[/align]
Ignatius.L
[align=left]Recommend[/align]
We have carefully selected several similar problems for you: 1072 1175 1180 1253 1242
题解:bfs+优先队列+记录路径
#include<cstdio> #include<cstring> #include<queue> #include<string> using namespace std; struct que { int x,y,s; string p; friend bool operator<(const que & a,const que & b) { return a.s > b.s; } }frt,tmp; priority_queue<que>Q; const int dx[]={-1, 0,+1, 0}; const int dy[]={ 0,+1, 0,-1}; int n,m; char ma[111][111]; char ch[20]; void prt(que q) { printf("It takes %d seconds to reach the target position, let me show you the way.\n",q.s); int len=q.p.length(),i,j=0,k,t=0; que pre,now; pre.x=pre.y=pre.s=0; for(i=0;i<len;i++) if(q.p[i]=='-') { for(k=0;j<i;j++)ch[k++]=q.p[j]; ch[k]='\0'; sscanf(ch,"%d,%d,%d",&now.x,&now.y,&now.s); printf("%ds:(%d,%d)->(%d,%d)\n",++t,pre.x,pre.y,now.x,now.y); for(k=now.s-pre.s;k>1;k--)printf("%ds:FIGHT AT (%d,%d)\n",++t,now.x,now.y); pre=now; i=j++; } //printf("%s\n",q.p.c_str()); puts("FINISH"); } void bfs() { while(!Q.empty())Q.pop(); tmp.x=tmp.y=tmp.s=0; tmp.p=""; Q.push(tmp); ma[0][0]='X'; int i; while(!Q.empty()) { frt=Q.top(); if(frt.x==n-1&&frt.y==m-1) { prt(frt); return; } Q.pop(); for(i=0;i<4;i++) { tmp.x=frt.x+dx[i]; tmp.y=frt.y+dy[i]; if(0<=tmp.x&&tmp.x<n&&0<=tmp.y&&tmp.y<m&&ma[tmp.x][tmp.y]!='X') { tmp.s=frt.s+1; if(ma[tmp.x][tmp.y]!='.')tmp.s+=(ma[tmp.x][tmp.y]-'0'); sprintf(ch,"%d,%d,%d-",tmp.x,tmp.y,tmp.s); tmp.p=frt.p+string(ch); Q.push(tmp); ma[tmp.x][tmp.y]='X'; } } } printf("God please help our poor hero.\nFINISH\n"); } int main() { int i; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++)scanf("%s",ma[i]); bfs(); } return 0; }
相关文章推荐
- (spring-第10回【IoC基础篇】)InstantiationStrategy--实例化Bean的第三大利器
- [LeetCode243]Shortest Word Distance
- [LeetCode186]Reverse Words in a String II
- 221. Maximal Square
- [LeetCode170]Two Sum III - Data structure design
- S3C6410的tv out输出lcd的内容
- B-树和B+树的应用:数据搜索和数据库索引【转】
- 普法知识(10):处分人要讲“道理”
- 一个简单的马尔可夫过程例子
- Volley源码解析
- [LeetCode167]Two Sum II - Input array is sorted
- [LeetCode163]Missing Ranges
- 220. Contains Duplicate III
- [LeetCode161] One Edit Distance
- [LeetCode160]Longest Substring with At Most Two Distinct Characters
- [LeetCode158]Read N Characters Given Read4 II - Call multiple times
- hdoj 1001(C实现)
- Algorithm Complexity
- 安装rhel-server-7.1-x64
- hdoj 1000(C实现)