设为首页 加入收藏

TOP

HDOJ1242 Rescue(营救) 搜索(一)
2017-10-11 17:35:28 】 浏览:1528
Tags:HDOJ1242 Rescue 营救 搜索

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22286    Accepted Submission(s): 7919


Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

 

Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

Process to the end of the file.
 

 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 

 

Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 

 

Sample Output
13



在BFS的搜索过程中,不能一判断出到达目标位置就退出BFS过程,否则求出来的仅仅只是r到a的最小步数。因为BFS所搜索的顶点都是按深度进行搜索的,所以BFS先搜索到的都是步数最少的,不一定是最优解,所用的时间可能更长。一定要等到链表为空,BFS搜索过程全部结束才能得出最优解或者得出无法找到目标位置的结论。
这一题并没有判断位置是否访问过,但是并不会无限循环下去。因为从某个位置出发判断是否要将它相邻的位置(x,y)入列,条件是这种走法比以前走到(x,y)所用的时间更少;
如果所用的时间更少,则(x,y)位置会重复入列,但不会无限下去。



 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 using namespace std;
 5 #define MAX 1000000
 6 int Map[250][250];
 7 int T[250][250];
 8 int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
 9 int n,m;
10 int si,sj,di,dj;
11 int sign=0;
12 typedef struct pointer
13 {
14     int x,y;
15     int time;
16     struct pointer *next;
17 } LNode,*LinkList;
18 LinkList ptr;
19 void bfs(LinkList head);
20 int main()
21 {
22     int i,j;
23     while(scanf("%d%d",&n,&m)!=EOF)
24     {
25         getchar();
26         for(i=0; i<n; i++)
27         {
28             for(j=0; j<m; j++)
29             {
30                 scanf("%c",&Map[i][j]);
31                 T[i][j]=MAX;
32                 if(Map[i][j]=='a')
33                 {
34                     di=i;
35                     dj=j;
36                 }
37                 else if(Map[i][j]=='r')
38                 {
39                     si=i;
40                     sj=j;
41                     T[si][sj]=0;
42                 }
43             }
44             getchar();
45         }
46         LinkList p;
47         p=(LinkList)malloc(sizeof(LNode));
48         p->x=si;
49         p->y=sj;
50         p->time=0;
51         p->next=NULL;
52         sign=0;
53         bfs(p);
54         if(T[di][dj]<MAX)cout<<T[di][dj]<<endl;
55         else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
56     }
57     return 0;
58 }
59 void bfs(LinkList head)
60 {
61     int i,fx,fy;
62     while(head!=NULL)
63     {
64         for(i=0; i<4; i++)
65         {
66             fx=head->x+dir[i][0];
67             fy=head->y+dir[i][1];
68             if(fx>=0&&fx<n&&fy>=0&&fy<m&&Map[fx][fy]!='#')
69             {
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇初识ege图形库 下一篇写一个Windows上的守护进程(4)..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目