zoukankan      html  css  js  c++  java
  • A. The Way to Home

    A. The Way to Home

    A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.

    For each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1 and n.

    Determine the minimal number of jumps that the frog needs to reach home which is in the point n from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.

    Input

    The first line contains two integers n and d (2 ≤ n ≤ 100, 1 ≤ d ≤ n - 1) — the point, which the frog wants to reach, and the maximal length of the frog jump.

    The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.

    Output

    If the frog can not reach the home, print -1.

    In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1.

    Examples
    input
    8 4
    10010101
    output
    2
    input
    4 2
    1001
    output
    -1
    input
    8 4
    11100101
    output
    3
    input
    12 3
    101111100101
    output
    4
    Note

    In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).

    In the second example the frog can not reach home, because to make it she need to jump on a distance three, but the maximum length of her jump equals to two.

     DP问题,从0走到n,最大走d步,字符为1才能走。

    1 n,d = map(int,input().split())
    2 s = input()
    3 dp = [100000]*n
    4 dp[0] = 0
    5 for i in range(n):
    6     if s[i] == '1':
    7         for j in range(max(0,i-d),i):
    8             dp[i] = min(dp[i],dp[j]+1)
    9 print([-1,dp[n-1]][dp[n-1]!=100000])
  • 相关阅读:
    selenium——上传文件
    selenium——下拉框
    selenium——鼠标操作ActionChains:点击、滑动、拖动
    selenium定位元素—逻辑运算、xpath函数、轴定位
    spring设置webAppRootKey
    树莓3 集群
    redis集群搭建
    MQ基础
    MQ集群测试环境搭建(多节点负载均衡,共享一个kahaDB文件(nas方式))
    weblogic对jms实现的QueueConnection实现与TopicConnection实现问题
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8214016.html
Copyright © 2011-2022 走看看