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的访问数。
AC代码:
/*
* @Author: Achan
* @Date: 2018-10-21 21:14:12
* @Last Modified by: Achan
* @Last Modified time: 2018-10-21 21:25:04
*/
#include
using namespace std;
int r,c,n,m,px,py;
int ans;
bool vis[2003][2003];
int mov[4][2] = {{0,-1},{0,1},{1,0},{-1,0}} ;
typedef struct Node
{
int x; //保存 行号
int y; //保存列号
int ox; //累计向x移动了多少
int oy; //累计向y移动了多少
} node;
inline bool inside(int x, int y) { return x && y && x<=n && y<=m ;}
void BFS()
{
ans = 0;
deque<node> q;
node s;
s.x = r;
s.y = c;
s.ox = 0;
s.oy = 0;
q.push_front(s);
while(!q.empty())
{
int xx = q.front().x;
int yy = q.front().y;
int nx = q.front().ox;
int ny = q.front().oy;
q.pop_front();
if(vis[xx][yy]) continue;
vis[xx][yy] = 1;
ans++;
node t;
for(int i=0;i<4;i++)
{
int xxx = xx + mov[i][0];
int yyy = yy + mov[i][1];
if(inside(xxx,yyy) && (!vis[xxx][yyy]) )
{
if(i==0 && nx+1<=px)
{
t.x = xxx;
t.y = yyy;
t.ox= nx+1; //向左边移动的距离,所以是正数 极限px
t.oy = ny;
q.push_back(t); //放队列后边,降低扩展优先级
continue;
}
if(i==1 && ny + 1 <= py)
{
t.x = xxx;
t.y = yyy;
t.ox = nx;
t.oy = ny+1; //向右边移动 极限py
q.push_back(t);
continue;
}
else if(i>1)
{
t.x = xxx;
t.y = yyy;
t.ox = nx;
t.oy = ny;
q.push_front(t);
}
}
}
}
cout<<ans<<endl;
}
int main(void)
{
std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
cin>>n>>m>>r>>c>>px>>py;
char ch;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>ch , vis[i][j] = (ch=='*'?1:0);
BFS();
}