算法:搜索+优先队列+(递归输出结果)

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.

输入

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.

输出

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.

样例输入

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.

样例输出

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

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <string>
#include <stdio.h>
using namespace std;
char ch[105][105];
int map[105][105];
int flag[105][105];
int cnt[105][105];
int a[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,tim;
struct dot
{
int x,y,time;
friend bool operator<(dot node,dot node1)
{return node.time>node1.time;}
} ;
int cmp(int ax,int ay)
{
if(ax>=0&&ax<n&&ay>=0&&ay<m&&!map[ax][ay]) return 1;
return 0;
}
int bfs()
{
priority_queue<dot>que;//优先队列
dot cur,loer;
cur.x=0;cur.y=0;cur.time=0;
memset(map,0,sizeof(map));
memset(flag,0,sizeof(flag));
map[0][0]=1;//标记是否走过该点;
que.push(cur);
while(que.size())
{
loer=que.top();
que.pop();
if(loer.x==n-1&&loer.y==m-1)
return loer.time;
for(int i=0;i<4;i++)
{
int dx,dy;
dx=loer.x+a[i][0];
dy=loer.y+a[i][1];
if(cmp(dx,dy)&&ch[dx][dy]!='X')
{
map[dx][dy]=1;
cur.x=dx;cur.y=dy;
cur.time=loer.time+1;
if(ch[dx][dy]>='1'&&ch[dx][dy]<='9')
cur.time+=ch[dx][dy]-'0';
flag[dx][dy]=i+1;//标记由哪个方向走过了;
que.push(cur);
}
} }
return -1;
}
//递归输出走向;
void print(int ax,int ay)
{int nx,ny;
if(flag[ax][ay]==0) return ;
nx=ax-a[flag[ax][ay]-1][0];
ny=ay-a[flag[ax][ay]-1][1];
print(nx,ny);
printf("%ds:(%d,%d)->(%d,%d)\n",tim++,nx,ny,ax,ay);
while(cnt[ax][ay]--)
{printf("%ds:FIGHT AT (%d,%d)\n",tim++,ax,ay);}
}
int main()
{
int i,j,k;
while(cin>>n>>m)
{
memset(cnt,0,sizeof(cnt));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>ch[i][j];
if(ch[i][j]>='0'&&ch[i][j]<='9')
cnt[i][j]=ch[i][j]-'0';
}
}
int ans=bfs();
if(ans>=0)
{
tim=1;
cout<<"It takes "<<ans<<
" seconds to reach the target position, let me show you the way."<<endl;
print(n-1,m-1);
}
else cout<<"God please help our poor hero."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}

Ignatius and the Princess I的更多相关文章

  1. 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 ...

  2. hdu acm 1028 数字拆分Ignatius and the Princess III

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

  3. hdu1026.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 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  5. ACM: HDU 1028 Ignatius and the Princess III-DP

     HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Form ...

  6. hdu 1028 Ignatius and the Princess III(DP)

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

  7. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  8. HDU 1027 Ignatius and the Princess II(康托逆展开)

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

  9. hdu---------(1026)Ignatius and the Princess I(bfs+dfs)

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

  10. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

随机推荐

  1. mongodb 教程三

    mongodb 实际上是保存json的数据的结构.当查询 是修改时 key 是可以用''保存. mongodb  插入语句 db.weixin.insert({name:'zhangfeng',age ...

  2. 手机控制电脑,在WIFI局域网下(关机,重启,遥控)

    这个软件叫百变遥控:http://blog.sina.com.cn/s/blog_9abc7dbc0101hmut.html 今天周末,在家里看电影,家里用的是台式电脑,我自己买了一个投影仪来专门看视 ...

  3. 转:WebSocket与Java

    Bozhidar Bozhanov是Ontotext AD的高级软件工程师,拥有多年的从业经验,也是stackoverflow上的活跃用户.他精通于Java与Java技术栈,如Spring.JPA.J ...

  4. poi导出word

    最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...

  5. BZOJ 1022 [SHOI2008]小约翰的游戏John

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1635  Solved: 1036[Submit] ...

  6. HDOJ 2102

    如果传送门'#'的另一层是传送门'#'或者是墙'*',就可以理解为这两层的这个位置都是'*'了 还有就是传送门'#'传过去可以是空地'.' 也可以是目的地'P',不要忽略了 #include < ...

  7. 本地apt

    Ubuntu建立本地源非常實用,很多服務器在局域網沒有網络或者網络很慢的情況下,或者需要批量安裝同样的軟件的時候,如果每一台服務器都去外網下載,是不是很慢,而且也不是一個運維工程師願意這麼幹的!那有什 ...

  8. mergeIDE

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CriticalDe ...

  9. King's Quest - poj 1904(强连通分量+外挂输入输出)

    题意:国王有N个儿子,每个儿子都有很多喜欢的姑娘,官员为每个王子都找了一个姑娘让他们结婚,不过国王不满意,他想知道他的每个儿子都可以和那个姑娘结婚(前提他的儿子必须喜欢那个姑娘) 分析:因为最下面一行 ...

  10. 【Python排序搜索基本算法】之Prim算法

    Prim算法是实现最简单的最小生成树(MST)算法,适合于稠密图.要实现Prim算法,我们主要关注的是增量的变化,也就是从每个非树顶点到树顶点的最短距离,使得最后生成一棵包括所有顶点的树,并且这棵树的 ...