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:
.++.
.+*.
.++.
.++.
解题思路
这道题需要的答案是能访问的最大数量。所以图可以简单建为布尔类型的vis[][]  ,搜索访问可以套用经典BFS模板,但这道题对左右的移动总次数有限制,所以我们在使用BFS的时候,不能再用BFS  “总步数少的优先增广 ”  的搜索规则。因为上下的移动无限制,优先扩展上下必能得到最多的访问数! 因为当左右移动的次数使用殆尽,结果是无法再继续扩展左右,如果该节点扩展得比较早,vis被其访问,之后的更加优秀的(左右扩展较少)增广路通过某途径到达同样节点,但却因vis被访问而放弃扩展,失去更进一步扩展的机会。
以下给出的是CF中test  40数据:
10 10
10 4
10 9
...*******
.*.*******
.*.*******
.*.*******
.*.*******
.*.*......
.*.*.*****
.*........
.********.
..........

当用一般的BFS得到的一定是41
而用优先队列调整访问优先级可以得到43的访问数。

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x,y,l,r;
    node(int xx,int yy,int ll,int rr)
    {
        x=xx;
        y=yy;
        l=ll;
        r=rr;

    }
    node(){}
    bool operator <(node a)const
	{

		return  l + r > a.l + a.r;

	}
};
int hang,lie;
int ha,li,lft,rit;
char mp[2020][2020];
bool flag[2020][2020];

priority_queue<node>q;
int sum=0;
node now;
void bfs()
{
  int xx,yy,ll,rr;
  while(!q.empty())
  {
      now=q.top();
      if(flag[now.x+1][now.y]==false&&mp[now.x+1][now.y]=='.')
      {
         node net(now.x+1,now.y,now.l,now.r);
        flag[now.x+1][now.y]=true;
        q.push(net);
      }
       if(flag[now.x-1][now.y]==false&&mp[now.x-1][now.y]=='.')
      {
          node net(now.x-1,now.y,now.l,now.r);
         flag[now.x-1][now.y]=true;
         q.push(net);
      }
       if(flag[now.x][now.y+1]==false&&mp[now.x][now.y+1]=='.'&&now.r<rit)
      {
        node net(now.x,now.y+1,now.l,now.r+1);
       flag[now.x][now.y+1]=true;
       q.push(net);
      }
      if(flag[now.x][now.y-1]==false&&mp[now.x][now.y-1]=='.'&&now.l<lft)
      {
        node net(now.x,now.y-1,now.l+1,now.r);
       flag[now.x][now.y-1]=true;
       q.push(net);
      }
      sum++;
      q.pop();
  }
}
int main()
{
  for(int i=0;i<2005;i++)
  {
      for(int j=0;j<2005;j++)
        flag[i][j]=false;
  }
    cin>>hang>>lie>>ha>>li>>lft>>rit;
    getchar();
    for(int i=1; i<=hang; i++)
    {
        for(int j=1;j<=lie;j++)
      mp[i][j]=getchar();
      getchar();
    }
    now.x=ha;
    now.y=li;
    now.l=0;
    now.r=0;
    flag[ha][li]=true;
    q.push(now);
    bfs();
    printf("%d",sum);
    return 0;
}