1. 程式人生 > >Codeforces contest 1064 problem D Labyrinth —— 帶位置資訊的bfs

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