The Way to Home (模拟)

本文介绍了一个经典的算法问题——青蛙如何在限定条件下用最少的跳跃次数从起点1抵达终点n。通过分析给定的01字符串,确定每个位置是否有可用的落脚点,并实现了一种有效的算法来解决此问题。

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.

给定n,d,两个整数,然后一个01字符串,n是这个字符串的长度,青蛙只能向右跳,d是每次它向右跳的最大步数,要求每次只能跳到1上,问青蛙从1跳到n最少跳几次

我的思路是从1起点开始,先加上最大跳跃步数,然后从这个下标往回找找到1,更新起点,答案加一,否则不能到达输出-1

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
    char s[109];
    int n,d;
    scanf("%d%d",&n,&d);
    getchar();
    for(int i = 1; i <= n; i++){
        scanf("%c",&s[i]);
    }
    int i = 1;
    int ans = 0;
    while(i <= n){//起点
        int j = i + d;//每次从起点跳最远到达的下标
        if(j > n){//如果从起点跳的最远下标超过了n肯定可达并结束
            ans++;
            break;
        }
        int flag = 0;
        while(j > i){
            if(s[j] == '1'){//找到第一个1停止
                ans++;
                flag = 1;
                i = j;
                break;
            }
            j--;
        }
        if(i == n) break;//如果恰好到了n退出循环
        if(!flag){//如果没有1说明不能到达
            printf("-1\n");
            return 0;
        }
    }
    printf("%d\n",ans);
    return 0;
}


The university of Bithampton is served by exactly one bus line. On itsway to the city centre, it serves several stops at which students may exit.Every student has a fixed bus stop where they want to exit.It is Friday afternoon, 4 PM, and as always, all the students want to gohome, leading to quite a long queue at the bus stop. Fortunately, the busline is served in regular intervals, with the first bus arriving at 4 PM.Whenever a bus arrives at the university, everyone in the queue triesto enter the bus, which makes the buses very crowded. This has led tonumerous complaints where people tried to exit buses but were unableto because of the sheer amount of people. As a consequence, the buscompany decided that at every stop which someone in the bus has asdestination, everyone in the bus must exit it. Those who want to travelfurther enter again. For every time a passenger enters or exits the bus, the bus needs to wait wseconds.To offer the best service, the bus company wants to minimize the maximum time it takes anybodyfrom 4 PM until they reach their destination. For each bus, the bus driver can decide how manypeople from the front of the queue enter the bus. The number of people that can enter a bus isunlimited. Help the bus drivers make the optimal decisions to achieve the company’s goal.InputThe input consists of:• One line with four integers n, b, r, and w (1 ≤ n, b ≤ 105, 1 ≤ r, w ≤ 106), the numberof passengers, the number of bus stops, the time between the arrival of two buses at theuniversity, and the delay for exiting and entering.• One line with b integers di (1 ≤ di , Pdi ≤ 106 ), the travel time from the (i − 1)th busstop to the ith bus stop (the 0th bus stop is the university).• One line with n integers ti (1 ≤ ti ≤ b), indicating that the destination of the ith person inline is the tith bus stop.All times are given in seconds.OutputOutput the minimum number of seconds until every person in line has exited at their destination.用c++做出来
最新发布
10-02
C:\Users\黄致冬>pip show notebook Name: notebook Version: 7.4.4 Summary: Jupyter Notebook - A web-based notebook environment for interactive computing Home-page: https://github.com/jupyter/notebook Author: Author-email: Jupyter Development Team <jupyter@googlegroups.com> License: BSD 3-Clause License - Copyright (c) 2001-2015, IPython Development Team - Copyright (c) 2015-, Jupyter Development Team All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Location: C:\Users\黄致冬\AppData\Roaming\Python\Python312\site-packages Requires: jupyter-server, jupyterlab, jupyterlab-server, notebook-shim, tornado Required-by: jupyter
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值