博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ignatius and the Princess I
阅读量:6420 次
发布时间:2019-06-23

本文共 4454 字,大约阅读时间需要 14 分钟。

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

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 
#include
#include
#include
#include
#include
#include
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
=0&&ay
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
>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 "<
<< " seconds to reach the target position, let me show you the way."<

转载于:https://www.cnblogs.com/wangyumin/p/5323421.html

你可能感兴趣的文章
kaptcha可配置项
查看>>
JavaMail邮箱验证用户注册
查看>>
系统时间——ntpd
查看>>
反射实现AOP动态代理模式(Spring AOP实现原理)
查看>>
Http协议与缓存
查看>>
监测超过特定内存阀值进程并结束
查看>>
Linux Centos 查询信息
查看>>
android adb命令
查看>>
python “双”稀疏矩阵转换为最小联通量“单”矩阵
查看>>
揭秘天猫双11背后:20万商家600万张海报,背后只有一个鹿班
查看>>
重置mysq root密码脚本
查看>>
我的友情链接
查看>>
MHA配置参数
查看>>
深入理解Lock
查看>>
vim的块选择
查看>>
HTML --块
查看>>
在DLL中获取主进程窗口句柄
查看>>
基于消息队列的双向通信
查看>>
一个不错的loading效果
查看>>
Debian允许root用户登录
查看>>