Labyrinth(记忆化BFS)
Labyrinth
http://codeforces.com/problemset/problem/1064/D
2 seconds
512 megabytes
standard input
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?
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.
Print exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.
4 5
3 2
1 2
.....
.***.
...**
*....
10
4 4
2 2
0 1
....
..*.
....
....
7
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)的更多相关文章
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)
题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1.向前走:2.左转90度:3.右转90度.现给定起点和终点,问到达终点最短路的条数. 思路:一般的题目只是求最短路的长度,但本题还 ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- FZU 2092 bfs+记忆化搜索
晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...
- FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力
题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...
- HDU 1428 漫步校园(记忆化搜索,BFS, DFS)
漫步校园 http://acm.hdu.edu.cn/showproblem.php?pid=1428 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于 ...
随机推荐
- while_else
使用while循环输出100-50,从大到小,到50的时候,再从0到50输出,然后结束count =
- 复现ICCV 2017经典论文—PyraNet
. 过去几年发表于各大 AI 顶会论文提出的 400 多种算法中,公开算法代码的仅占 6%,其中三分之一的论文作者分享了测试数据,约 54% 的分享包含“伪代码”.这是今年 AAAI 会议上一个严峻的 ...
- JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...
- HTML5 监听移动端浏览器返回键兼容版本
// 往windosw对象中的历史记录注入URL的方法 function addUrl() { var state = { title: "title", url: "# ...
- 视频地址blog加密
/* JS部分 没处理兼容什么的 */ var id='<?php echo $_GET['id'];?>'; var video = document.getElementById(&q ...
- 关于Nginx的负载均衡
一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独一个服务器压力过大,将来自用户的请求转发给不同的服务器.详情请查看我的另一篇博客. 二.Ng ...
- 亿图eddx与visio转换
Visio支持的格式 AutoCAD 绘图文件格式 (.dwg..dxf) 压缩增强型图元文件 (.emz) 增强型图元文件 (.emf) 可交换图像文件格式 (GIF) JPEG 文件交换格式 (. ...
- web 项目手机页面不允许缩放
https://blog.csdn.net/ljw_jiawei/article/details/80421240
- vue-cli 上手
1.cnpm install --global vue-cli 安装脚手架 2.vue init webpack baoge 创建 3.选择配置项 Project name (baoge): ---- ...
- (Python)numpy的argmax用法
解释 还是从一维数组出发.看下面的例子. import numpy as np a = np.array([3, 1, 2, 4, 6, 1]) print(np.argmax(a))4 argm ...