[Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/
BH is in a maze,the maze is a matrix,he wants to escape!
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).
The minimum time BH get to the goal cell,if he can't,print "Please help BH!".
|
5 5
....E
.....
.....
##...
S#...
5 8
........
........
..A....A
A######.
S......E
|
|
Please help BH!
12
|
由于OJ上传数据的BUG,换行请使用"\r\n",非常抱歉
题目大意:一个迷宫逃离问题,只是有了加速符A,正常情况下通过一个格子2s,有了加速符只要1s,并且加速符持续5s,‘S'代表起点
'E'代表终点,'#'代表障碍,'.'空格子,能够逃离输出最少用时,否则输出"Please help BH!"
解题思路:BFS,用一个3维dp数组存贮,每一点在不同加速状态下的值,然后筛选dp数组终点的最小值即可
代码如下:
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; #define maxn 101
#define inf 0x3f3f3f3f int dir[][] = { , , , , -, , , - };
int dp[maxn][maxn][];
int sx, sy, ex, ey, n, m;
char map[maxn][maxn]; struct node{
int x, y, step, speed;//spead加速
};
void bfs(){
node now, next;
now.x = sx, now.y = sy, now.step = , now.speed = ;
dp[sx][sy][] = ;
queue<node>Q;
Q.push(now);
while (!Q.empty()){
now = Q.front(); Q.pop();
for (int i = ; i < ; i++){
next = now;
next.x += dir[i][];
next.y += dir[i][];
if (next.x < || next.x >= n || next.y < || next.y >= m || map[next.x][next.y] == '#')continue;//不可行状态
if (next.speed){
//加速效果
next.speed--;
next.step++;
}
else next.step += ;
if (map[next.x][next.y] == 'A')next.speed = ;//获得加速神符
if (next.step < dp[next.x][next.y][next.speed]){
dp[next.x][next.y][next.speed] = next.step;
Q.push(next);
}
}
}
int ans = inf;
for (int i = ; i >= ; i--)
ans = min(ans, dp[ex][ey][i]);
if (ans >= inf)
cout << "Please help BH!\r\n";
else
cout << ans << "\r\n";
}
int main(){
while (cin >> n >> m){
memset(dp, inf, sizeof dp);
for (int i = ; i < n; i++){
cin >> map[i];
for (int j = ; j < m; j++){
if (map[i][j] == 'S')sx = i, sy = j;
if (map[i][j] == 'E')ex = i, ey = j;
}
}
bfs();
}
return ;
}
[Swust OJ 1023]--Escape(带点其他状态的BFS)的更多相关文章
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Pots POJ - 3414【状态转移bfs+回溯】
典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [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 ...
- [Swust OJ 385]--自动写诗
题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535 Descripti ...
随机推荐
- Open开发平台,认证,授权,计费
1.申请appid和appkeyhttp://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0 appid:应用的唯一 ...
- HUB主要芯片方案
HUB主要品牌:慧荣.擎泰.联盛 安国.创惟 创惟GL850G简介:拥有低耗电.温度低及接脚数减少等产品特性.它支援4个下游连接埠,采用48 pin LQFP封装,可完全支援USB 2.0/1.1规 ...
- NAND闪存颗粒结构及工作原理
NAND闪存是一种电压元件,靠其内存电压来存储数据,现在我们就来谈谈它的结构及工作原理. 闪存的内部存储结构是金属-氧化层-半导体-场效晶体管(MOSFET),里面有一个浮置栅极(Floating G ...
- hihoCoder 1388 Periodic Signal(FFT)
[题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...
- Noip2013心态调整
决定成绩的,很多时候可能不是实力,而是心态,一年走来,承受着一次次失败,怀疑,背负着希望与压力,突然发现,只有拥有过,失去过,才可以真正去超越,我希望完成我的梦想,但是唯有放下梦想,才可以走向它. 心 ...
- windows下Qt Creator5.1.0编写程序以及调用OpenCV库
系统说明 最近使用opencv编写程序,程序编的差不多就学习使用QT加个界面,首先声明下本人的系统和使用的软件版本, 系统: windows xp QT IDE:QT Creator5.1.0 Ope ...
- linux下用mail发送邮件
利用外部邮箱发送邮件的方法 bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动Sendmail服务,配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能 ...
- std::map中函数用法集合
1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点 2 eg: 3 map<string,string> mapTest; 4 typedef map< ...
- HTML静态网页(框架)
1.frameset frameset最外层,使用时需要去除body改用frameset. <frameset rows="100,*" frameborder=" ...
- ##DAY7 UINavigationController
##DAY7 UINavigationController #pragma mark ———————UINavigationController——————————— 概括: 导航视图控制器也是一个视 ...