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. PCB表面处理工艺

    PCB表面处理最基本的目的是保证良好的可焊性或电性能.由于自然界的铜在空气中倾向于以氧化物的形式存在,不大可能长期保持为原铜,因此需要对铜进行其他处理. 1.热风整平(喷锡) 热风整平又名热风焊料整平 ...

  2. CNV

    CNV: 人类主要是二倍体.如果有些区域出现3个.4个拷贝,那就是扩增了,如果只出现1个拷贝,就是缺失.所以CNV分析是依靠特定位置的测序深度来估算的,先在染色体上划窗,然后看每个窗口的平均测序深度, ...

  3. servlet&jsp高级:第二部分

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

  4. hd1496---->这道题是水水的数论吗?

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1496 题意: Consider equations having the following form: ...

  5. domion Designer 管理员ID过期

    上班没几天,刚接触lotus domion 有一个服务器上打开相应的数据库提示 你的证书已经过期,网上找到的解决方案: ---------------------------------------- ...

  6. 常用的邮箱服务器(SMTP、POP3)地址、端口

    常用的邮箱服务器(SMTP.POP3)地址.端口 参考网址:http://wenku.baidu.com/link?url=IPv15rPRkd0nsuGH0Dm0A5kFyRaeHJY2_gYpDW ...

  7. 一个封装较好的删除方法(Delete)

    前台的引用 @Html.ActionLink(“删除字样”,“后台的删除方法”,new{绑定id},new{@style="样式"});方法,如何要独立使用的话,一般还要使用到相应 ...

  8. JCO事务管理

    /* * 标准对账单过账 * @account 标准对账单号 * @year 年度 */ public List<String> doAccountStatmentPost(String ...

  9. HDU5785 manacher+差分数组

    用manacher算法O(n)求出所有的回文半径.有了回文半径后,就可以求出L[i]表示以i结尾的回文串的起始位置的和R[i]表示以i起始的回文串的结尾位置的和,然后就可以求出答案了,这里要注意奇偶长 ...

  10. (六)ARM状态寄存器-PSR

    ARM程序状态寄存器Program State Register 在ARM模式中, 有16个数据寄存器和1或2个状态寄存器是可以随时访问的.在特权模式 (privileged mode) 下, 对应的 ...