Codeforces contest 1064 problem D Labyrinth —— 带位置信息的bfs

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:

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

题意:

给你一张图,*是不能走的地方,.是可以走的地方,给你起始位置坐标和最多向左走的步数和向右走的步数,问你最多能走多少点。

题解:

普通的bfs,再加上走到每个位置记录一下这个位置的左步数和右步数,如果再次走到这个点,只要有一方的步数比当前这个点的大,那么就可以继续走。

#include<bits/stdc++.h>
using namespace std;
char Map[2005][2005];
int n,m,sx,sy,le,rig;
int xmov[]={1,0,-1,0};
int ymov[]={0,1,0,-1};
int signl[2005][2005],signr[2005][2005];
//int step[2005][2005][2];
struct node
{
    int x,y,l,r;
};
int bfs()
{
    queue<node>Q;
    Q.push({sx,sy,le,rig});
    int ans=1;
    memset(signl,-1,sizeof(signl));
    memset(signr,-1,sizeof(signr));
    signl[sx][sy]=le;
    signr[sx][sy]=rig;
    while(!Q.empty())
    {
        node v,ne;
        v=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            if(i==1&&v.r==0)
                continue;
            if(i==3&&v.l==0)
                continue;
            ne.x=v.x+xmov[i];
            ne.y=v.y+ymov[i];
            if(ne.x>n||ne.x<1||ne.y>m||ne.y<1||Map[ne.x][ne.y]=='*')
                continue;
            ne.r=v.r,ne.l=v.l;
            if(i==1)
                ne.r-=1;
            if(i==3)
                ne.l-=1;
            if(signl[ne.x][ne.y]>=ne.l&&signr[ne.x][ne.y]>=ne.r)
                continue;
            
            if(signl[ne.x][ne.y]==-1)
                ans++;
            signl[ne.x][ne.y]=ne.l,signr[ne.x][ne.y]=ne.r;

            Q.push(ne);

        }
    }
/*for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            printf("%d%c",signr[i][j],j==m?'\n':' ');
            printf("+++++++++++++++\n");*/
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
    scanf("%d%d",&sx,&sy);
    scanf("%d%d",&le,&rig);
    for(int i=1;i<=n;i++)
        scanf("%s",Map[i]+1);
    printf("%d\n",bfs());
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值