【题解】CF1063B Labyrinth(BFS)
作者:互联网
原题戳这里---> https://www.luogu.com.cn/problem/CF1063B
题目描述
You are playing some computer game.
One of its levels puts you in a maze consisting of n lines,
each of which contains m cells. Each cell either is free or is occupied by an obstacle.
The starting cell is in the row r and column c.
In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle.
You can't move beyond the boundaries of the labyrinth.
Unfortunately, your keyboard is about to break, so you can move left no more than x times
and move right no more than y times. There are no restrictions on the number of moves up and down
since the keys used to move up and down are in perfect condition.
Now you would like to determine for each cell whether there exists a sequence of moves
that will put you from the starting cell to this particular one.
How many cells of the board have this property?
Input
The first line contains two integers n,m(1 ≤ n, m ≤ 2000)
— the number of rows and the number columns in the labyrinth respectively.
The second line contains two integers r,c(1 ≤ r ≤ n,1 ≤ c ≤ m)
— index of the row and index of the column that define the starting cell.
The third line contains two integers x,y(0 ≤ x, y ≤ 109)
— the maximum allowed number of movements to the left and to the right respectively.
The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'.
The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j.
Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.
It is guaranteed, that the starting cell contains no obstacles.
Output
Print exactly one integer
— the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.
Sample 1
Input
4 5
3 2
1 2
.....
.∗∗∗.
...∗∗
∗....
Output
10
Sample 2
Input
4 4
2 2
0 1
....
..*.
....
....
Output
7
分析及代码实现
-
这道题核心就是BFS,但是和普通的宽搜不一样,本题有向左向右的限制。
-
如果按照一般的思路,可能会先做花一些代价的方案到某一个点而把不用花代价的方案给排掉了
-
所以我们可以借助双端队列(deque)
-
在队列中记录 坐标 以及 当前还剩下的左右移动的次数
-
每次把不用花代价的方案放到队首,把花代价的放到队尾,每次从队首取点即可
C++代码实现
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <deque>
using namespace std;
struct node{
int a, b, lc, rc;
};
const int N = 2022;
int n, m, r, c, x, y, cnt;
char g[N][N];
deque<node> q;
bool v[N][N];
int main(){
scanf("%d%d%d%d%d%d", &n, &m, &r, &c, &x, &y);
for (int i = 1; i <= n; i++) scanf("%s", g[i] + 1);
q.push_back({r, c, x, y});
while(q.size()){
auto t = q.front();
q.pop_front();
int a = t.a, b = t.b;
if(v[a][b]) continue;
v[a][b] = true, cnt++;
if (a - 1 >= 1 && g[a - 1][b] == '.')
q.push_front({a - 1, b, t.lc, t.rc});
if (a + 1 <= n && g[a + 1][b] == '.')
q.push_front({a + 1, b, t.lc, t.rc});
if (b - 1 >= 1 && g[a][b - 1] == '.' && t.lc)
q.push_back({a, b - 1, t.lc - 1, t.rc});
if (b + 1 <= m && g[a][b + 1] == '.' && t.rc)
q.push_back({a, b + 1, t.lc, t.rc - 1});
}
printf("%d\n", cnt);
return 0;
}
标签:d%,Labyrinth,题解,move,number,cell,CF1063B,contains,starting 来源: https://www.cnblogs.com/Yi-Shan/p/15819545.html