D. Labyrinth

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

10

题意:

        给你一个图,一个起点,你最多可以向左x次,向右y次,问你最多可以到达多少个点。

思路:

        2000*2000=4e6,很明显这是一个bfs,而且可以经过观察发现这是贪心的bfs,为什么这样说呢,无论我们向上走,还是向下走,我们所需要付出的代价都不会改变,所以在bfs时维护一下就行。

code:

      

#include
#include
#include
#include
using namespace std;
typedef long long LL;
struct node{
	int x;
	int y;
	int l;
	int r;
	node(int a,int b,int c,int d)
	{
		x=a;
		y=b;
		l=c;
		r=d;
	}
};
queueq,q2;
LL sx,sy,mx,my,n,m,ad,sz;
LL ans=0;
bool book[2010][2010];
char mymap[2010][2010];
int main()
{
	cin>>n>>m;
	cin>>sx>>sy;
	cin>>mx>>my;
	for(int i=1;i<=n;i++)
		scanf("%s",mymap[i]+1);
	node temp(sx,sy,0,0);
	book[sx][sy]=1;
	q.push(temp);
	ans++;
	while(!q.empty())
	{
		sz=q.size();
		int sz2=0;
		for(int i=1;i<=sz;i++)
		{
			node temp=q.front();
			q2.push(temp);
			q.pop();
			ad=1;
			while(temp.x+ad<=n&&mymap[temp.x+ad][temp.y]=='.'&&!book[temp.x+ad][temp.y])
			{
				book[temp.x+ad][temp.y]=1;
				q2.push(node(temp.x+ad,temp.y,temp.l,temp.r));
				ad++;
				ans++;
				sz2++;
			}
			ad=1;
			while(temp.x-ad>=1&&mymap[temp.x-ad][temp.y]=='.'&&!book[temp.x-ad][temp.y])
			{
				book[temp.x-ad][temp.y]=1;
				q2.push(node(temp.x-ad,temp.y,temp.l,temp.r));
				ad++;
				ans++;
				sz2++;
			}
		}
		sz+=sz2;
		for(int i=1;i<=sz;i++)
		{
			node temp=q2.front();
			q2.pop();
			ad=1;
			if(temp.y+ad<=m&&mymap[temp.x][temp.y+ad]=='.'&&!book[temp.x][temp.y+ad]&&temp.r+ad<=my)
			{
				book[temp.x][temp.y+ad]=1;
				q.push(node(temp.x,temp.y+ad,temp.l,temp.r+ad));
				ad++;
				ans++;
			}
			ad=1;
			if(temp.y-ad>=1&&mymap[temp.x][temp.y-ad]=='.'&&!book[temp.x][temp.y-ad]&&temp.l+ad<=mx)
			{
				book[temp.x][temp.y-ad]=1;
				q.push(node(temp.x,temp.y-ad,temp.l+ad,temp.r));
				ad++;
				ans++;
			}
		}
	}
	cout<

 

你可能感兴趣的:(bfs)