【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,结果还是只 ...
随机推荐
- UVA 10125 - Sumsets(POJ 2549) hash
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 读文件头数据判断 PE 文件格式和类型
namespace X.Reflection { using System; using System.IO; public static partial class ReflectionX { pu ...
- C++开源码项目汇总
Google的C++开源码项目 v8 - V8 JavaScript Engine V8 是 Google 的开源 JavaScript 引擎. V8 採用 C++ 编写,可在谷歌浏览器(来自 G ...
- 用FATFS在SD卡里写一串数字
用FATFS写SD卡,如写入数组 s[] ={1,2,3,4,5,6} 想要在txt中显示“123456” 就要把 s[0]=1+'0' 或 s[0]=1+48 或 s[0]=1+0x30 ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
- 开源分享三(炫酷的Android Loading动画)
开源分享三(炫酷的Android Loading动画) 分享GitHub上的一些Loading,为了提升产品用户体验,一个好的Loading必然是不可缺少的,对于一些耗时需要用户等待的页面来说会转移用 ...
- 我的前端规范——HTML篇
相关文章 简书原文:https://www.jianshu.com/p/a46ff0504982 我的前端规范——开篇:http://www.cnblogs.com/shcrk/p/9271561.h ...
- ZOJ 1203 Swordfish MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203 大意: 给出一些点,求MST 把这几天的MST一口气发上来. kru ...
- Chinese remainder theorem
https://en.wikipedia.org/wiki/Chinese_remainder_theorem http://planetmath.org/ChineseRemainderTheore ...
- LSH︱python实现局部敏感随机投影森林——LSHForest/sklearn(一)
关于局部敏感哈希算法.之前用R语言实现过,可是由于在R中效能太低.于是放弃用LSH来做类似性检索.学了python发现非常多模块都能实现,并且通过随机投影森林让查询数据更快.觉得能够试试大规模应用在数 ...