码迷,mamicode.com
首页 > 其他好文 > 详细

[Swust OJ 1023]--Escape(带点其他状态的BFS)

时间:2015-06-22 16:09:46      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 

解题思路:http://acm.swust.edu.cn/problem/1023/

Time limit(ms): 5000        Memory limit(kb): 65535
 
 
Description

BH is in a maze,the maze is a matrix,he wants to escape!

 
Input

The input consists of multiple test cases.

For each case,the first line contains 2 integers N,M( 1 <= N, M <= 100 ).

Each of the following N lines contain M characters. Each character means a cell of the map.

Here is the definition for chracter.

For a character in the map: 

‘S‘:BH‘s start place,only one in the map.

‘E‘:the goal cell,only one in the map.

‘.‘:empty cell.

‘#‘:obstacle cell.

‘A‘:accelerated rune.

BH can move to 4 directions(up,down,left,right) in each step.It cost 2 seconds without accelerated rune.When he get accelerated rune,moving one step only cost 1 second.The buff lasts 5 seconds,and the time doesn‘t stack when you get another accelerated rune.(that means in anytime BH gets an accelerated rune,the buff time become 5 seconds).

 
 
Output

The minimum time BH get to the goal cell,if he can‘t,print "Please help BH!".

 
Sample Input
5 5
....E
.....
.....
##...
S#...
 
5 8
........
........
..A....A
A######.
S......E

Sample Output
Please help BH!
12

 

由于OJ上传数据的BUG,换行请使用"\r\n",非常抱歉

 

题目大意:一个迷宫逃离问题,只是有了加速符A,正常情况下通过一个格子2s,有了加速符只要1s,并且加速符持续5s,‘S‘代表起点

     ‘E‘代表终点,‘#‘代表障碍,‘.‘空格子,能够逃离输出最少用时,否则输出"Please help BH!"

解题思路:BFS,用一个3维dp数组存贮,每一点在不同加速状态下的值,然后筛选dp数组终点的最小值即可

代码如下:

技术分享
 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 using namespace std;
 5 
 6 #define maxn 101
 7 #define inf 0x3f3f3f3f
 8 
 9 int dir[][2] = { 1, 0, 0, 1, -1, 0, 0, -1 };
10 int dp[maxn][maxn][6];
11 int sx, sy, ex, ey, n, m;
12 char map[maxn][maxn];
13 
14 struct node{
15     int x, y, step, speed;//spead加速
16 };
17 void bfs(){
18     node now, next;
19     now.x = sx, now.y = sy, now.step = 0, now.speed = 0;
20     dp[sx][sy][0] = 0;
21     queue<node>Q;
22     Q.push(now);
23     while (!Q.empty()){
24         now = Q.front(); Q.pop();
25         for (int i = 0; i < 4; i++){
26             next = now;
27             next.x += dir[i][0];
28             next.y += dir[i][1];
29             if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m || map[next.x][next.y] == #)continue;//不可行状态
30             if (next.speed){
31                 //加速效果
32                 next.speed--;
33                 next.step++;
34             }
35             else next.step += 2;
36             if (map[next.x][next.y] == A)next.speed = 5;//获得加速神符
37             if (next.step < dp[next.x][next.y][next.speed]){
38                 dp[next.x][next.y][next.speed] = next.step;
39                 Q.push(next);
40             }
41         }
42     }
43     int ans = inf;
44     for (int i = 4; i >= 0; i--)
45         ans = min(ans, dp[ex][ey][i]);
46     if (ans >= inf)
47         cout << "Please help BH!\r\n";
48     else
49         cout << ans << "\r\n";
50 }
51 int main(){
52     while (cin >> n >> m){
53         memset(dp, inf, sizeof dp);
54         for (int i = 0; i < n; i++){
55             cin >> map[i];
56             for (int j = 0; j < m; j++){
57                 if (map[i][j] == S)sx = i, sy = j;
58                 if (map[i][j] == E)ex = i, ey = j;
59             }
60         }
61         bfs();
62     }
63     return 0;
64 }
View Code

 

[Swust OJ 1023]--Escape(带点其他状态的BFS)

标签:

原文地址:http://www.cnblogs.com/zyxStar/p/4593378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!