美文网首页
leetcode403青蛙过河问题

leetcode403青蛙过河问题

作者: HelloWorldWow | 来源:发表于2018-10-17 17:27 被阅读0次

一.问题描述

  1. 一只青蛙正在过河。 河流分为x个单位,每个单位可能存在或不存在石头。 青蛙可以跳到石头上,但不能跳入水中。

  2. 给出按照升序排序的石块位置(单位),确定青蛙是否能够通过落在最后一块石头上过河。 最初,青蛙在第一块石头上并假设第一次跳跃必须是1个单位。

  3. 如果青蛙的最后一次跳跃是k个单位,那么它的下一个跳跃必须是k-1,k或k + 1个单位。 请注意,青蛙只能向前

* 石头的数量 > 0 & <1,100
* 每个石头的位置都是非负整数 <  2^31
* 第一块石头的位置始终为0。
Example 1:
[0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. The frog can jump to the last stone by jumping 
1 unit to the 2nd stone, then 2 units to the 3rd stone, then 
2 units to the 4th stone, then 3 units to the 6th stone, 
4 units to the 7th stone, and 5 units to the 8th stone.

Example 2:
[0,1,2,3,4,8,9,11]

Return false. There is no way to jump to the last stone as 
the gap between the 5th and 6th stone is too large.

二.代码实现

#include"stdlib.h"
#include"stdio.h"

#define MAX_LEN 1100

bool canCrossedByStep(int* stones, int stonesSize,int i,int j,int curStep);
bool canCross(int* stones, int stonesSize);
bool canCross(int* stones, int stonesSize) {
    if(stonesSize < 2)
    {
        return true;
    }
    if(stones[0] != 0 || stones[1] != 1)
    {
        return false;
    }
    return canCrossedByStep(stones, stonesSize,1,2, 1);
    
}

//i当前所在的位置,j寻找下一步的开始位置,curStep当前步长
bool canCrossedByStep(int* stones, int stonesSize,int i,int j,int curStep)
{
    if (j == stonesSize)
    {
        return true;
    }

    for (int k = j; k < stonesSize; ++k)
    {
        //已跳不过去,因为stones[index] > stones[k](index > k)
        if (stones[k] > stones[i] + curStep + 1)
        {
            return false;
        }
        else if (stones[k] == stones[i] + curStep + 1)
        {
            //后面不可能再跳过去了,可以直接return
            //printf("stones[j] == stones[i] + curStep + 1,stones[i]:%d,stones[j]:%d,step:%d\n",stones[i],stones[k],curStep + 1);
            return canCrossedByStep(stones, stonesSize,k,k+1,curStep + 1);
        }
        else if (stones[k] == stones[i] + curStep)
        {
            //printf("stones[j] == stones[i] + curStep,stones[i]:%d,stones[j]:%d,step:%d\n",stones[i],stones[k],curStep);
            if(canCrossedByStep(stones, stonesSize,k,k+1,curStep))
            {
                return true;
            }
        }
        else if (curStep - 1 > 0 && stones[k] == stones[i] + curStep - 1)
        {
            //printf("stones[j] == stones[i] + curStep - 1,stones[i]:%d,stones[j]:%d,step:%d\n",stones[i],stones[k],curStep - 1);
            if(canCrossedByStep(stones, stonesSize,k,k+1,curStep - 1))
            {
                return true;
            }
        }
    }
    return false;
}

int main()
{
    // int a[] = {0,1,3,5,6,8,12,17};
    // int b[] = {0,1,2,3,4,8,9,11};

    while(true){
        int buf[MAX_LEN];
        int index = 0;
        printf("please input array,limited length 1100:\n");
        while(scanf("%d",&buf[index]) && buf[index] >= 0){
           // printf("%d\n",buf[index]);
            ++index;
            if (index >= 1100)
            {
                break;
            }
        }
        printf("length:%d\n", index);
        printf("the result is:%d\n",canCross(buf,index));
    }
    return 1;
}


三.测试结果

image.png

相关文章

  • leetcode403青蛙过河问题

    一.问题描述 一只青蛙正在过河。 河流分为x个单位,每个单位可能存在或不存在石头。 青蛙可以跳到石头上,但不能跳入...

  • 【底层思维】从本能谈起

    先讲一个故事。 一只蝎子要过河,就叫来青蛙,请青蛙背它过河。青蛙说:“那不行,我背你过河,你可不得咬我啊!”。蝎子...

  • 日更|微故事:两只青蛙过河

    故事:两只青蛙过河 有两只青蛙要过河,但河中间有一堆大石头,青蛙们只能跳到石头上才能过河。其中一只青蛙很自信地跳了...

  • 独木桥(青蛙过河)问题思考

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子,青蛙很讨厌踩在这些石子上...

  • 蝎子过河

    一只蝎子想要过河水,于是它来到岸边对青蛙说:“青蛙,你可以背我过河吗?" 青蛙说:“不可以,你肯定趁我游泳的时候蛰...

  • 不忘初心

    一只蝎子到了河边想过河,但不可以,因为它不能下水,之后他见到了一只青蛙,就问青蛙:“你可以背我过河吗?”青蛙说:“...

  • 江山易改,本性难移!

    一只蝎子到了河边想过河,但不可以,因为它不能下水,之后他见到了一只青蛙,就问青蛙:“你可以背我过河吗?” 青蛙说:...

  • 《HOW NASA BUILDS TEAMS》习书系列记录-6-

    ?️ & ? 一只蝎子要过河,但蝎子不会游泳,正当他要放弃的时候,一只青蛙游过来。蝎子喊到,青蛙先生,把我捎过河吧...

  • 青蛙和蝎子

    有只蝎子要过河,对青蛙说:“你驮我过河好不好?” 青蛙犯难的说:“我倒很乐意帮忙,但我怕你会蛰我!” “不会的,不...

  • 女版西门庆,就是要做个真的我

    有则寓言故事很有型,在此分享给痴男怨女,“一只蝎子无法过河,于是对在岸边的青蛙说能否带它过河,青蛙不肯,说道:俺游...

网友评论

      本文标题:leetcode403青蛙过河问题

      本文链接:https://www.haomeiwen.com/subject/pzurzftx.html