ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【题解】CF1063B Labyrinth(BFS)

2022-01-18 20:02:10  阅读:165  来源: 互联网

标签:d% Labyrinth 题解 move number cell CF1063B contains starting


原题戳这里---> https://www.luogu.com.cn/problem/CF1063B

题目描述

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.

Sample 1

Input

4 5
3 2
1 2
.....
.∗∗∗.
...∗∗
∗....

Output

10

Sample 2

Input

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

Output

7

分析及代码实现

  • 这道题核心就是BFS,但是和普通的宽搜不一样,本题有向左向右的限制。

  • 如果按照一般的思路,可能会先做花一些代价的方案到某一个点而把不用花代价的方案给排掉了

  • 所以我们可以借助双端队列(deque)

  • 在队列中记录 坐标 以及 当前还剩下的左右移动的次数

  • 每次把不用花代价的方案放到队首,把花代价的放到队尾,每次从队首取点即可

C++代码实现

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <deque>
using namespace std;

struct node{
    int a, b, lc, rc;
};

const int N = 2022;
int n, m, r, c, x, y, cnt;
char g[N][N];
deque<node> q;
bool v[N][N];

int main(){
    scanf("%d%d%d%d%d%d", &n, &m, &r, &c, &x, &y);
    for (int i = 1; i <= n; i++)    scanf("%s", g[i] + 1);

    q.push_back({r, c, x, y});

    while(q.size()){
        auto t = q.front();
        q.pop_front();
        int a = t.a, b = t.b;
        if(v[a][b])    continue;

        v[a][b] = true, cnt++;

        if (a - 1 >= 1 && g[a - 1][b] == '.')
            q.push_front({a - 1, b, t.lc, t.rc});
        if (a + 1 <= n && g[a + 1][b] == '.')
            q.push_front({a + 1, b, t.lc, t.rc});
        if (b - 1 >= 1 && g[a][b - 1] == '.' && t.lc)
            q.push_back({a, b - 1, t.lc - 1, t.rc});
        if (b + 1 <= m && g[a][b + 1] == '.' && t.rc)
            q.push_back({a, b + 1, t.lc, t.rc - 1});
    }
    printf("%d\n", cnt);
    return 0;
}

标签:d%,Labyrinth,题解,move,number,cell,CF1063B,contains,starting
来源: https://www.cnblogs.com/Yi-Shan/p/15819545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有