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. [翻译]观察变换View Transform (Direct3D 9)

    这一节介绍在Direct3d中观察变换的基本概念和怎么去设置观察矩阵. 视口变换把观察者放在世界坐标系中,并把顶点转化到摄像机空间.在摄像机空间,摄像机或者说观察者在原点,观察方向为z轴正向.Dire ...

  2. VBA中的FileSystemObject对象(FSO)和文本流

    对FileSystemObject一直略有耳闻,VBA爱好者常常简称为FSO对象. 在Scripting类库中有三个可以直接使用NEW关键字实例化的类,第一个就是常用的字典,第三个是FSO. 一.FS ...

  3. Fully differential amplifiers

    Introduction       专业音频工程师通常使用术语“平衡”来指代差分信号传输.这也告知了我们对称的概念,同时它在差分系统中也是非常重要的.在差分系统中,驱动器有平衡的输出,传输线有平衡的 ...

  4. ABAP锁、数据库锁

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

  5. 使用Jmeter进行简单的http接口测试

    1.添加线程组:在“测试计划”上点击鼠标右键-->添加-->threads(Users)-->线程组,添加测试场景设置组件,接口测试中一般设置为1个“线程数”,根据测试数据的个数设定 ...

  6. hdu 5673 Robot 卡特兰数+逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  7. web服务器安装配置

    学习目标 javaweb概念和web资源分类 服务器的分类和常用服务器apache说明 tomcat 服务器目录结构介绍和工程发布 虚拟主机说明和配置 1.Web的概念 1.1.JavaWeb的概念 ...

  8. SQL SERVER赋权限

    --创建登录账户 use master GO EXEC sp_addlogin 'jacky', 'pwd' --EXEC sp_droplogin 'jacky' --删除登陆账户 use Test ...

  9. jdk和eclipse位数不一致出错

    32位的eclipse无法打开:找不64位jdk6的jvm.dll文件(64位的没有这个文件).网上说法可以通过设置eclipse初始化文件xxx.ini改变方式: 直接换成了同位数的了,没去试了.

  10. FileCopy

    /*[入]指的是到内存里,[出]指的是到内存外*/ import java.io.*; public class MyReadFile{ public static void main(String[ ...