欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Labyrinth【BFS+优先队列】

程序员文章站 2022-05-20 20:24:06
...

题面

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

Note

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

First example:

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

Second example:

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

题目问你有X次走左边的方式与Y次走右边的方式,与无限次上下次的方式,问你能遍历到的最多的格子。

很明显是一道BFS的题目,但是,我们需要对它作出优化,不然可能会用到一个低(X+Y)的遍历走少的格子,所以我们要让遍历到的每个格子的同时能刚好是最大的(X+Y)形式。

于是,优先队列优化下即可。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN=2005;
int dir[4][2]=
{
    -1, 0,
    1, 0,
    0, -1,
    0, 1
};
int N, M, R, C, X, Y, ans;
char mp[maxN][maxN];
bool vis[maxN][maxN];
bool within(int x, int y) { return x>=1 && x<=N && y>=1 && y<=M; }
struct node
{
    int x, y, rest_x, rest_y;
    node(int a=0, int b=0, int c=0, int d=0):x(a), y(b), rest_x(c), rest_y(d) {}
};
struct cmp
{
    bool operator ()(node e1, node e2)
    {
        return (e1.rest_x+e1.rest_y)<(e2.rest_x+e2.rest_y);
    }
};
priority_queue<node, vector<node>, cmp > Q;
void init()
{
    while(!Q.empty()) Q.pop();
    memset(vis, false, sizeof(vis));
    ans=0;
}
void bfs()
{
    while(!Q.empty())
    {
        node now=Q.top(); Q.pop();
        int x=now.x, y=now.y;
        for(int i=0; i<4; i++)
        {
            int xx=x+dir[i][0], yy=y+dir[i][1];
            if(vis[xx][yy] || mp[xx][yy]=='*' || !within(xx, yy)) continue;
            if(i<=1)
            {
                Q.push(node(xx, yy, now.rest_x, now.rest_y));
                vis[xx][yy]=true;
                ans++;
            }
            else
            {
                if(i==2 && now.rest_x>0)
                {
                    Q.push(node(xx, yy, now.rest_x-1, now.rest_y));
                    vis[xx][yy]=true;
                    ans++;
                }
                if(i==3 && now.rest_y>0)
                {
                    Q.push(node(xx, yy, now.rest_x, now.rest_y-1));
                    vis[xx][yy]=true;
                    ans++;
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        scanf("%d%d", &R, &C);
        scanf("%d%d", &X, &Y);
        init();
        for(int i=1; i<=N; i++)
        {
            getchar();
            scanf("%s", mp[i]+1);
        }
        Q.push(node(R, C, X, Y));
        vis[R][C]=true;
        ans++;
        bfs();
        printf("%d\n", ans);
    }
    return 0;
}

 

相关标签: BFS