D. Labyrinth(bfs+记忆化)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/83088771

D. Labyrinth

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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

Copy

4 5
3 2
1 2
.....
.***.
...**
*....

Output

Copy

10

Input

Copy

4 4
2 2
0 1
....
..*.
....
....

Output

Copy

7

Note

Cells, reachable in the corresponding example, are marked with '+'.

First example:

+++..
+***.
+++**
*+++.

Second example:

.++.
.+*.
.++.
.++.

题意:

就是给定了起点,往左,右走限制的步数,然后问能到达的有几个点。

就是记忆化+bfs,只要这个点多次走过,并且当前的步数小于之前的值,那么我就更新他。

之前写的一发WA点在于我直接bfs,但是有一组样例会有第二次经过,而我没有考虑,没有搜到。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int maxn=3010;
char mp[3110][3010];
///int vis[maxn][maxn];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int biaozhil[maxn][maxn];
int biaozhir[maxn][maxn];
int num[maxn][maxn];
struct node{
    int x,y;
    int L,R;
    node(int a,int b,int c,int d){x=a;y=b;L=c;R=d;}
};
queue<node> Q;
int n,m,lef,righ,q,r;
void bfs(int q,int r){
   /// memset(vis,0,sizeof(vis));
    memset(biaozhil,-1,sizeof(biaozhil));
    memset(biaozhir,-1,sizeof(biaozhir));
    while(!Q.empty()){
        Q.pop();
    }
    node f(q,r,0,0);
    Q.push(f);///起点
///    vis[q][r]=1;
    biaozhil[q][r]=0;
    biaozhir[q][r]=0;
    while(!Q.empty()){ ///lala1是left,lala2是right
        node a=Q.front();
        int lala1,lala2;
      ///  vis[a.x][a.y]=1;
        num[a.x][a.y]=1;
        Q.pop();
        int x=a.x;
        int y=a.y;
        for(int i=0;i<4;i++){
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
    ///        cout<<"xx:"<<xx<<"   "<<"yy:"<<yy<<endl;
            if(i==2)///right
            {
                lala1=a.L;
                lala2=a.R+1;
            }
            else if(i==3){
                lala1=a.L+1;
                lala2=a.R;
            }
            else{
                lala1=a.L;
                lala2=a.R;
            }
            if(lala2>righ || lala1>lef)
                continue;
            if(xx<=0 || xx>n || yy<=0 || yy>m)
                    continue;
  ///          if(vis[xx][yy]) continue;
            if(mp[xx][yy]=='*'){
                continue;
            }
            if(biaozhil[xx][yy]!=-1 && lala1>=biaozhil[xx][yy])
                continue;
            if(biaozhir[xx][yy]!=-1 && lala2>=biaozhir[xx][yy])
                continue;
///            vis[xx][yy]=1;
            biaozhil[xx][yy]=lala1;
            biaozhir[xx][yy]=lala2;
            node f(xx,yy,lala1,lala2);
            num[xx][yy]=1;
            Q.push(f);
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    scanf("%d%d",&q,&r);
    scanf("%d%d",&lef,&righ);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mp[i][j];
        }
    }
    memset(num,0,sizeof(num));
    bfs(q,r);
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(num[i][j])
            {
                ans++;
     ///           cout<<"i:"<<i<<"   j:"<<j<<endl;
            }
        }
    }
    printf("%d\n",ans);
}
/*

4 5
3 2
1 2
.....
.***.
...**
*....

*/
/*
2 2000

2 1007

69 63

............*..*.........*.......*...............*..........................................*.....*................................*..*...........*.....***.*................*.................*....*..........*...*......*....*........*......................*........................*.....**.......*.....................*..*..........*.......*...............*......*............*..............*...*............................*..*...................*..*......*...*.....*.....*..................
*/

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/83088771