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

解题思路: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 }

时间: 2025-01-08 14:45:49

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

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

[Swust OJ 404]--最小代价树(动态规划)

题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ (2+3))=((5)+(5))=10.除去原数不4,1,2,3之外,其余都为中间结果,如5,5,10,将中间结果相加

胜利大逃亡(续)(状态压缩bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

hdu1429胜利大逃亡(续) (状态压缩+BFS)

Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置.Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个.魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去.经过若干次的尝试,Ignatius已画

hdu1885Key Task(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 这题和hdu1429是姊妹题  请参见传送门 题目: Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1187    Accepted Submission(s): 470 Problem Description The Czech Technical University is rath