Codeforces Round #382 (Div. 2)-735A. Ostap and Grasshopper(BFS)

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

题意:给出N和K,接着给出一个长度为N的字符串,G代表起点,T代表终点,.代表空,#代表有障碍,从起点到终点可以选择向左或者向右跳K个格子并且不允许跳到#上面。问是否能跳到终点。

思路:暴力广搜出左右方向所有可能

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
const int maxn=105;
int N,K;
char str[maxn];
int vis[maxn];
bool Judge(int x)
{
    if(x<0||x>=N||str[x]=='#'||vis[x]==1)
        return false;
    return true;
}
void BFS(int star,int endn)
{
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(star);
    vis[star]=1;
    while(!q.empty())
    {
        star=q.front();
        q.pop();
        if(star==endn)
        {
            printf("YES\n");
            return;
        }
        if(Judge(star+K)==true)
        {
            q.push(star+K);
            vis[star+K]=1;
        }
        if(Judge(star-K)==true)
        {
            q.push(star-K);
            vis[star-K]=1;
        }
    }
    printf("NO\n");
}
int main()
{

    scanf("%d%d",&N,&K);
    scanf("%s",str);
    int star,endn;
    for(int i=0; i<N; i++)
    {
        if(str[i]=='G')
            star=i;
        if(str[i]=='T')
            endn=i;
    }
    BFS(star,endn);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值