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:
.++. .+*. .++. .++.
题意:左右只能走有限的次数,问能去多少个位置
解题思路:按照走迷宫的思路,直接宽搜,但是左右有次数限制,并且上下没有限制,所以我们要记录一下还能走多少步左右。但是直接宽搜是不行的,因为最快走到的,不一定是最优的(他有可能使用了大量的左右)。所以我们要用优先队列,让使用左右次数较少的,先走,然后上下的,一次性全部入队。
#include
using namespace std;
const int MAXN=100005;
typedef long long ll;
bool vis[2005][2005];
char maze[2005][2005];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,1,-1};
struct point{
int x, y;
int X, Y;
point(int a,int b,int c,int d,int e){
x=a;
y=b;
X=c;
Y=d;
t=e;
}
int t;
friend bool operator <(const point &a,const point &b){
return a.t
>b.t;
}
};
int N,M;
void bfs(int x,int y,int X,int Y){
priority_queue que;
que.push(point(x,y,X,Y,0));
vis[x][y]=1;
while(!que.empty()){
point tp=que.top();
que.pop();
for(int i=0;i<4;i++){
int x=tp.x+dx[i];
int y=tp.y+dy[i];
if(maze[x][y]=='.'&&vis[x][y]==0){
if(i==2){
if(tp.Y>0){
que.push(point(x,y,tp.X,tp.Y-1,tp.t+1));
vis[x][y]=1;
}
}
else{
if(i==3){
if(tp.X>0){
que.push(point(x,y,tp.X-1,tp.Y,tp.t+1));
vis[x][y]=1;
}
}
else{
for(int j=x;j<=N;j++){
if(vis[j][y]==0){
if(maze[j][y]=='.'){
que.push(point(j,y,tp.X,tp.Y,tp.t));
vis[j][y]=1;
}
else
break;
}
else
break;
}
for(int j=x;j>=1;j--){
if(vis[j][y]==0){
if(maze[j][y]=='.'){
que.push(point(j,y,tp.X,tp.Y,tp.t));
vis[j][y]=1;
}
else
break;
}
else
break;
}
}
}
}
}
}
}
int main(){
int SX,SY;
int X,Y;
cin>>N>>M>>SX>>SY>>X>>Y;
for(int i=1;i<=N;i++)
scanf("%s",maze[i]+1);
bfs(SX,SY,X,Y);
int num=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
num+=vis[i][j];
cout<