欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforces 1064D. Labyrinth (有限制的BFS)

程序员文章站 2022-03-02 23:26:14
...

链接

D. Labyrinth
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
inputCopy
4 5
3 2
1 2

.*.


outputCopy
10
inputCopy
4 4
2 2
0 1

.


outputCopy
7
Note
Cells, reachable in the corresponding example, are marked with ‘+’.

First example:

+++…
+*.
+++

*+++.
Second example:

.++.
.+*.
.++.
.++.

题意:给你一个N*M的地图,‘*'表示不能走的地区,起点为r,c,向左最多移动x次,向右最多移动y次,问你能到达的地点有多少。

思路:很明显采用bfs的做法,最开始我就是直接bfs一遍。但这样很显然有问题,因为左右移动次数有限制,所以不一定先到达某个点就是最佳的,所以我们标记到某个点时向左向右的移动次数,如果有更佳的选择就更换。

代码如下:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 2020;
char str[MAX][MAX];
const int xx[4] = {0,1,0,-1};
const int yy[4] = {1,0,-1,0};
class Node{
public:
    int l,r;
    int x,y;
};
class Tag{
public:
    int left,right;
    Tag(){
        left = right = -1;
    }
};
Tag book[MAX][MAX];
int N,M;
int r,c;
int px,py;
void bfs(){
    queue<Node> que;
    Node e;
    e.x = r;e.y = c;
    e.l = 0;e.r = 0;
    que.push(e);
    book[r][c].left = book[r][c].right = 0;
    while(!que.empty()){
        Node v = que.front();que.pop();
        for(int i=0;i<4;++i){
            int nx = v.x + xx[i];
            int ny = v.y + yy[i];
            if(nx < 1 || ny < 1 || nx > N || ny > M)
                continue;
            if(str[nx][ny] == '*')  continue;
            Node es;
            es.x = nx;es.y = ny;
            es.l = v.l;es.r = v.r;
            if(i == 2)  es.l++;
            if(i == 0)  es.r++;
            if(es.l <= px && es.r <= py){
                if(es.l == book[nx][ny].left && es.r == book[nx][ny].right) continue;
                //与该点左右移动次数完全相同的要被剔除,否则会造成不断的向上向下移动。
                if(book[nx][ny].left == -1 || (es.l <= book[nx][ny].left && es.r <= book[nx][ny].right)){
                    que.push(es);
                    book[nx][ny].left = es.l;
                    book[nx][ny].right = es.r;
                }
            }
        }
    }
}
int main(void){
    scanf("%d%d",&N,&M);
    scanf("%d%d",&r,&c);
    scanf("%d%d",&px,&py);
    for(int i=1;i<=N;++i)
        scanf("%s",str[i]+1);
    bfs();
    int res = 0;
    for(int i=1;i<=N;++i){
        for(int j=1;j<=M;++j){
            if(book[i][j].left != -1)
                res++;
        }
    }
    printf("%d\n",res);

    return 0;
}
/*
Special data
10 10
10 4
10 9
...*******
.*.*******
.*.*******
.*.*******
.*.*******
.*.*......
.*.*.*****
.*........
.********.
..........


res = 43;

*/

相关标签: codeforces