搜索(HDU 1026)

来源:互联网 发布:张锦洪画家淘宝网 编辑:程序博客网 时间:2024/06/02 04:25


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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH

 

 

 

很典型的一道搜索题,其中主要是用广搜进行搜索,但是与传统的广搜不同的是,每个点不止搜索一次,所以用广搜的时候利用队列保存走过的节点,如果用数组当做队列的话,数组的大小要远大于迷宫的大小,另外用一个mark二维数组保存走过每个节点所花的时间,如果当经过某个节点的时候所花的时间小于原本保存的时间,则用新的时间替换原本保存的时间,最后可以利用mark二维数组求出经过每一个节点的最短时间。

还有一个问题就是关于打印路径的问题,这是从最后一个节点开始,然后利用深搜进行回朔法进行打印,当找到第一个节点之后再回朔到最后一个节点,最后可以打印出所有的路径。

 

代码:

 

#include<stdio.h>#include<string.h>struct point{int x,y;};struct node{point p;int step;};struct map_count{int count;int pre;};int m,n;char map[110][110];int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};node que[1001500];int front,rear,flag;map_count mark[110][110];void bfs(){que[0].p.x = 0;que[0].p.y = 0;que[0].step = 0;mark[0][0].count = 0;mark[0][0].pre = -1;rear++;while(front!=rear){node now;now = que[front];if(now.p.x==(n-1) && now.p.y==(m-1)){flag = 1;}for(int i=0;i<4;i++){node tmp = now;tmp.p.x = tmp.p.x+dir[i][0];tmp.p.y = tmp.p.y+dir[i][1];   if(tmp.p.x<n && tmp.p.y<m && tmp.p.x>=0 && tmp.p.y>=0 ){if(map[tmp.p.x][tmp.p.y]!='X'){if(map[tmp.p.x][tmp.p.y]=='.'){tmp.step +=1;if(tmp.step<mark[tmp.p.x][tmp.p.y].count){mark[tmp.p.x][tmp.p.y].count = tmp.step;mark[tmp.p.x][tmp.p.y].pre = i;que[rear] = tmp;rear++;}}else if(map[tmp.p.x][tmp.p.y]>='1' && map[tmp.p.x][tmp.p.y]<='9'){tmp.step += map[tmp.p.x][tmp.p.y]-'0'+1;if(tmp.step<mark[tmp.p.x][tmp.p.y].count){mark[tmp.p.x][tmp.p.y].count = tmp.step;mark[tmp.p.x][tmp.p.y].pre = i;que[rear] = tmp;rear++;}}}}}front++;}}void printPath(int pre,int x,int y,int step){int pre_x = x-dir[pre][0];int pre_y = y-dir[pre][1];if(x==0 && y==0)return;else{if(map[x][y]=='.'){printPath(mark[pre_x][pre_y].pre, pre_x, pre_y, mark[pre_x][pre_y].count);printf("%ds:(%d,%d)->(%d,%d)\n", step, pre_x, pre_y, x, y);}else{printPath(mark[pre_x][pre_y].pre, pre_x, pre_y, mark[pre_x][pre_y].count);printf("%ds:(%d,%d)->(%d,%d)\n", mark[pre_x][pre_y].count+1, pre_x, pre_y, x, y);for(int i=map[x][y]-'0'-1;i>=0;i--)printf("%ds:FIGHT AT (%d,%d)\n",step-i, x, y);}}}int main(){while(scanf("%d %d", &n,&m)!=EOF){memset(map, '\0', sizeof(char)*110*110);front = 0;rear = 0;flag = 0;for(int i=0;i<n;i++){scanf("%s", map[i]);}for(int i=0;i<110;i++){for(int j=0;j<110;j++){mark[i][j].count = 100100;}}bfs();if(flag){printf("It takes %d seconds to reach the target position, let me show you the way.\n", mark[n-1][m-1].count);printPath(mark[n-1][m-1].pre, n-1, m-1, mark[n-1][m-1].count);}else{printf("God please help our poor hero.\n");}printf("FINISH\n");}return 0;}


 

0 0
原创粉丝点击