Codeforces Round #516 (Div. 2, by Moscow Team Olympiad)D. Labyrinth·「BFS」
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
Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y
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
題目大意:給你一個圖,問你從起點開始沿著四個方向走能到達多少個點,其中向左走和向右走右次數限制。 大致思路:我們可以用BFS遍歷整個圖,因為BFS的遍歷是無序的,可能有的點走過一次後會重複走,如果不做處理會浪費向左走和向右走的次數。所以我們可以用兩個二維陣列來記錄每到達一個點已經向左走和向右走的步數的最小值。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 2010;
char maze[MAXN][MAXN];
int n,m,MaxLeft,MaxRight,sx,sy;
struct node{
int x,y,cntl,cntr;
node(int x,int y,int cntl,int cntr) : x(x), y(y), cntl(cntl), cntr(cntr) {}
};
bool vis[MAXN][MAXN];
int now1[MAXN][MAXN],now2[MAXN][MAXN],ans = 0;
int dx[4] = {-1,0,1,0},dy[4] = {0,-1,0,1};
bool Outside(int x,int y){
if(x < 1 || x > n || y < 1 || y > m || maze[x][y] == '*') return true;
else return false;
}
void BFS(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) now1[i][j] = now2[i][j] = INF;
}
memset(vis,0,sizeof(vis));
queue<node> qu;
qu.push(node(sx,sy,0,0));
vis[sx][sy] = 1;
ans = 1;
while(!qu.empty()){
node now = qu.front(); qu.pop();
int nx = now.x,ny = now.y,nl = now.cntl,nr = now.cntr;
for(int i = 0; i < 4; i++){
int tx = nx + dx[i];
int ty = ny + dy[i];
if(Outside(tx,ty)) continue;
if(i == 1 && nl == MaxLeft) continue;
if(i == 3 && nr == MaxRight) continue;
if(vis[tx][ty]){
if(i == 1){
if(now1[tx][ty] > nl + 1 || now2[tx][ty] > nr)
now1[tx][ty] = min(now1[tx][ty],nl + 1),now2[tx][ty] = min(now2[tx][ty],nr);
else continue;
}
else if(i == 3){
if(now1[tx][ty] > nl || now2[tx][ty] > nr + 1)
now1[tx][ty] = min(now1[tx][ty],nl),now2[tx][ty] = min(now2[tx][ty],nr + 1);
else continue;
}
else{
if(now1[tx][ty] > nl || now2[tx][ty] > nr)
now1[tx][ty] = min(now1[tx][ty],nl),now2[tx][ty] = min(now2[tx][ty],nr);
else continue;
}
}
else{
if(i == 1) now1[tx][ty] = nl + 1,now2[tx][ty] = nr;
else if(i == 3) now1[tx][ty] = nl,now2[tx][ty] = nl + 1;
else now1[tx][ty] = nl,now2[tx][ty] = nr;
}
// qu.push(node(tx,ty,now1[tx][ty],now2[tx][ty]));
if(i==1) qu.push(node(tx,ty,nl+1,nr));
else if(i==3) qu.push(node(tx,ty,nl,nr+1));
else qu.push(node(tx,ty,nl,nr));
if(!vis[tx][ty])ans++;
vis[tx][ty] = 1;
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
scanf("%d%d",&sx,&sy);
scanf("%d%d",&MaxLeft,&MaxRight);
for(int i = 1; i <= n; i++) scanf("%s",maze[i] + 1);
BFS();
printf("%d\n",ans);
}
return 0;
}