Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth

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:

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

分析:呃呃呃呃,这道题没有终测之前过的人挺多的,不过终测后都被fst了,当然我的也被fst,当时看到这道题没想太多,直接上宽搜暴力交了上去,结果后来想了想,不能直接按照宽搜来写,因为一个点可以有种途径来到达,我们要从这些途径中选出x,y最大的,因为他要尽可能的走更多的点。cf翻译群里大佬们说时最短路也可以写,我没有用最短路来写,我是在被fst的代码的基础上改了一下。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int N=1e5+10;
int n,m;
int r,c;
int Left,Right;
int idex=0;
char str[2005][2005];
int sum=0;
int dp[4][2]= {{0,-1},{0,1},{-1,0},{1,0}};
bool vis[2005][2005];
struct node
{
    int x;
    int y;
};
struct Node
{
    int l;
    int r;
} dis[2005][2005];
void bfs(int x1,int y1)
{
    queue<node> Q;
    node first;
    first.x=x1;
    first.y=y1;
    Q.push(first);
    vis[first.x][first.y]=true;
    dis[x1][y1].l=Left;
    dis[x1][y1].r=Right;
    while(!Q.empty())
    {
        node v=Q.front();
        Q.pop();
        node next;
        for(int i=0; i<4; i++)
        {
            next.x=v.x+dp[i][0];
            next.y=v.y+dp[i][1];
            if(i==0)
            {
                if(dis[v.x][v.y].l==0) continue;
            }
            else if(i==1)
            {
                if(dis[v.x][v.y].r==0) continue;
            }
            if(next.x<=0||next.x>n||next.y<=0||next.y>m||str[next.x][next.y]=='*')
                continue;
            str[next.x][next.y]='A';                          ///我把可以走到的点用A来标记了,最后在统计A的个数。
            if(i==0)       ///对应左移                                  
            {
                int flog=0;
                if(vis[next.x][next.y])                      ///当该点走过时,要判断一下新途径对应该点的l,r,选最大的l,r对应的情况。
                {
                    if(dis[v.x][v.y].l-1>dis[next.x][next.y].l)
                        dis[next.x][next.y].l=dis[v.x][v.y].l-1,flog=1;
                    if(dis[v.x][v.y].r>dis[next.x][next.y].r)
                        dis[next.x][next.y].r=dis[v.x][v.y].r,flog=1;
                }
                else
                {
                    flog=1;
                    dis[next.x][next.y].l=dis[v.x][v.y].l-1;
                    dis[next.x][next.y].r=dis[v.x][v.y].r;
                }
                if(flog)
                    Q.push(next);
            }
            else if(i==1)                ///对应右移
            {
                int flog=0;
                if(vis[next.x][next.y])                          ///同上i==0的情乱
                {
                    if(dis[v.x][v.y].l>dis[next.x][next.y].l)
                        dis[next.x][next.y].l=dis[v.x][v.y].l,flog=1;
                    if(dis[v.x][v.y].r-1>dis[next.x][next.y].r)
                        dis[next.x][next.y].r=dis[v.x][v.y].r-1,flog=1;
                }
                else
                {
                    flog=1;
                    dis[next.x][next.y].l=dis[v.x][v.y].l;
                    dis[next.x][next.y].r=dis[v.x][v.y].r-1;
                }
                if(flog)
                    Q.push(next);
            }
            else
            {
                int flog=0;
                if(vis[next.x][next.y])                      ///同上
                {
                    if(dis[v.x][v.y].l>dis[next.x][next.y].l)
                        dis[next.x][next.y].l=dis[v.x][v.y].l,flog=1;
                    if(dis[v.x][v.y].r>dis[next.x][next.y].r)
                        dis[next.x][next.y].r=dis[v.x][v.y].r,flog=1;
                }
                else
                {
                    flog=1;
                    dis[next.x][next.y].l=dis[v.x][v.y].l;
                    dis[next.x][next.y].r=dis[v.x][v.y].r;
                }
                if(flog)
                    Q.push(next);
            }
            vis[next.x][next.y]=true;
        }
    }
}
void CIN()
{
    scanf("%d%d",&r,&c);
    scanf("%d%d",&Left,&Right);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            scanf(" %c",&str[i][j]);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        CIN();
        memset(vis,false,sizeof(vis));
        sum=0;
        str[r][c]='A';
        bfs(r,c);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(str[i][j]=='A') sum++;
        printf("%d\n",sum);
    }
    return 0;
}
/*
20 7
3 6
5 2
......*
.****.*
.****.*
....*.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**.**.*
**....*
*******

10 4
1 1
10 3
....
.**.
.*..
.*.*
.*..
.*.*
.*.*
.*.*
.*.*
...*

*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值