搜索(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;}
- 搜索(HDU 1026)
- hdu搜索
- hdu搜索
- HDU 1704 (搜索题)
- HDU 4016 搜索
- HDU 1181 搜索
- HDU-1128 暴力搜索
- 搜索 hdu 1238
- hdu 3786 搜索
- HDU 1430 魔板 搜索
- HDU-1548(BFS搜索)
- 搜索hdu 1240
- HDU 4536 状态搜索
- hdu 2141 二分搜索
- hdu 1429搜索
- hdu 搜索专题
- HDU 2531 搜索
- HDU 1072 Nightmare(搜索)
- 【Android】Intent简介
- 八皇后
- iOS 中基础字符判断函数收集(如判断大小写、数字等)
- [补充二]基于多幅图像的3D重构
- butterknife的使用
- 搜索(HDU 1026)
- android mtk6592 禁止拖动主界面图标
- Java学习笔记之获取当前目录或文件
- 小波变换简介(3)
- 第五章 - 图像形态学 - 基于图像金字塔的图像分割(cvPyrSegmentation)
- 苹果开发者帐号(Company)申请流程
- ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- 母函数 多项式相乘求系数(HDU 1028)