zoukankan      html  css  js  c++  java
  • Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs

    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好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e2+7;
    string s;
    int vis[maxn],st,n,ed,k;
    int main()
    {
        queue<int>Q;
        cin>>n>>k;
        cin>>s;
        for(int i=0;i<s.size();i++)
            if(s[i]=='G')st=i;
            else if(s[i]=='T')ed=i;
        Q.push(st);
        while(!Q.empty()){
            int now=Q.front();
            Q.pop();
            if(vis[now])continue;
            vis[now]=1;
            if(now+k<s.size()&&vis[now+k]==0&&s[now+k]!='#')
                Q.push(now+k);
            if(now-k>=0&&vis[now-k]==0&&s[now-k]!='#')
                Q.push(now-k);
        }
        if(vis[ed])cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
  • 相关阅读:
    老年人微信教程手绘版|微信入门教程1
    微信网页版朋友圈在哪?怎么找不到
    如何用<dl>标签做表格而不用table标签
    600万个!520元的微信红包发了这么多!
    微信红包最高能发520元啦!只限今天!
    微信和WeChat的合并月活跃账户数达到7.62亿了
    excel隔行选中内容如何操作
    各大公司广泛使用的在线学习算法FTRL详解
    在线最优化求解(Online Optimization)之五:FTRL
    在线最优化求解(Online Optimization)之四:RDA
  • 原文地址:https://www.cnblogs.com/qscqesze/p/6168960.html
Copyright © 2011-2022 走看看