Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12124    Accepted Submission(s): 3824
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
 
Author
Ignatius.L
 
简单的搜索题: 记录路径额搜索...
代码:
 /*Problem : 1026 ( Ignatius and the Princess I )     Judge Status : Accepted
RunId : 11510650 Language : G++ Author : huifeidmeng
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/ #include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<iostream>
using namespace std;
const int maxn=;
struct node{
int x,y;
};
struct sode{
int val;
int sum;
node pre; //他一个点
void setint(int x,int y){
pre.x=x;
pre.y=y;
}
};
int n,m,cont;
sode map[maxn][maxn];
int dir[][]={{,},{-,},{,},{,-}};
void bfs(){
queue<node> anc;
node tem={,};
anc.push(tem);
while(!anc.empty()){
node sav=anc.front();
anc.pop();
for(int i=;i<;i++){
tem=(node){dir[i][]+sav.x,dir[i][]+sav.y};
if(tem.x>=&&tem.x<n&&tem.y>=&&tem.y<m&&map[tem.x][tem.y].val!=-){
if(map[tem.x][tem.y].sum==
||map[tem.x][tem.y].sum>map[sav.x][sav.y].sum+map[tem.x][tem.y].val+)
{
map[tem.x][tem.y].sum=map[sav.x][sav.y].sum+map[tem.x][tem.y].val+;
map[tem.x][tem.y].setint(sav.x,sav.y);
anc.push(tem);
}
}
}
}
}
void dfs(sode a,node cur){
if(cur.x==&&cur.y==){
cont=;
while(map[cur.x][cur.y].val-->)
printf("%ds:FIGHT AT (%d,%d)\n",cont++,cur.x,cur.y);
// printf("%ds:(%d,%d)->(%d,%d)\n",cont++,a.pre.x,a.pre.y,cur.x,cur.y);
return;
}
dfs(map[a.pre.x][a.pre.y],a.pre);
printf("%ds:(%d,%d)->(%d,%d)\n",cont++,a.pre.x,a.pre.y,cur.x,cur.y);
if(a.val>){
while(a.val-->){
printf("%ds:FIGHT AT (%d,%d)\n",cont++,cur.x,cur.y);
}
}
}
int main(){
char ss[];
//freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%s",ss);
for(int j=;j<m;j++){
map[i][j].sum=;
map[i][j].setint(,);
if(ss[j]=='.') map[i][j].val=;
else if(ss[j]=='X')map[i][j].val=-;
else{
map[i][j].val= ss[j]-'';
if(i+j==)map[i][j].sum=map[i][j].val;
}
}
}
bfs();
if(map[n-][m-].pre.x==)
printf("God please help our poor hero.\n");
else {
printf("It takes %d seconds to reach the target position, let me show you the way.\n",map[n-][m-].sum);
dfs(map[n-][m-],(node){n-,m-});
}
puts("FINISH");
}
return ;
}
 

hdu---------(1026)Ignatius and the Princess I(bfs+dfs)的更多相关文章

  1. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  2. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  3. HDU 1026 Ignatius and the Princess I(BFS+记录路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  5. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  7. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. CodeForces 151B Phone Numbers

     Phone Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  2. Quartz实用二三事

    注意:本文项目使用的Quartz版本为2.2.1 一.关于Trigger Trigger tg = newTrigger().withIdentity("tg3", "g ...

  3. [POJ2182]Lost Cows(树状数组,二分)

    题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...

  4. XML详解:第三部分 XML解析

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. FZU 2219 StarCraft(星际争霸)

    Description 题目描述 ZB loves playing StarCraft and he likes Zerg most! One day, when ZB was playing SC2 ...

  6. 纯js写验证码

    <html> <head> <meta name="viewport" content="width=device-width" ...

  7. 面向对象---prototype

    构造函数里的this,外面的new <script> //用工厂方式构造对象 function createPerson(name, sex) //构造函数 { //假想的系统内部工作流程 ...

  8. XAF应用开发教程(七)外观控制模块

    很多时候,我们需要按照不同的条件显示不同的效果,在传统的软件开发中,我们会直接使用 控件名称.BackColor,Enable,Visible等属性进行控制. 如果一个业务对象在多处使用,要么我们会去 ...

  9. Redis核心知识之—— 时延问题分析及应对、性能问题和解决方法【★★★★★】

    参考网址: Redis时延问题分析及应对:http://www.cnblogs.com/me115/p/5032177.html Redis常见的性能问题和解决方法:http://www.search ...

  10. 如何删除github里面的文件夹?

    按照以下步骤即可(本地删除) 1. git pull you git url2. git checkout 3. rm -r dirName4. git add --all5. git commit  ...