【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,结果还是只 ...
随机推荐
- P2P借款的几种情况
借款,至少出现2种人,借款人和出借人.根据人的性质,企业和个人,分成4种情况. 企业-个人,企业-企业,个人-企业,个人-个人. P2P平台可能出现几种情况: 个人-个人 2种情况: a. 借款人 ...
- OpenJDK源码研究笔记(十六):在Java中使用JavaScript脚本语言
友情提示 本文主要参考了51CTO上的一篇文章,代码经过自己的模仿和整理,还算凑合. 本文中的代码注释比较多,不再过多解释. 更多用法,还是得看JDK的API或者看原文http://developer ...
- C语言深度剖析-----函数与指针的分析
指针的本质 指针需要保证指向任意数据类型,所以指针变量都占用32位bit即4字节. PS:不同机器上,指针占用内存不一 ...
- IOS日期转为今天昨天形式
近期项目有类似QQ空间展示动态的UI,模仿了QQ空间的时间显示.在此记录,以备查阅. 这是QQ空间的ui: 时间显示为: 1.今天-->今天 xx:xx(今天 15:39) 2.昨天--> ...
- Windows Vista下UIPI和Windows消息的故事(UIPI默认情况下会禁止所有高于WM_USER的消息)
一. 什么是UIPI UIPI指User Interface Privilege Isolation(用户界面特权隔离),是Windows Vista 新引入的一种安全特性.用于拦截接收比自身进程MI ...
- [WPF自定义控件库]排序、筛选以及高亮
1. 如何让列表的内容更容易查找 假设有这么一个列表(数据源在本地),由于内容太多,要查找到其中某个想要的数据会比较困难.要优化这个列表,无非就是排序.筛选和高亮. 改造过的结果如上. 2. 排序 在 ...
- 得到INI文件所有Section(所有节点名称)
char SectionNames[MAX_PATH],*pSectionName; ZeroMemory(SectionNames,MAX_PATH); GetPrivateProfileSecti ...
- [Angular2 Animation] Control Undefined Angular 2 States with void State
Each trigger starts with an “undefined” state or a “void” state which doesn’t match any of your curr ...
- php中模拟多继承如何实现
php中模拟多继承如何实现 一.总结 一句话总结:其实你继承别人也是想调用别人类里面的方法和属性,所以可以这样做:这本类中创建目标类的对象,然后通过这个对象来调用方法和属性,这样比继承来的方便. 二. ...
- java I/O库的设计模式
在java语言 I/O库的设计中,使用了两个结构模式,即装饰模式和适配器模式. 在任何一种计算机语言中,输入/输出都是一个很重要的部分.与一般的计算机语言相比,java将输入/输出的功能和使 ...