题意:

一个 n×mn\times mn×m 的图,现在有一束激光从左上角往右边射出,每遇到 ‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个 ‘#’ 往四个方向射出才能使光线在第 n 行往右边射出。

题目:

“The Chamber of Secrets has been opened again” — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren’t good news for Lord Voldemort. The problem is, he doesn’t want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.

The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk’s gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below.
The left light ray passes through a regular column, and the right ray — through the magic column.
The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk’s gaze directly dies immediately. But if someone meets a basilisk’s gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction
of the upper right cell) from that position.

This figure illustrates the first sample test.
Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it’s impossible to secure the chamber.

Input

The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either “.” or “#” and represents one cell of the Chamber grid. It’s “.” if the corresponding cell is empty and “#” if it’s a regular column.

Output

Print the minimum number of columns to make magic or -1 if it’s impossible to do.

Examples

Input

3 3
.#.

.#.

Output

2

Input

4 3
##.

.#.
.#.

Output

2

Note

The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.

分析:

1.此题目正解不是 0-1 BFS 但是适用 0-1 BFS 可以不需要思考过程,赛时许多大佬都是这么做的。
2.做法很简单,一个方向射出不需要花费(0),而往四个方向射出需要花费(1),然后直接来就可以了。
3.复习双端队列deque
std::deque 是 STL 提供的 双端队列 数据结构。能够提供线性复杂度的插入和删除,以及常数复杂度的随机访问。

  • 构造函数
    参见如下代码(假设你已经 using 了 std 命名空间相关类型):
// 1. 定义一个int类型的空双端队列 v0
deque<int> v0;
// 2. 定义一个int类型的双端队列 v1,并设置初始大小为10; 线性复杂度
deque<int> v1(10);
// 3. 定义一个int类型的双端队列 v2,并初始化为10个1; 线性复杂度
deque<int> v2(10, 1);
// 4. 复制已有的双端队列 v1; 线性复杂度
deque<int> v3(v1);
// 5. 创建一个v2的拷贝deque v4,其内容是v4[0]至v4[2]; 线性复杂度
deque<int> v4(v2.begin(), v2.begin() + 3);
// 6. 移动v2到新创建的deque v5,不发生拷贝; 常数复杂度; 需要 C++11
deque<int> v5(std::move(v2));
  • 元素访问
    与 vector 一致,但无法访问底层内存。其高效的元素访问速度可参考实现细节部分。
函数 作用
at() 返回容器中指定位置元素的引用,执行越界检查,常数复杂度。
operator[] 返回容器中指定位置元素的引用。不执行越界检查,常数复杂度。
front() 返回首元素的引用。
back() 返回末尾元素的引用。
  • 元素增删及修改
    与 vector 一致,并额外有向队列头部增加元素的函数。
函数 作用
clear() 清除所有元素
insert() 支持在某个迭代器位置插入元素、可以插入多个。复杂度与 pos 与两端距离较小者成线性。
erase() 删除某个迭代器或者区间的元素,返回最后被删除的迭代器。复杂度与 insert 一致。
push_front() 在头部插入一个元素,常数复杂度。
pop_front() 删除头部元素,常数复杂度。
push_back() 在末尾插入一个元素,常数复杂度。
pop_back() 删除末尾元素,常数复杂度。
swap() 与另一个容器进行交换,此操作是 常数复杂度 而非线性的。
  • deque 的实现细节
    deque 通常的底层实现是多个不连续的缓冲区,而缓冲区中的内存是连续的。而每个缓冲区还会记录首指针和尾指针,用来标记有效数据的区间。当一个缓冲区填满之后便会在之前或者之后分配新的缓冲区来存储更多的数据。

AC代码:

#include<stdio.h>
#include<string.h>
#include<deque>
#include<iostream>
using namespace std;
const int M=1e3+10;
const int inf=0x3f3f3f3f;
int n,m;
char mp[M][M];
int f[M][M][10];
int c[4][2]= {1,0,-1,0,0,1,0,-1};
deque<int>q;
void Add_front(int x,int y,int dir,int step)
{if(step<f[x][y][dir]){q.push_front(dir);//放在首位的需要倒着放入q.push_front(y);q.push_front(x);f[x][y][dir]=step;}
}
void Add_back(int x,int y,int dir,int step)
{if(step<f[x][y][dir]){q.push_back(x);q.push_back(y);q.push_back(dir);f[x][y][dir]=step;}
}
int main()
{cin>>n>>m;for(int i=0; i<n; i++)cin>>mp[i];for(int i=0; i<n; i++)for(int j=0; j<m; j++)for(int k=0; k<4; k++)f[i][j][k]=inf;Add_front(n-1,m-1,3,0);while(!q.empty()){int x=q[0];int y=q[1];int dir=q[2];q.pop_front();q.pop_front();q.pop_front();int xx=x+c[dir][0];int yy=y+c[dir][1];if(xx>=0&&yy>=0&&xx<n&&yy<m)Add_front(xx,yy,dir,f[x][y][dir]);if(mp[x][y]=='#'){for(int i=0; i<4; i++){if(i!=dir)Add_back(x,y,i,f[x][y][dir]+1);}}}if(f[0][0][3]==inf)cout<<"-1"<<endl;elsecout<<f[0][0][3]<<endl;return 0;
}

双端队列 BFS + Chamber of Secrets CodeForces - 173B相关推荐

  1. AcWing 2019. 拖拉机(双端队列BFS)

    [题目描述] 干了一整天的活,农夫约翰完全忘记了他把拖拉机落在田地中央了. 他的奶牛非常调皮,决定对约翰来场恶作剧. 她们在田地的不同地方放了NNN捆干草,这样一来,约翰想要开走拖拉机就必须先移除一些 ...

  2. 洛谷P1346 电车(双端队列BFS)

    [题目描述] 在一个神奇的小镇上有着一个特别的电车网络,它由一些路口和轨道组成,每个路口都连接着若干个轨道,每个轨道都通向一个路口(不排除有的观光轨道转一圈后返回路口的可能).在每个路口,都有一个开关 ...

  3. 广度优先搜索BFS进阶(一):多源BFS、优先队列BFS、双端队列BFS

    一.多源BFS 在上一篇博客:广度优先搜索BFS基础中,我们接触到的BFS均是单起点(单源)的,但是对于某一些问题,其有多个起点,此类问题我们称为多源BFS问题.先思考下面一道例题: 1.腐烂的橘子 ...

  4. 双端队列BFS:拖拉机

    原题链接:https://www.acwing.com/problem/content/2021/ 一个裸的双端队列广搜. #include <iostream> #include < ...

  5. 电路维修 -> 双端队列 BFS

    [问题描述] Elf是来自Gliese星球的少女,她有一辆飞行车.飞行车电路板的整体结构是一个R行C列的网格(R,C≤500),网格的交点都是电路板的接点,每个格子都包含一个电子元件.电子元件的主要部 ...

  6. 【搜索专题】BFS中的多源BFS-双端队列BFS

    A.AcWing 173. 矩阵距离(多源BFS) 所有点到多个终点的最短距离 我们可以建一个虚拟源点,虚拟源点到所有终点连0权的边,跑一次单源最短路即可 答案就是每一个点到虚拟源点的最近距离 先将d ...

  7. Python 数据结构与算法 —— list与deque(双端队列)

    TimeComplexity - Python Wiki 1. 底层数据结构 list 的底层是数组(array),其最大的时间空间消耗出现在存储元素增长超过当前数组分配的大小时,所有元素都必须移动到 ...

  8. 【双端队列广搜/搜索+图论】AcWing 2019.拖拉机 USACO 2012 March Contest Silver Division

    [题目描述] 干了一整天的活,农夫约翰完全忘记了他把拖拉机落在田地中央了. 他的奶牛非常调皮,决定对约翰来场恶作剧. 她们在田地的不同地方放了 NNN 捆干草,这样一来,约翰想要开走拖拉机就必须先移除 ...

  9. 2019 拖拉机(双端队列广搜)

    1. 问题描述: 干了一整天的活,农夫约翰完全忘记了他把拖拉机落在田地中央了.他的奶牛非常调皮,决定对约翰来场恶作剧.她们在田地的不同地方放了 N 捆干草,这样一来,约翰想要开走拖拉机就必须先移除一些 ...

最新文章

  1. Google I/O 2014 - Keynote for Android
  2. 马云缺席的一个半小时,李彦宏和马化腾都聊了什么
  3. vs2010编译生成后清除obj目录
  4. 左斜杠和右斜杠有什么区别_「斜杠云」SEO推广和SEO优化有什么区别?
  5. oracle11g中的join,sql - 使用Oracle 11g中的+符号进行左外连接
  6. java线程通讯的方式
  7. Unity Heathaze shader
  8. array函数参数 scala_3小时Scala入门
  9. 【语谱图】基于matlab语音信号语谱图【含Matlab源码 137期】
  10. arcgis发布路网路径规划服务
  11. 个人游戏开发者是如何盈利
  12. 2 ubuntu下geographiclib的使用--经纬度坐标转utm平面坐标及重置ECEF原点
  13. RS-485什么情况下需要上下拉电阻?
  14. 【阅读笔记】量子信息
  15. MapReduce 与 Database 的乌龙战
  16. 汇编指令CALL的硬编码E8 和 JMP 的硬编码E9 后面参数的计算方法
  17. red hat 系统下载
  18. HLOJ 1562* 手指游戏
  19. 计算机系统组装音乐制作型,电脑音乐制作系统、设备购买方案
  20. 3D光场重建率先被中国团队推向零售商用!谷歌同款技术,哈佛高材生创办,高通投资认定...

热门文章

  1. CityEngine Web Scene如何在IIS下部署
  2. IOS之Xcode之快捷键
  3. 安卓模拟器获取服务器信息出错,安卓模拟器客户端与服务器不同步
  4. 如何在Clion中使用C++调用Python代码
  5. 机器学习与数据挖掘——第二章 数据与数据预处理
  6. python ctypes 指针_Python Ctypes传递.h文件中定义的结构指针。
  7. 用柠檬来发电真的可行吗?
  8. 饿了么翻车,美团被质疑,马云也赔了40亿,这项技术为什么让人害怕?
  9. 网页设计的css样式,网页设计引入CSS样式的五种方式_css
  10. 湘乡江南计算机学校,湘乡职业中等专业学校2021年招生录取分数线