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             {
70                 LinkList p;
71                 if(sign==0) ptr=head;
72                 p=(LinkList)malloc(sizeof(LNode));
73                 p->x=fx;
74                 p->y=fy;
75                 p->time=head->time+1;
76                 if(Map[fx][fy]==‘x‘) p->time++;
77                 if(p->time<T[fx][fy])
78                 {
79                     T[fx][fy]=p->time;
80                     ptr->next=p;
81                     p->next=NULL;
82                     ptr=ptr->next;
83                     sign=1;
84                 }
85             }
86         }
87         head=head->next;
88     }
89 }

时间: 2024-12-26 00:41:19

HDOJ1242 Rescue(营救)的相关文章

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

HDU 1242 Rescue 营救天使

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

Linux_破解密码-营救模式

实验用机:CentOS 5.7 破解密码 设置开机启动界面 系统运行级别 营救模式 一.破解密码 root用户可以更改任何用户的密码,普通用户只能修改自己的密码. 步骤: 1.重新启动系统 2.开机倒数时间内,敲任意键 3.按字母e去编辑 4.选择kernel 开头的行,再次按字母e 编辑 然后敲空格 输入数字1,再敲回车 进入单用户模式,破解密码.(此模式绕过密码验证) 5.按字母键b 去引导进入系统 6.直接passwd 修改root用户的密码 7.输入init 5或者init 3 进入系统

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

【网络流24题----14】孤岛营救问题

孤岛营救问题 Time Limit: 1 Sec  Memory Limit: 128 MB Description 1944年,特种兵麦克接到国防部的命令.要求马上赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形,其南北方向被划分为 N行,东西方向被划分为 M列,于是整个迷宫被划分为 N×M个单元.每个单元的位置可用一个有序数对 (单元的行号,单元的列号)来表示.南北或东西方向相邻的 2个单元之间可能互

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

BZOJ 2324: [ZJOI2011]营救皮卡丘( floyd + 费用流 )

昨晚写的题...补发一下题解... 把1~N每个点拆成xi, yi 2个. 预处理i->j经过编号不超过max(i,j)的最短路(floyd) S->0(K, 0), S->xi(1, 0)(从i点继续走), 0->yi(1, distance(0->i))(从0出发), xi->yi(1, distance(i->j))(i点走向j点), yi->T(1, 0)(每个点必须经过至少一次), 然后跑最小费用最大流, 费用即为答案. 写完这道题感觉...只是会

bzoj 2324 营救皮卡丘

2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1440  Solved: 560 Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点之间存在M条双向道路.据点分别从1到N标号.小智一行K人从真新镇出发,营救被困在N号据点的皮卡丘.为了方便起见,我们将真新镇

HDU 4057 Rescue the Rabbit (AC自动机+DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1482    Accepted Submission(s): 430 Problem Description Dr. X is a biologist,