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. Logstash之三:命令行中常用的命令

    -f:通过这个命令可以指定Logstash的配置文件,根据配置文件配置logstash -e:后面跟着字符串,该字符串可以被当做logstash的配置(如果是“” 则默认使用stdin作为输入,std ...

  2. 【H3C交换机】cpu各个进程的详细说明

    display cpu-usage命令用来查看设备CPU占用率的统计信息,以及各个进程的cpu占用率. 各个进程详细说明如下,不同软件版本.盒式和框式的cpu进程略有不同,详细信息可以查看手册中的命令 ...

  3. 基于sklearn的 BaseEstimator开发接口:模型融合Stacking

    转载:https://github.com/LearningFromBest/CMB-credit-card-department-prediction-of-purchasing-behavior- ...

  4. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  5. pycharm格式报错: Remove redundant parentheses

    处理:所在代码行,最外层括号去掉

  6. Redis-基本数据类型与内部存储结构

    1-概览 Redis是典型的Key-Value类型数据库,Key为字符类型,Value的类型常用的为五种类型:String.Hash .List . Set . Ordered Set 2- Redi ...

  7. ubuntu16.04安装python3,numpy,pandas等量化计算库

    ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...

  8. tornado-简单的服务器非阻塞

    1.服务器 非阻塞 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.op ...

  9. 《GPU高性能编程CUDA实战》附录三 关于book.h

    ▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...

  10. 31. Studio获取新的ID值方法

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");var vid1 = fun.utilHelper.getNextID(&quo ...