D. Labyrinth
time limit per test 2 seconds
memory limit per test 512 megabytes
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.
Examples
input
4 5 3 2 1 2 ..... .***. ...** *....
output
10
input
4 4 2 2 0 1 .... ..*. .... ....
output
7
Note
Cells, reachable in the corresponding example, are marked with '+'.
First example:
+++.. +***. +++** *+++.
Second example:
.++. .+*. .++. .++.
之前用普通的队列行不通,因为这种情况会漏一个点
20 7
3 6
5 2
......*
.****.*
.****.*
...¥*.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**....*
*******
// ¥为将漏掉的点
然后就选则用双向队列,没有限制的上下走先解决掉,再来走有限制的左右存放的时候放在队列的后面即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
struct Node{
int r;
int c;
int L;
int R;
Node (int rr,int cc,int ll,int _r) : r(rr),c(cc),L(ll),R(_r) {}
};
deque <Node> q;
bool visited[2005][2005];
char v[2005][2005];
int main(){
int n,m;
int r0,c0;
int lx,ry;
char ss;
scanf("%d%d",&n,&m);
scanf("%d%d",&r0,&c0);
scanf("%d%d",&lx,&ry);
getchar();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%c",&ss);
v[i][j] = ss;
}
getchar() ;
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++){
// printf("%c ",v[i][j]);
// }
// putchar('\n') ;
// }
q.push_front(Node(r0,c0,lx,ry));
int ans=1;
memset(visited,false,sizeof(visited));
visited[r0][c0]=true;
while(!q.empty()){
Node p=q.front();
q.pop_front();
int r1,c1;
//LL
r1=p.r; c1=p.c-1;
if(1<=r1&&r1<=n&&1<=c1&&c1<=m && (p.L-1)>=0 && p.R>=0&&!visited[r1][c1]&&v[r1][c1]!='*'){
ans++;
q.push_back(Node(r1,c1,p.L-1,p.R));visited[r1][c1]=true;
//printf("(%d,%d) ",r1,c1);
}
//RR
r1=p.r; c1=p.c+1;
if(1<=r1&&r1<=n&&1<=c1&&c1<=m&&(p.R-1)>=0&&p.L>=0&&!visited[r1][c1]&&v[r1][c1]!='*'){
ans++;
q.push_back(Node(r1,c1,p.L,p.R-1));visited[r1][c1]=true;
//printf("(%d,%d) ",r1,c1);
}
//Up
r1=p.r+1; c1=p.c;
if(1<=r1&&r1<=n&&1<=c1&&c1<=m&&p.L>=0&&p.R>=0&&!visited[r1][c1]&&v[r1][c1]!='*'){
ans++;
q.push_front(Node(r1,c1,p.L,p.R));visited[r1][c1]=true;
//printf("(%d,%d) ",r1,c1);
}
//down
r1=p.r-1; c1=p.c;
if(1<=r1&&r1<=n&&1<=c1&&c1<=m&&p.L>=0&&p.R>=0&&!visited[r1][c1]&&v[r1][c1]!='*'){
ans++;
q.push_front(Node(r1,c1,p.L,p.R));visited[r1][c1]=true;
//printf("(%d,%d) ",r1,c1);
}
}
printf("%d",ans);
}