[BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B
2 seconds
256 megabytes
standard input
standard output
Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.
The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.
Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:
- climb one area up;
- climb one area down;
- jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall.
If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon.
The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.
The level is considered completed if the ninja manages to get out of the canyon.
After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.
The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly.
The second line contains the description of the left wall — a string with the length of ncharacters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area.
The third line describes the right wall in the same format.
It is guaranteed that the first area of the left wall is not dangerous.
Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).
7 3
---X--X
-X--XX-
YES
6 2
--X-X-
X--XX-
NO
In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon.
In the second sample there's no way the ninja can get out of the canyon.
题意:
有一个人在两堵平行的墙之间,每面墙墙被分为n个单位,墙上有一些位置人不能到达,人可以移动,但每次移动水位会上升,人不能碰到水,现在初始位置在左边最下方,就是样例中的左上角,若人当前高度为x,则人每次可以进行三种操作,
第一种是在当前墙上升一个单位即x+1,第二种是在当前墙下降一个单位及x-1,第三种是跳的另一个墙上且高度变为x+k,问人能否跳出墙(人的高度严格大于n!!!注意是严格大于,也就是不能等于)且不被水淹没
思路:
一道非常水的搜索大水题,真想吐槽一下赛时没把问题想明白,在比赛时写成了判断条件写出了y+k>=n,但有可能k==1导致跳了一格后水正好上升一格,就凉凉,赛后改成y+k>n就AC了,真想给当时的自己一巴掌
by the way,注意人物先移动了水才会上升,所以先判断能不能跳出去再判断水有没有上升到人所在的层
注意:
问人能否跳出墙(人的高度严格大于n!!!注意是严格大于,也就是不能等于)且不被水淹没
人物先移动了水才会上升,所以先判断能不能跳出去再判断水有没有上升到人所在的层
#include<bits/stdc++.h>
using namespace std;
const int amn=1e5+;
#define fi first
#define se second
int n,k,mp[][amn],hm[][amn];bool valid,idx[][amn];
struct pii{
int first,second,h;
pii(int f,int s,int hh){first=f,second=s,h=hh;}
};
void bfs(){
memset(idx,,sizeof idx);
queue<pii> q;
idx[][]=;
q.push(pii(,,));
while(q.size()){
int x=q.front().first,y=q.front().second,h=q.front().h;q.pop();
if(y+k>n){valid=;return;} ///赛时没把问题想明白,在比赛时写成了判断条件写出了y+k>=n,但有可能k==1导致跳了一格后水正好上升一格,就凉凉,赛后改成y+k>n就AC了,真想给当时的自己一巴掌
if(y<=h){continue;} ///这里是先跳了水才会上升,所以先判断能不能跳出去再判断水有没有上升到人所在的层
if(!idx[x][y+]&&mp[x][y+]){idx[x][y+]=;q.push(pii(x,y+,h+));}
if(!idx[x][y-]&&mp[x][y-]){idx[x][y-]=;q.push(pii(x,y-,h+));}
if(!idx[x^][y+k]&&mp[x^][y+k]){idx[x^][y+k]=;q.push(pii(x^,y+k,h+));}
}
}
int main(){
ios::sync_with_stdio();
cin>>n>>k;
char in;
for(int i=;i<=n;i++){
cin>>in;
if(in=='-')mp[][i]=;
else mp[][i]=;
}
for(int i=;i<=n;i++){
cin>>in;
if(in=='-')mp[][i]=;
else mp[][i]=;
}
valid=;
bfs();
if(valid!=)printf("YES\n");
else printf("NO\n");
}
/**
有一个人在两堵平行的墙之间,每面墙墙被分为n个单位,墙上有一些位置人不能到达,人可以移动,但每次移动水位会上升,人不能碰到水,现在初始位置在左边最下方,就是样例中的左上角,若人当前高度为x,则人每次可以进行三种操作,
第一种是在当前墙上升一个单位即x+1,第二种是在当前墙下降一个单位及x-1,第三种是跳的另一个墙上且高度变为x+k,问人能否跳出墙(人的高度严格大于n!!!注意是严格大于,也就是不能等于)且不被水淹没
一道非常水的搜索大水题,真想吐槽一下赛时没把问题想明白,在比赛时写成了判断条件写出了y+k>=n,但有可能k==1导致跳了一格后水正好上升一格,就凉凉,赛后改成y+k>n就AC了,真想给当时的自己一巴掌
by the way,注意人物先移动了水才会上升,所以先判断能不能跳出去再判断水有没有上升到人所在的层
**/
[BFS,大水题] Codeforces 198B Jumping on Walls的更多相关文章
- Jumping on Walls CodeForces - 198B
Jumping on Walls CodeForces - 198B 应该是一个隐式图的bfs,或者叫dp. 先是一个TLE的O(nklogn) #include<cstdio> #inc ...
- BZOJ_1621_[Usaco2008_Open]_Roads_Around_The_Farm_分岔路口(模拟+大水题)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1621\(n\)头奶牛,刚开始在一起,每次分成\(x\)和\(x+m\)两部分,直到不能再分,问 ...
- 第三届山西省赛1004 一道大水题(scanf)
一道大水题 时间限制: C/C++ 2000ms; Java 4000ms 内存限制: 65535KB 通过次数: 44 总提交次数: 1020 问题描述 Dr. Pan作为上兰帝国ACM的总负责人, ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
- PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)
如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...
- BFS简单题套路_Codevs 1215 迷宫
BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- 【数据结构】 最小生成树(四)——利用kruskal算法搞定例题×3+变形+一道大水题
在这一专辑(最小生成树)中的上一期讲到了prim算法,但是prim算法比较难懂,为了避免看不懂,就先用kruskal算法写题吧,下面将会将三道例题,加一道变形,以及一道大水题,水到不用高级数据结构,建 ...
随机推荐
- Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习
16.7挑战练习:优化照片显示 新建dialog_photo.xml 1234567891011121314 <?xml version="1.0" encoding=&qu ...
- UIView绘制原理,异步绘制
绘制原理 首先看一幅流程图 UIView调用setNeedsDisplay方法后,实际上并没有发生当前视图的绘制工作,而是在之后的某一时机进行绘制工作,为什么会在之后的某一时机进行绘制工作呢? 当UI ...
- 搭建websocket消息推送服务,必须要考虑的几个问题
近年,不论是正在快速增长的直播,远程教育以及IM聊天场景,还是在常规企业级系统中用到的系统提醒,对websocket的需求越来越大,对websocket的要求也越来越高.从早期对websocket的应 ...
- Java基础IO流 ,文件读取,由易至难
最基础的读取文件 import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;imp ...
- mysql从5.5升级到5.7遇到的坑
在安装mysql5.7时很顺利安装完成,但在启动项目时报错: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clau ...
- Pycharm IDE安装及注册激活笔记(1)
一.Windows 下的安装及激活. 1.首先去Pycharm官网,或者直接输入网址:http://www.jetbrains.com/pycharm/download/#section=window ...
- python 深浅拷贝 元组 字典 集合操作
深浅拷贝 :值拷贝 :ls = [,,] res = ls 则print(res)就是[,,] 浅拷贝 :ls.copy() 深拷贝:ls3 = deepcopy(ls) # 新开辟列表空间,ls列表 ...
- VUE三 vue-router(路由)详解
前端路由 根据不同的 url 地址展示不同的内容或页面,无需依赖服务器根据不同URL进行页面展示操作 优点 用户体验好,不需要每次都从服务器全部获取,快速展现给用户 缺点 使用浏览器的前进,后退键的时 ...
- Markdown For EditPlus插件发布(基于EditPlus快速编辑Markdonw文件,写作爱好的福音来啦)
详细介绍: Markdown For EditPlus插件使用说明 开发缘由 特点好处: 中文版使用说明 相关命令(输入字符敲空格自动输出): EditPlus常用快捷键: 相关教程: English ...
- java面试汇总一
第一部分 Java SE基础(1) 1.1 java的8种基本数据类型 装箱 拆箱 1.1.1 8种基本的数据类型 1.1.2装箱 拆箱 自动装箱是 Java 编译器在基本数据类型和对应的对象包 ...