题目描述:

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!".

理解:

总体来说,这个游戏是要玩最短路hhh,但是要判断有无加速向,最开始只想着用二维数组来记录从起点到每一个点的最短路径(距离),然后用queue正常做,只不过在结构体中多用一个buff元素来判断是否有吃到加速,然后每一次 if(que.front().buff > 0) temp.buff = que.front().buff-1;但由于经过同一点时,可能会有不同的路径,并且也可能有更短的出现,在标记buff和最短路难以权衡。

于是在某大大怪大牛的提醒下,在结构体中用step来记录最短路(这样就可以更新相同点的最短路),然后就是本题最核心也最有意思的地方了,用优先队列处理数据~。。由于同一点都可能有不同的路径,而不同的路径就会有可能有的路径有buff,有的没有(也可以理解为,同一个点先去捡一个buff,大致捡了buff的路径通常会最短?这个有待我思考。)于是用一个三维vis来记录同一个点出现的不同能量值,如果没标记过,就要标记起来并且压入的队列。

代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
bool vis[maxn][maxn][];
int way[][] = { {,}, {,-}, {,}, {-,} };
int mins;
struct node{
int x,y,step,buff;
friend bool operator < (const node a, const node b)
{
if(a.step == b.step) return a.buff < b.buff;
return a.step > b.step;
}
};
priority_queue<node> que;
int n, m, sx, sy, ex, ey;
char a[maxn][maxn];
void bfs(){
node temp, next, first;
vis[sx][sy][] = true;
first.x = sx, first.y = sy, first.step = , first.buff = ;
que.push(first);
while(!que.empty()){
temp = que.top();
que.pop();
if(temp.x == ex && temp.y == ey){
mins = min(mins, temp.step);
break;
}
else
{
for(int i = ; i < ; i++)
{
int dx = temp.x+way[i][];
int dy = temp.y+way[i][];
if(dx< || dx>=n || dy< || dy>=m || a[dx][dy] == '#') continue;
if(temp.buff > ){
next.buff = temp.buff - ;
next.step = temp.step + ;
}else{
next.buff = ;
next.step = temp.step + ;
}
if(a[dx][dy] == 'A')
next.buff = ;
if(vis[dx][dy][next.buff])
continue;
vis[dx][dy][next.buff] = true;
next.x = dx, next.y = dy;
que.push(next);
}
}
}
return ;
} int main()
{
while(cin >> n >> m){
mins = INF;
memset(vis, false, sizeof(vis));
memset(a, , sizeof(a));
while(!que.empty()) que.pop();
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
cin >> a[i][j];
for(int i = ; i < n; i++)
for(int j = ; j < m; j++){
if(a[i][j] == 'S')
sx = i, sy = j;
else if(a[i][j] == 'E')
ex = i, ey = j;
}
bfs();
if(mins == INF)
cout << "Please help BH!" << "\r\n";
else
cout << mins << "\r\n";
}
return ;
}
 

Escape(反思与总结)的更多相关文章

  1. [考试反思]1029csp-s模拟测试93:殇逝

    并不是把它消成上三角矩阵 停止! 思考, 回顾. 疑惑? 遗忘… 一直只是在匆忙的赶进度,实际上的确是一点也不扎实. T1,裸的偏序,想了一个多小时什么也没想到,只打了$O(n^2)$ 难道之前学的就 ...

  2. Noip2016 总结&反思

    一直在期盼的联赛,真正来临时,却远不像我想象的样子. 有些事,真的不敢再想. 算法可以离线,时光却不能倒流.dfs可以回溯,现实却没有如果. 有些事,注定只能成为缺憾,抱恨终生. 不得不说今年Noip ...

  3. 关于2016.12.12——T1的反思:凸包的意义与应用

    2016.12.12 T1 给n个圆,保证圆圆相离,求将圆围起来的最小周长.n<=100 就像上图.考场上,我就想用切线的角度来做凸包.以圆心x,y排序,像点凸包一样,不过用两圆之间的下切线角度 ...

  4. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  5. 简单明了区分escape、encodeURI和encodeURIComponent

    一.前言 讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它们不是同一类 简单来说,escape是对字符串(string)进行编码(而另外两种 ...

  6. c#模拟js escape方法

    public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...

  7. 【BZOJ-1340】Escape逃跑问题 最小割

    1340: [Baltic2007]Escape逃跑问题 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 264  Solved: 121[Submit] ...

  8. LYDSY热身赛 escape

    Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上矩形的行 ...

  9. 上个项目的一些反思 I

    最近一直在反思之前的项目,发现了很多问题.比如数据安全... 虽然项目需求是只展示最新的数据,所以几乎没用什么本地存储.除了通讯录和用户的Token. 用户通讯录另表,今天反思下用户的Token的存储 ...

随机推荐

  1. python笔记05

    python笔记05 数据类型 上个笔记知识点总结: 列表中extend特性:extend,(内部循环,将另外一个列表,字符串.元组添加到extend前的列表中) li.extend(s),将s中元素 ...

  2. JPA基本注解的使用

    一:JPA基本注解 使用: 使用: 使用: 查看表: 二:用table来生成主键 使用: allocationSize:每次增加多少 tablel:指定使用那张表 执行两次main方法后查看表: jp ...

  3. CSS-04-层叠选择器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 数据库中事务的ACID特性

    数据库中事务的ACID特性 前言前面我们介绍过数据库中 带你了解数据库中JOIN的用法 与 带你了解数据库中group by的用法 的相关用法.本章节主要来介绍下数据库中一个非常重要的知识点事务,也是 ...

  5. 本地Linux虚拟机内网穿透,服务器文件下载到本地磁盘

    本地Linux虚拟内网穿透 把服务器文件下载到本地磁盘 https://natapp.cn/ 1.注册账户点击免费隧道  

  6. git上传本地代码到远程失败

    出现这种错误的原因是由于我不小心勾选了这个

  7. MCMC&Gibbs sampling

    Note of Markov Chain Monte Carlo and Gibbs Sampling :  http://pan.baidu.com/s/1jHpWY1o 序:A major lim ...

  8. 1-NoSQL介绍及Redis安装

    背景 随着互联网的不断发展和软件架构的不断复杂化,同时随着网站访问量的日渐上升,导致传统单机关系型数据库应用已经无法满足人们的需求,在高并发的场景下,频繁的数据库存取操作使得服务器压力剧增,甚至导致服 ...

  9. c和c++中读取数据的方式总结

    目录 c 输出 printf() 输入 scanf getchar(), putchar() gets(), puts() c++ 输入 cin() getline() get() 输出 cout 最 ...

  10. .windows模拟linux命令iostat的显示

    脚本如下: #!/usr/bin/env python #coding:utf- import win32com.client import time def disk_status(): try: ...