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不能自拔,每天寝室.机房两点一线.由于 ...
随机推荐
- OS信号实现Java异步通知
OS信号实现Java异步通知本文将结合操作系统的信号机制,来尝试实现一个简单的,不依赖功能环境的Java异步通知功能.没有特殊说明,本文所有的示例,都是基于Linux.信号简介信号是在软件层次上对中断 ...
- python-appium520-2初步使用
1.录制自动化脚本 场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号 2.Appium客户端 客户端介绍:https://github.com/appium/appium/blob/ ...
- 关于Linux服务器磁盘空间占满问题的解决方法
下面给大家分享一篇关于Linux服务器磁盘占满问题解决方法(/dev/sda3 满了),需要的的朋友参考下吧 下面我们一起来看一篇关于Linux服务器磁盘占满问题解决(/dev/sda3 满了), ...
- 详解UE4静态库与动态库的导入与使用
转自:http://blog.csdn.net/u012999985/article/details/71554628 一.基本内容概述 最近做项目时经常看到build.cs文件,就想研究一下UE ...
- javascript 中的函数声明和函数表达式区别
函数声明格式: function add(a, b) { alert(a+b); } 函数表达式格式: var add = function (a, b) { alert(a+b); } 解析器在向环 ...
- 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v4.0.50727/Temporary ASP.NET Files/root .... 拒绝访问。
此问题困扰良久,得终极解决方案 环境:windows 2008 server r2 + iis7 + .net framework4.5 解决:1. 错误信息中包含的目录“c:/Windows/Mic ...
- XPath 常用语法札记
* 不包含属性的元素 例如不包含属性的span: span[not(@*)] * 文本包含某部分的元素 例如文本包含Rank的元素: *[contains(text(),'Rank')] * 选择匹配 ...
- angularJs按需加载代码(未验证)
一网友写的AngularJs按需加载代码,但未验证,放着备用. application.config( function($routeProvider) { ...
- 程序员必看:给你一份详细的Spring Boot知识清单
在过去两三年的Spring生态圈,最让人兴奋的莫过于Spring Boot框架.或许从命名上就能看出这个框架的设计初衷:快速的启动Spring应用.因而Spring Boot应用本质上就是一个基于Sp ...
- Spring boot Tomcat配置
来自: https://www.cnblogs.com/a8457013/p/7687764.html