B. Labyrinth(BFS 记忆化搜索)

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 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

Copy

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

output

Copy

10

input

Copy

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

output

Copy

7

Note

记忆化搜索。dp[0] 向左 dp[1] 向右 dp[2] 表示这个点能够满足条件

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
char map[2005][2005];
int step[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int vist[2005][2005];
int dp[2005][2005][3];
int n,m;
struct node{
	int x;
	int y;
	int r;
	int l;
};
node star;
int judge(int x,int y,int l,int r){
			if(x>=1&&x<=n&&y>=1&&y<=m&&vist[x][y]){
				if(dp[x][y][0]<l||dp[x][y][1]<r||!dp[x][y][2]){
					dp[x][y][0]=l;
					dp[x][y][1]=r;
					dp[x][y][2]=1;
					return 1;
				}
			}
			return 0;
}
void bfs(){
	memset(dp,0,sizeof(dp));
	queue<node>q;
	 node next;
	 dp[star.x][star.y][0]=star.l;
	 dp[star.x][star.y][1]=star.r;
	 dp[star.x][star.y][2]=1;
	q.push(star);
	while(!q.empty()){
		node cur=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int x=cur.x+step[i][0];
			int y=cur.y+step[i][1];
			if(map[x][y]=='*')continue;
			//cout<<next.x<<" "<<next.y<<endl;
			int l=cur.l;
			int r=cur.r;
			if(i==1) r--;
			if(i==3) l--;
			if(l<0||r<0) continue;
			if(judge(x,y,l,r)){
				//cout<<sum<<endl;
				next.x=x;
				next.y=y;
				next.r=r;
				next.l=l;
				q.push(next);
			}
		}
	}
}
int main(){
	cin>>n>>m>>star.x>>star.y>>star.l>>star.r;
	memset(vist,0,sizeof(vist));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>map[i][j];
			if(map[i][j]=='.')
			vist[i][j]=1;
		}
	}
	int sum=0;
	bfs();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(dp[i][j][2]==1){
				sum++;
			}
		}
	
	}
	cout<<sum<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41453511/article/details/85048254