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

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

  1. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

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

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  3. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

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

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. Pots POJ - 3414【状态转移bfs+回溯】

    典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. Radio Checkbox Select 操作

    一个小总结 <!DOCTYPE html> <html> <head> <meta name="description" content= ...

  2. linux下如何查看系统和内核版本

        1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@cro ...

  3. 《windows程序设计》学习_2.2:初识消息,双键的使用

    /* 双键的使用 */ #include <windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI ...

  4. Lua环境配置 windows + VS

    环境搭建: 首先从 http://www.lua.org/ftp/下载lua 源码, 我选择的是lua-5.1.5.tar.gz 我的开发环境是Win7+ VS2010 打开VS2010新建一个工程L ...

  5. android程序启动画面之Splash总结[转]

    方法一: 很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐.实现这种效果的方法有两种(暂时只发现两种)1.使用两个Activity,程序启动时候load第一张Activity,然后由tick ...

  6. ANDROID对文件的操作

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  7. raphael入门到精通---入门篇之总览

    什么是Raphael raphael.js是一小巧的javascript库,它可以在web上画矢量图简化你的工作,如果你想创建你指定的图表,图形区域或者可移动的组件,那么就使用raphael吧 话不多 ...

  8. iOS面试题04-runtime

    runtime/KVO等面试题 1.KVO内部实现原则 回答:1>KVO是基于runtime机制实现的 2>当某个类的对象第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个 ...

  9. BZOJ 2662: [BeiJing wc2012]冻结(最短路)

    这道题和 BZOJ 2763飞行路线 几乎一模一样..然后飞行路线我是1A,这道题WA了4次,我开始怀疑我的智商了.. ---------------------------------------- ...

  10. windows server2008 r2修改远程桌面连接端口。

    1. windows 2008远程桌面端口默认是用的是3389端口,但是由于安全考虑,通常我们安装好系统后一般都会考虑把原来的3389端口更改为另外的端口.   2.更改过程: 2-1.打开注册表:  ...