题意:
一个 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;
}