【64.22%】【codefoces round 382A】Ostap and Grasshopper
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The grasshopper wants to eat the insect.
Ostap knows that grasshopper is able to jump to any empty cell that is exactly k cells away from the current (to the left or to the right). Note that it doesn’t matter whether intermediate cells are empty or not as the grasshopper makes a jump over them. For example, if k = 1 the grasshopper can jump to a neighboring cell only, and if k = 2 the grasshopper can jump over a single cell.
Your goal is to determine whether there is a sequence of jumps such that grasshopper will get from his initial position to the cell with an insect.
Input
The first line of the input contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1) — the number of cells in the line and the length of one grasshopper’s jump.
The second line contains a string of length n consisting of characters ‘.’, ‘#’, ‘G’ and ‘T’. Character ‘.’ means that the corresponding cell is empty, character ‘#’ means that the corresponding cell contains an obstacle and grasshopper can’t jump there. Character ‘G’ means that the grasshopper starts at this position and, finally, ‘T’ means that the target insect is located at this cell. It’s guaranteed that characters ‘G’ and ‘T’ appear in this line exactly once.
Output
If there exists a sequence of jumps (each jump of length k), such that the grasshopper can get from his initial position to the cell with the insect, print “YES” (without quotes) in the only line of the input. Otherwise, print “NO” (without quotes).
Examples
input
5 2
G#T
output
YES
input
6 1
T….G
output
YES
input
7 3
T..#..G
output
NO
input
6 2
..GT..
output
NO
Note
In the first sample, the grasshopper can make one jump to the right in order to get from cell 2 to cell 4.
In the second sample, the grasshopper is only able to jump to neighboring cells but the way to the insect is free — he can get there by jumping left 5 times.
In the third sample, the grasshopper can’t make a single jump.
In the fourth sample, the grasshopper can only jump to the cells with odd indices, thus he won’t be able to reach the insect.
【题目链接】:http://codeforces.com/contest/735/problem/A
【题解】
写个搜索就好.
每次往左或者往右搜.
然后用个bool型记录某个位置之前有没有搜过;
遇到障碍物就停止,遇到昆虫就直接结束整个程序;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 200;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,k;
char s[MAXN];
bool before[MAXN];
void dfs(int x)
{
before[x] = true;
if (s[x]=='#') return;
if (s[x]=='T')
{
puts("YES");
exit(0);
}
bool flag1 = false,flag2 = false;
if (x-k>=1 && !before[x-k])
dfs(x-k);
if (x+k <= n&&!before[x+k])
dfs(x+k);
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
scanf("%s",s+1);
int q=1;
rep1(i,1,n)
if (s[i] == 'G')
{
q = i;
break;
}
dfs(q);
puts("NO");
return 0;
}
【64.22%】【codefoces round 382A】Ostap and Grasshopper的更多相关文章
- 【CH 弱省互测 Round #1 】OVOO(可持久化可并堆)
Description 给定一颗 \(n\) 个点的树,带边权. 你可以选出一个包含 \(1\) 顶点的连通块,连通块的权值为连接块内这些点的边权和. 求一种选法,使得这个选法的权值是所有选法中第 \ ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- 1333:【例2-2】Blah数集
1333:[例2-2]Blah数集 注意是数组,答案数组中不能有重复数字 q数组是存储答案的 代码: #include<iostream> #include<cstdio> # ...
- 【UOJ#22】【UR #1】外星人(动态规划)
[UOJ#22][UR #1]外星人(动态规划) 题面 UOJ 题解 一道简单题? 不难发现只有按照从大往小排序的顺序选择的才有意义,否则先选择一个小数再去模一个大数是没有意义的. 设\(f[i][j ...
- JAVA 基础编程练习题22 【程序 22 递归求阶乘】
22 [程序 22 递归求阶乘] 题目:利用递归方法求 5!. 程序分析:递归公式:fn=fn_1*4! package cskaoyan; public class cskaoyan22 { @or ...
- 【题解】Comet OJ Round 70 简要题解
[题解]Comet OJ Round 70 简要题解 A 将放在地上的书按照从小到大排序后,问题的本质就变成了合并两个序列使得字典序最小.可以直接模拟归并排序.直接用循环和std::merge实现这个 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...
- 【BestCoder】【Round#41】
枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...
随机推荐
- CNTK 搞深度学习-1
CNTK 搞深度学习 Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包.本文介绍CNTK的基本内容,如何写CNTK的网络定义语言,以及跑通一个简 ...
- async和await在项目中的应用
Async基础知识: async函数是ES7标准引入的语法,基于Generator函数实现的,也就是说是Generator函数的语法糖.什么是Generator函数?(留个坑) 返回值是Promise ...
- 8.1 Android灯光系统_总体框架
1.框架 APP(java语言实现) ------------------------------- JNI(c++语言实现) 向上提供Java执行c函数的接口 向下访问HAL ------ ...
- 【习题 5-9 UVA - 1596】Bug Hunt
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map模拟 map<string,int>记录每个数组的大小 map <pair<string, int&g ...
- struts2注入类
struts2是能够注入一个对象的,那么一定须要继承ModelDriven的泛型接口. package com.test.action; import com.opensymphony.xwork2. ...
- ldd 查看程序/动态库 的依赖
今天在帮同事查看一个问题时, 需要用到ldd, 于是就顺便看了一下ldd的实现. 好在ldd本身只是一个脚本, 而不是executable, 可以直接查看实现的代码. 根据注释: 21 # This ...
- [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid
For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...
- 多校 hdu
欢迎參加--每周六晚的BestCoder(有米!) Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others) M ...
- 记录一次mysql由5.6升级到5.7出现的异常---Expression #23 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'c.commentCount' which is not functionally dependent on columns in GROUP BY clause;
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expre ...
- Android 离线缓存的高速实现
离线缓存是指在有网络的状态下将从server获取的网络数据.如Json 数据缓存到本地,在断网的状态下启动APP时读取本地缓存数据显示在界面上,经常使用的APP(网易新闻.知乎等等)都是支持离线缓存的 ...