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
Input

Copy
4 5
3 2
1 2
.....
.***.
...**
*....
Output

Copy
10
Input

Copy
4 4
2 2
0 1
....
..*.
....
....
Output

Copy
7
Note

Cells, reachable in the corresponding example, are marked with '+'.

First example:

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

Second example:

.++.
.+*.
.++.
.++. 题意 : 给你一个n*m 的矩阵,再给你一个起点,你可以往上下左右四个方向走,但同时又有一个往左右的步数限制,询问所有可能到达的点数有多少个?
思路分析 :
  比赛场上直接写了一个 bfs ,pp 了,自信以为A了,今晚又可以涨分,结果....
  其实有一点贪心的想法,就是可以上下走的时候一定要先上下走,最后再左右走,用一个双端队列即可
代码示例:
int n, m;
int sx, sy;
int sl, sr; char mp[2005][2005];
struct node
{
int x, y;
int l, r;
};
deque<node>que;
bool vis[2005][2005]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; void bfs(){
que.push_front({sx, sy, sl, sr});
int ans = 0;
//vis[sx][sy] = 1; while(!que.empty()){
node v = que.front(); que.pop_front();
if (vis[v.x][v.y]) continue;
vis[v.x][v.y] = true;
ans++;
for(int i = 0; i < 4; i++){
int fx = v.x+dir[i][0];
int fy = v.y+dir[i][1];
if (fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == '*'||vis[fx][fy]) continue; if (i == 2) {
if (v.r > 0) que.push_back({fx, fy, v.l, v.r-1});
}
else if (i == 3) {
if (v.l > 0) que.push_back({fx, fy, v.l-1, v.r});
}
else que.push_front({fx, fy, v.l, v.r});
}
}
printf("%d\n", ans);
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m;
cin >> sx >> sy;
cin >> sl >> sr;
for(int i = 1; i <= n; i++){
scanf("%s", mp[i]+1);
}
bfs();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(mp[i][j] == '*') printf("*");
else if (vis[i][j]) printf("+");
else printf(".");
}
printf("\n");
}
return 0;
}

2 . 用类似最短路的写法,每次左右走的时候步数+1,上下走的时候步数不变

代码示例:

const int inf = 0x3f3f3f3f;

int n, m;
int sx, sy, sl, sr;
char mp[2005][2005];
struct node
{
int x, y;
int l, r;
int sum;
bool operator< (const node &v)const{
return sum < v.sum;
}
};
priority_queue<node>que;
bool vis[2005][2005];
int bu[2005][2005];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; void bfs() {
que.push({sx, sy, sl, sr, sl+sr});
memset(vis, false, sizeof(vis));
memset(bu, inf, sizeof(bu));
int ans = 0;
bu[sx][sy] = 0; while(!que.empty()){
node v = que.top(); que.pop(); if (!vis[v.x][v.y]) ans++;
vis[v.x][v.y] = 1;
for(int i = 0; i < 4; i++){
int fx = dir[i][0]+v.x;
int fy = dir[i][1]+v.y; if (fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == '*') continue;
if (bu[v.x][v.y] >= bu[fx][fy]) continue;
bu[fx][fy] = bu[v.x][v.y]; if ((i == 2) || (i == 3)) bu[fx][fy]++;
if (i == 2) {
if (v.r) que.push({fx, fy, v.l, v.r-1, v.l+v.r-1});
}
else if (i == 3) {
if (v.l) que.push({fx, fy, v.l-1, v.r, v.l+v.r-1});
}
else que.push({fx, fy, v.l, v.r, v.l+v.r});
}
}
printf("%d\n", ans);
} int main() {
cin >> n >> m;
cin >> sx >> sy;
cin >> sl >> sr;
for(int i = 1; i <= n; i++) scanf("%s", mp[i]+1);
bfs();
return 0;
}
												

带限制的广搜 codeforces的更多相关文章

  1. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

  2. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

  3. Codeforces 1105D(双层广搜)

    要点 题意:可以拐弯,即哈密顿距离 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索 因此采用双层广搜,把同一轮先都出队列再的一起搜 #include & ...

  4. 『ice 离散化广搜』

    ice(USACO) Description Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000. 湖面上的N(1 & ...

  5. 队列&广搜

    搜索里有深搜,又有广搜,而广搜的基础就是队列. 队列是一种特殊的线性表,只能在一段插入,另一端输出.输出的那一端叫做队头,输入的那一端叫队尾.是一种先进先出(FIFO)的数据结构. 正经的队列: 头文 ...

  6. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  7. 用广搜实现的spfa

    用广搜实现的spfa,如果是用一般的最短路,会发现构图很麻烦,因为它不是路径带权值,而是自身带权值.写起来只要注意,在点出队列的生活将其标记为0,在要压入队列的时候,判断其标记是否为0,为0表示队列中 ...

  8. CF520B——Two Buttons——————【广搜或找规律】

    J - Two Buttons Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  9. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

随机推荐

  1. P1106 细胞分裂

    题目描述 Hanks博士是BT(Bio-Tech,生物技术)领域的知名专家.现在,他正在为一个细胞实验做准备工作:培养细胞样本. Hanks博士手里现在有 \(N\) 种细胞,编号从 \(1\) 到 ...

  2. H3C 以跳数评估的路由并非最优路径

  3. 【58.33%】【codeforces 747B】Mammoth's Genome Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. dijkstra堆优化(multiset实现->大大减小代码量)

    例题: Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣 ...

  5. iview table中的render函数使用

    1.表格列数据内容过多可以用以下两个属性解决: ellipsis:"true', tooltip:true 使每个列的内容如果过多的话变为省略号 2.table中的render函数(实现根据 ...

  6. Visual Studio Team Services使用教程【6】:Readers tfs组checkin权限修改

    你也可以只开启部分代码的权限 把上面开启的整个应用的权限先去掉 只开启一个文件的权限 2017.4.23之后建议朋友看下面的帖子 TFS2017 & VSTS 实战(繁体中文视频) Visua ...

  7. 基于python的二分搜索和例题

    二分搜索 二分概念 二分搜索是一种在有序数组中查找某一特定元素的搜索算法. 搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束: 如果某一特定元素大于或者小于中间元素,则在数 ...

  8. 使用element的upload组件实现一个完整的文件上传功能(上)

    说到标题就有点心塞了,前段时间项目上需要实现一个文件上传的功能,然后就咔咔的去用了element的upload组件,不用不知道一用吓一跳哇. 在使用的过程中遇到了很多让意想不到的问题,后来也因为时间问 ...

  9. Centos7源码编译安装mysql8

    前面介绍了很多关于mysql的文章,下面主要介绍一下mysql8的源码编译安装 一 基本环境 [root@CentOS-7-x86-64-Minimal-1810 ~]# cd /usr/local/ ...

  10. 【证明与推广与背诵】Matrix Tree定理和一些推广

    [背诵手记]Matrix Tree定理和一些推广 结论 对于一个无向图\(G=(V,E)\),暂时钦定他是简单图,定义以下矩阵: (入)度数矩阵\(D\),其中\(D_{ii}=deg_i\).其他= ...