A. Ostap and Grasshopper

题面

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.

输入

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.

输出

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).

样例输入

5 2

G#T#

样例输出

YES

题意

给你一个长度为n的字符串,G是起点,T是终点,现在你每步要走距离为k,现在问你能否从G走到T。

题解

直接暴力bfs好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e2+7;
  4. string s;
  5. int vis[maxn],st,n,ed,k;
  6. int main()
  7. {
  8. queue<int>Q;
  9. cin>>n>>k;
  10. cin>>s;
  11. for(int i=0;i<s.size();i++)
  12. if(s[i]=='G')st=i;
  13. else if(s[i]=='T')ed=i;
  14. Q.push(st);
  15. while(!Q.empty()){
  16. int now=Q.front();
  17. Q.pop();
  18. if(vis[now])continue;
  19. vis[now]=1;
  20. if(now+k<s.size()&&vis[now+k]==0&&s[now+k]!='#')
  21. Q.push(now+k);
  22. if(now-k>=0&&vis[now-k]==0&&s[now-k]!='#')
  23. Q.push(now-k);
  24. }
  25. if(vis[ed])cout<<"YES"<<endl;
  26. else cout<<"NO"<<endl;
  27. }

Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs的更多相关文章

  1. Codeforces Round #382 (Div. 2)E. Ostap and Tree

    E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

  3. Codeforces Round #382 (Div. 2) (模拟|数学)

    题目链接: A:Ostap and Grasshopper B:Urbanization C:Tennis Championship D:Taxes 分析:这场第一二题模拟,三四题数学题 A. 直接模 ...

  4. Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想

    D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...

  5. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

  6. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  7. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  8. Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契

    C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. Codeforces Round #382 (Div. 2) D. Taxes 歌德巴赫猜想

    题目链接:Taxes D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. NOIP2011 题解

    铺地毯 题解:比大小 #include <cstdio> +; int n, x, y, a[MAXN], b[MAXN], g[MAXN], k[MAXN]; inline int So ...

  2. select 练习2

    group by :select 分组字段,聚合函数 from 表名 where  条件 group by 分组字段 having 过滤条件 select cno,avg(degree) from s ...

  3. SkipList算法实现

    SkipList是一种快速查找链表,链表元素的是有序的.由W.Pugh发明于1989年.其算法复杂度如下: Average Worst caseSpace O(n) O(n log n)Search ...

  4. Registered Nurse in the US

    注册护士移民美国的条件 美国护士RN考试介绍 美国注册护士考试复习 美国各州注册护士考试要求 CGFNS Registered nurse Top Paid Registered Nurses

  5. Sql语句,先查询再插入一条语句完成。

    if ( (select COUNT(*) from Hr where 考勤号码 = '149' and 日期时间 = '2015/7/3 12:00:26') = 0 )INSERT  INTO [ ...

  6. Unrecognized Windows Sockets error: 0: JVM_Bind 异常怎么办

    Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法 java.net.SocketException: Unrecognized Window ...

  7. 39行代码实现JS HTML模板(轻量+高效+易用)

    otmpl 是一个轻量级前端模版(仅有39行无压缩代码,支持缓存),所用指令仅需[#...#]和{#...#},前者包含需要输出html语句,后者包含js变量. 支持javascript完整语法,你可 ...

  8. JAVA通过XPath解析XML性能比较(原创)

    (转载请标明原文地址) 最近在做一个小项目,使用到XML文件解析技术,通过对该技术的了解和使用,总结了以下内容. 1 XML文件解析的4种方法 通常解析XML文件有四种经典的方法.基本的解析方式有两种 ...

  9. C/C++文件操作1

    在C语言中,文件操作都是由库函数来完成的. 在本章内将介绍主要的文件操作函数. 文件打开函数fopen fopen函数用来打开一个文件,其调用的一般形式为:文件指针名=fopen(文件名,使用文件方式 ...

  10. 如何利用tomcat搭建一个动态服务器

    这篇文章只记录已解压缩包的方式安装,通常linux服务器上也是这样. 1.下载tomcat.zip压缩包. http://tomcat.apache.org/download-70.cgi 2.把zi ...