# HDU1026 bfs

24 篇文章 0 订阅

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

#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;

int N,M;
char map[110][110];
int ex,ey;
int result;
int flag[110][110];

int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};

struct point{
int x,y;
int time;
};

bool check(int x,int y){
if(x>=0&&y>=0&&x<N&&y<M)return true;
return false;
}
int Search(int sx,int sy){
point t1,t2;
queue<point> Q;
t1.x=sx;
t1.y=sy;
t1.time=0;
flag[0][0]=0;
Q.push(t1);
while(!Q.empty()){
t1=Q.front();
if(t1.x==N-1&&t1.y==M-1){
if(result>t1.time)result=t1.time;
}
Q.pop();
for(int i=0;i<4;i++){
int dx=t1.x+dir[i][0];
int dy=t1.y+dir[i][1];

if(check(dx,dy)&&map[dx][dy]=='.'){
t2.x=dx;
t2.y=dy;
t2.time=t1.time+1;
if(t2.time<flag[t2.x][t2.y]&&t2.time<=result){
flag[t2.x][t2.y]=t2.time;
Q.push(t2);
}
}
else if(check(dx,dy)&& (map[dx][dy]>='1' && map[dx][dy]<='9')){
t2.x=dx;
t2.y=dy;
t2.time=t1.time+1+map[dx][dy]-'0';
if(t2.time<flag[t2.x][t2.y]&&t2.time<=result){
flag[t2.x][t2.y]=t2.time;
Q.push(t2);
}
}
}
}
return result;
}

void print(int x,int y,int step){
if(step==0)return;
int time=0,i;
int dx,dy,rx,ry;
if(map[x][y]>='1' && map[x][y]<='9'){
time=map[x][y]-'0';
}
for(i=0;i<4;i++){
dx = x+dir[i][0];
dy = y+dir[i][1];

if(check(dx,dy)){
int t=0;
if(map[x][y]>='1' && map[x][y]<='9')t=map[x][y]-'0';
if(flag[dx][dy]+1+t==flag[x][y]){
rx=dx;
ry=dy;
print(dx,dy,step-time-1);
break;
}
}
}
printf("%ds:(%d,%d)->(%d,%d)\n",step-time,rx,ry,x,y);
while(time>0){
printf("%ds:FIGHT AT (%d,%d)\n",step-time+1,x,y);
time--;
}
return ;
}

int main(){
int i,j;
while(scanf("%d%d",&N,&M)!=EOF){
for(i=0;i<110;i++)
for(j=0;j<110;j++)
flag[i][j]=99999;
for(i=0;i<N;i++){
scanf("%s",map[i]);
}
result=99999;

int t=Search(0,0);
if(t==99999){
}
else{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",t);
print(N-1,M-1,t);
printf("FINISH\n");
}
}
return 0;
}


• 0
点赞
• 0
收藏
• 打赏
• 0
评论
09-22
05-04 1781
07-11 876
11-23 532
10-31 213
07-02 2025
11-24 1111
06-06 96
08-24 20
03-24 447
08-17 1121
01-22 902
02-12 379
01-15 97
11-14 825
07-05 1674
08-24 23

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

qq_18952045

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。