Labyrinth

http://codeforces.com/problemset/problem/1064/D

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard 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 nm (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

The second line contains two integers rc (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 xy (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:

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

加个记忆化,判断L和R剩余多少就行

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#pragma GCC optimize(2)
using namespace std; int n,m;
char map[][];
struct Num{
int L,R;
}book[][];
int r,c,L,R;
struct sair{
int x,y,L,R;
};
int dir[][]={,-,-,,,,,};//R,D,L,U void bfs(){
queue<sair>Q;
sair s,e;
s.x=r,s.y=c,s.L=L,s.R=R;
Q.push(s);
book[s.x][s.y].L=L;
book[s.x][s.y].R=R;
while(!Q.empty()){
s=Q.front();
Q.pop();
for(int i=;i<;i++){
e.x=s.x+dir[i][];
e.y=s.y+dir[i][];
if(e.x>=&&e.x<n&&e.y>=&&e.y<m&&map[e.x][e.y]!='*'){
e.L=s.L;
e.R=s.R;
if(i==){
e.R--;
if(e.R<) continue;
}
else if(i==){
e.L--;
if(e.L<) continue;
}
if(book[e.x][e.y].L<e.L||book[e.x][e.y].R<e.R){
book[e.x][e.y].L=max(book[e.x][e.y].L,e.L);
book[e.x][e.y].R=max(book[e.x][e.y].R,e.R);
Q.push(e);
} }
}
}
} int main(){
cin>>n>>m;
cin>>r>>c;
cin>>L>>R;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
book[i][j].L=book[i][j].R=-;
}
}
for(int i=;i<n;i++){
cin>>map[i];
}
r--,c--;
bfs();
int ans=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(book[i][j].L!=-||book[i][j].R!=-){
ans++;
}
}
}
cout<<ans<<endl;
}

Labyrinth(记忆化BFS)的更多相关文章

  1. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  2. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  3. HDU 2579 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...

  4. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  5. HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)

    题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1.向前走:2.左转90度:3.右转90度.现给定起点和终点,问到达终点最短路的条数. 思路:一般的题目只是求最短路的长度,但本题还 ...

  6. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  7. FZU 2092 bfs+记忆化搜索

    晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...

  8. FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力

    题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...

  9. HDU 1428 漫步校园(记忆化搜索,BFS, DFS)

    漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...

随机推荐

  1. poj 3253 Fence Repair (贪心,优先队列)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. Unreal Engine 4 笔记

    1.UE4的调试输出 //*1 调试输出*// /*case a.快速使用 不设置log类别 默认为LogTemp*/ UE_LOG(LogTemp,Log,TEXT("Your messa ...

  3. more 可翻页查看(一页一页翻动)

    命令解释 我们查看数据的时候,使用前面提到的nl与cat.tac等等,都是将文件内容一次性输出到屏幕上,看起来不是很方便,那我们就可以使用这个命令,一页一页查看,前面的数据不至于看不到. 命令说明 [ ...

  4. 3D Render

    记录最近遇到的问题: 1:崩溃问题 由于高频率获取DC异常导致. void D3D11Texture2D::Copy2Window(void* srcdc, uint32_t left, uint32 ...

  5. 查看linux服务器CPU数量

    首先,要区分两个概念:物理CPU和逻辑CPU. 物理CPU就是服务器上实际安装的CPU.但是一个物理CPU可以有多个核.例如,一个 i5 760 是双核,而一个 i5 2250 是四核.如果开启了In ...

  6. Redis如何存储对象与集合示例详解

      前言 大家都知道在项目中,缓存以及mq消息队列可以说是不可或缺的2个重要技术.前者主要是为了减轻数据库压力,大幅度提升性能.后者主要是为了提高用户的体验度,我理解的是再后端做的一个ajax请求(异 ...

  7. 经典算法 BFPRT算法详解

    内容: 1.原始问题     =>  O(N*logN) 2.BFPRT算法    => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...

  8. IPv4报文分片

    1:为什么需要分片 每个数据链路层协议都有自己的帧格式,在这个格式中有一个字段是"数据字段最大长度"(MTU,最大传输单元),当数据报被封装成帧时,数据报的总长度必须小于这个最大长 ...

  9. jquery函数写法

    普通jquery函数写法 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script&g ...

  10. 2.纯 CSS 创作一个矩形旋转 loader 特效

    原文地址:2.纯 CSS 创作一个矩形旋转 loader 特效 扩展后地址:https://scrimba.com/c/cNJVWUR  扩展地址:https://codepen.io/pen/ HT ...