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.

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).
这里写图片描述
这里写图片描述

题目大意:

一个蚂蚱可以从左跳k格,也可以从右跳k格,标#的点不能跳,问能否跳到T吃掉小昆虫?

解题思路:

分别从左右方向模拟即可
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
  int n,k,location;
  cin>>n>>k;
  char line[110];
  for(int i=0;i<n;i++)
  {
    cin>>line[i];
    if(line[i]=='G')location=i;
  }
  int left=location,right=location;
  bool flag=false;
  while(left>=0)
  {
    left-=k;
    if(left>=0)
    {
      if(line[left]=='T')
      {
        flag=true;
        break;
      }
      else if(line[left]=='#')break;
    }
  }
  if(!flag)
  {
    while(right<n)
    {
      right+=k;
      if(right<n)
      {
        if(line[right]=='T')
        {
          flag=true;
          break;
        }
        else if(line[right]=='#')break;
      }
    }
  }
  if(flag)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
  return 0;
}

50篇留念

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MooBook-魔书(手机书阅读器) V0.0.0.9Beta: Windows平台,PC端的手机书阅读软件。 1.目前支持TXT(GB2312/UNICODE/UTF-8)、JAR(普通单文档)、UMD(普通文本/杰奇文本)、HTM(网页文件)和HTML(网页文件)格式的阅读; 2.支持拖拽打开文档及书签直接打开文档; 3.支持自动计算分页及当前阅读页标记; 4.支持书签记录(程序做了限制,最多支持添加100条书签),及书签窗口中的右键菜单控制书签目录的功能。支持书签按照文件名称和添加书签时间排序; 5.支持跳转分页; 6.UMD文件自动更换为分章节阅读; 7.支持单页查找(文字搜索); 8.支持文件信息显示; 9.程序会自动识别TXT(GB2312/UNICODE/UTF8)编码。同时,支持用户手动设置文件的编码显示格式; 10.支持双击分页目录快速阅读; 11.支持快速打开推荐页,点击目录标签仍可返回浏览; 12.支持键盘左右键翻页; 13.在阅读页面的文字上支持右键菜单复制,目前不支持[Ctrl+C]复制文字内容; 14.支持窗口最大化显示,支持拉伸改变窗口大小,阅读区的文字内容会随窗口大小的改变而自动换行; 15.支持使用窗口分割条来动态调整工作区和阅读区的窗口大小; 16.在鼠标按钮的默认设置模式下:用鼠标左键点击窗口分割条上的箭头按钮,支持窗口分割条快速伸缩;用鼠标右键点击窗口分割条上的箭头按钮,支持调整窗口分割条快速伸缩的方向; 17.程序采用UNICODE工程开发,在非中文系统(操作系统需要有中文语言包)下不会出现中文乱码; 18.阅读窗口的内容采用[HTML+CSS]框架结构显示,如需调整文字的颜色、字体和大小等,请手动修改“MooBook\Database\book.css”样式文件进行配置; 19.支持软件在线升级; BUG和建议请您发送到 moobook#126.com ("#"请用"@"替代),我们万分感谢!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值