zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    sqlserver 镜像 断开连接 正在恢复+主机服务器关机用备用镜像
    SQL SERVER 2005镜像配置(有无见证服务器都行)
    Terminal Service 终端链接
    SSL/TLS Diffie-Hellman Modulus <= 1024 位 (LogJam) 使用2048位或更高的Diffie-Hellman
    各国各种语言翻译
    Windows Server 2012上安装.NET Framework 3.5
    ALM11服务器IP变更相关配置修改
    转载:Linux下启动和关闭Weblogic(管理服务器+被管服务器)
    转载:AWR介绍使用
    安装QTP之后造成环境变量java冲突问题的解决方案
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626924.html
Copyright © 2011-2022 走看看