Codeforces Round #516 (Div. 2, by Moscow Team Olympiad)D. Labyrinth·「BFS」

本文介绍了一个迷宫游戏挑战,玩家需要在有限的左右移动次数内,从起点出发探索迷宫中可达的所有格子。通过使用BFS算法并记录每个点的最小左右移动步数,有效地解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意:给你一个图,问你从起点开始沿着四个方向走能到达多少个点,其中向左走和向右走右次数限制。
大致思路:我们可以用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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值