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. 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)

    留着提醒自己,底层才是最重要的,不要一直傻瓜的编程下去! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  2. [HDU5727]Necklace(二分图最大匹配,枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:有N个阴珠子和N个阳珠子,特定序号的阴阳珠子放在一起会让阳珠子暗淡.现在问排放成一个环,如 ...

  3. SY全局系统字段

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

  4. C#:字段与属性

    MSDN中是这么介绍字段和属性的: A field is a variable of any type that is declared directly in a class or struct. ...

  5. oracle的基本查询~上

    SQL> --查询一下当前登录的用户名SQL> show user;USER 为 "SCOTT"SQL> --查询当前用户下有哪些表SQL> select ...

  6. MySQL DATE_ADD() 函数

    用途: 在MySql语句执行日期的加减 实际情况: 常用于加减一段时间和当前时间比较的情况 函数如下: #date 指定的时间值 #INTERVAL #expr 添加的时间间隔 # 时间间隔单位 DA ...

  7. 优秀c++开源项目集合

    本文会持续更新, 我希望通过这篇文章把我看到过的优秀开源项目记录下来, 有时间仔细阅读. cockroachdb 前googler开发的开源的spanner数据库: https://github.co ...

  8. 让你分分钟读懂CPU架构及芯片厂商

    CPU架构是CPU厂商给属于同一系列的CPU产品定的一个规范,主要目的是为了区分 不同类型CPU的重要标示.目前市面上的CPU指令集分类主要分有两大阵营,一个是intel.AMD为首的复杂指令集CPU ...

  9. 构建maven项目3

    1.1.创建Jave Project 1.使用mvn archetype:generate命令,如下所示: mvn archetype:generate -DgroupId=com.mycompany ...

  10. BestCoder Valentine's Day Round

    昨晚在开赛前5分钟注册的,然后比赛刚开始就掉线我就不想说了(蹭网的下场……),只好用手机来看题和提交,代码用电脑打好再拉进手机的(是在傻傻地用手机打了一半后才想到的办法). 1001,也就是 hdu ...