1. 程式人生 > >CodeForce 1064-D. Labyrinth 優先佇列

CodeForce 1064-D. Labyrinth 優先佇列

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