0
点赞
收藏
分享

微信扫一扫

D. Labyrinth(bfs记忆化爆搜)


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 nm (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

The second line contains two integers rc (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 xy (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


题目大概:

给出地图,向左和向右有移动次数限制,问最多能够访问的点的数量。

思路:

bfs加个数组记忆化判断一下。

dp[x][y][0]是点(x,y)可以向左走的步数。
dp[x][y][1]是点(x,y)可以向右走的步数。
dp[x][y][2]是点(x,y)是否被走过。

代码:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=2100+100;
const int INF=0x3f3f3f3f;
const long long mod=1e9+7;
const double pi=acos(-1);
int dp[maxn][maxn][3];
int Map[maxn][maxn];
char f[maxn];
int dx[4]= {-1,1,0,0};
int dy[4]= {0,0,-1,1};
int n,m;
struct node
{
int x,y;
int l,r;
} st;

bool pan(int x,int y,int l,int r)
{
if(x>=1&&x<=n&&y>=1&&y<=m&&Map[x][y])
{
if(dp[x][y][0]<l||dp[x][y][1]<r||dp[x][y][2]==0)
{
dp[x][y][0]=l;
dp[x][y][1]=r;
dp[x][y][2]=1;
return 1;
}

}
return 0;
}
void bfs()
{
queue<node>Q;
Q.push(st);
dp[st.x][st.y][0]=st.l;
dp[st.x][st.y][1]=st.r;
dp[st.x][st.y][2]=1;
while(!Q.empty())
{
node u=Q.front();
Q.pop();

for(int i=0; i<4; i++)
{
int x=u.x+dx[i];
int y=u.y+dy[i];
int l=u.l;
int r=u.r;
if(i==2)l--;
if(i==3)r--;
if(l<0||r<0)continue;
if(pan(x,y,l,r))
{
node v;
v.x=x;
v.y=y;
v.l=l;
v.r=r;
Q.push(v);
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
scanf("%d%d",&st.x,&st.y);
scanf("%d%d",&st.l,&st.r);
for(int i=1; i<=n; i++)
{
scanf("%s",f+1);
for(int j=1; j<=m; j++)
{
int w=0;
if(f[j]=='.')w=1;
Map[i][j]=w;
}
}
bfs();
ll ans=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(dp[i][j][2])
{
ans++;
}
}
}
printf("%I64d\n",ans);
return 0 ;
}

 

举报

相关推荐

0 条评论