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不能自拔,每天寝室.机房两点一线.由于 ...
随机推荐
- centos7开机界面出现多个选项
第一个选项正常启动,第二个选项急救模式启动(系统出项问题不能正常启动时使用并修复系统) 在CentOS更新后,并不会自动删除旧内核.所以在启动选项中会有多个内核选项,可以手动使用以下命令删除多余的内核 ...
- centos7 设置系统时间与网络同步
1.安装ntpdate工具 yum -y install ntp ntpdate 2.设置系统时间与网络时间同步 ntpdate cn.pool.ntp.org 3.将系统时间写入硬件时间 hwclo ...
- PHP 弹出文件下载 原理 代码
/** * @author default7<default7@zbphp.com> * @description 演示PHP弹出下载的原理 * * @param $file_n ...
- numpy的flat、flatten、ravel
import numpy as np dt = np.arange(10).reshape(5,2) # =============================================== ...
- 关闭浏览器时的友情提醒jQuery写法
$(window).bind('beforeunload', function () { return '您确定退出该页面吗?'; }); 支持以下浏览器(对号表示支持,叉号表示不支持.):
- 笔记本电脑安装centos 7(转)
1. 下载 CentOS 镜像 下载地址 : https://wiki.centos.org/Download, 我下载的是1406 2. 使用 UltraISo 将镜像刻录到U盘 UltraISo ...
- 文件系统性能测试--iozone
iozone 一个文件系统性能评测工具,可以测试Read, write, re-read,re-write, read backwards, read strided, fread, fwrite, ...
- nginx配置详解(转)
Nginx 配置文件详解 user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error. ...
- 经典算法 BFPRT算法详解
内容: 1.原始问题 => O(N*logN) 2.BFPRT算法 => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...
- idea-activate code
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...