Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 根据给定条件跳过数组元素的算法_Arrays_Algorithm - Fatal编程技术网

Arrays 根据给定条件跳过数组元素的算法

Arrays 根据给定条件跳过数组元素的算法,arrays,algorithm,Arrays,Algorithm,我正在练习解决问题,已经通过了5个测试用例,但有些测试用例失败了,我无法找出我的算法中的问题。虽然我尝试了一些失败测试用例中的测试数据,但大多数都是正确的,但我相信有些是不正确的,因此导致了我的算法失败。因此,如果有人能给出一个正确的方法来实现这个算法,这将是非常有帮助的,或者我在实现中哪里出错了 我的算法: 1. Index for the move is at index '0' of string (say moving index) 2. Loop over the st

我正在练习解决问题,已经通过了5个测试用例,但有些测试用例失败了,我无法找出我的算法中的问题。虽然我尝试了一些失败测试用例中的测试数据,但大多数都是正确的,但我相信有些是不正确的,因此导致了我的算法失败。因此,如果有人能给出一个正确的方法来实现这个算法,这将是非常有帮助的,或者我在实现中哪里出错了

我的算法:

    1. Index for the move is at index '0' of string (say moving index)
    2. Loop over the string starting with index '1' of string:

       2.1. check if (moving index + leap) can outrun the array:

       2.2. If not then, check whether the character is 1 or 0 :

           2.2.1 Check for the number of '1's that are continuous, if they exceed the leap value then return false (as anyway we will not be able to jump).

           2.2.2 If its 0, then check whether its a zero after continuous '1's.

              If not so, continue moving forward one step at a time.

              If so, first try to skip over those continuous '1's by checking whether (moving index + leap) is allowed or not as per the rule.

              If not allowed, check in a while loop till what point we can move backwards one step at a time to get (moving index + leap) to satisfy.

              If not possible, return false.
我不知道这是否是解决这类问题的有效方法,任何其他可能的方法都值得赞赏

代码:

import java.util.*;
公共类解决方案{
公共静态整数跳跃(整数索引、整数跳跃、整数长度、整数[]游戏){
如果(游戏[索引+跳跃]==0){
指数+=闰;
}
收益指数;
}
公共静态布尔canWin(int-leap,int[]游戏){
int指数=0;
int len=游戏长度;
int连续长度=0;
用于(int i=1;ilen-1){
返回true;
}
如果(博弈[i]==1){
连续长度++;
如果(连续长度>=跳跃){
返回false;
}
i++;
}否则{
如果(连续长度==0){
指数=i;
i++;
}否则{
if(索引+leap0&&game[index-1]==0){
布尔值=假;
而(索引>0){
如果(游戏[索引-1]!=0)
返回false;
指数-=1;
i=指数+1;
tryLeap=跳跃(指数、跳跃、镜头、游戏);
如果(索引0){
int n=scan.nextInt();
int leap=scan.nextInt();
int[]游戏=新int[n];
对于(int i=0;i
对我来说,更好的方法是递归地解决这个问题,如下所示(它通过了所有测试):

public静态布尔canWin(int[]数组、int索引、int-leap){
//我们输了的唯一情况
if(索引<0 | |数组[索引]>0){
返回false;
} 
//如果您站在最后一个条目中或(index+leap)>=array.length,则获胜
if((index>=array.length-1)| |((index+leap)>=array.length)){
返回true;
}
//将其标记为已访问,以免再次重复
数组[索引]=1;
//再次递归检查所有3个条件
返回canWin(数组,索引+1,leap)| canWin(数组,索引-1,leap)| canWin(数组,索引+leap,leap);
}
在下面的输入中显示了几对线。每对线的第一个元素表示
leap
,第二个元素表示数组

输入:

三,

0 0 0 0

五,

0 0 1 1 1

三,

0 0 1 1 0

一,

0110

输出:

真的

真的

假的

假的

说明:

假设您当前的位置是
索引

  • 如果是负数或数组值大于
    0
    则游戏失败。如果是最后一个位置或
    index+leap
    至少达到数组长度,则游戏根据定义获胜
  • 否则,从这里开始的唯一可能的移动可能是
    index-1
    index+1
    index+leap
    。因此,对后面的每个索引重复步骤1,并获取结果的
    ,因为找到一条路径就足够了。不要忘记将单元格的值设置为1,因为它对v没有意义这是第二次吗?我们不想一次又一次重复同样的动作而崩溃

  • 对我来说,更好的方法是递归地解决这个问题,如下所示(它通过了所有测试):

    public静态布尔canWin(int[]数组、int索引、int-leap){
    //我们输了的唯一情况
    if(索引<0 | |数组[索引]>0){
    返回false;
    } 
    //如果您站在最后一个条目中或(index+leap)>=array.length,则获胜
    if((index>=array.length-1)| |((index+leap)>=array.length)){
    返回true;
    }
    //将其标记为已访问,以免再次重复
    数组[索引]=1;
    //再次递归检查所有3个条件
    返回canWin(数组,索引+1,leap)| canWin(数组,索引-1,leap)| canWin(数组,索引+leap,leap);
    }
    
    在下面的输入中显示了几对线。每对线的第一个元素表示
    leap
    ,第二个元素表示数组

    输入:

    三,

    0 0 0 0

    五,

    0 0 1 1 1

    三,

    0 0 1 1 0

    一,

    0110

    输出:

    真的

    真的

    假的

    假的

    说明:

    假设您当前的位置是
    索引

  • 如果是负数或数组值大于
    0
    则游戏失败。如果是最后一个位置或
    index+leap
    至少达到数组长度,则游戏根据定义获胜
  • 否则,从这里开始的唯一可能的移动可能是
    index-1
    index+1
    index+leap
    。因此,对后面的每个索引重复步骤1,并获取结果的
    ,因为找到一条路径就足够了。不要忘记设置单元格的值
    
    import java.util.*;
    
    public class Solution {
    
        public static int leapStep(int index,int leap,int len,int[] game){
            if(game[index+leap]==0){
                index += leap;
            }
            return index;
        }
    
        public static boolean canWin(int leap, int[] game) {
            int index = 0;
            int len = game.length;
            int consecutiveLength=0;
            for(int i=1;i<len;){
                if(index+leap>len-1){
                    return true;
                }
                if(game[i]==1){
                    consecutiveLength++;
                    if(consecutiveLength>=leap){
                        return false;
                    }
                    i++;
                }else{
                    if(consecutiveLength==0){
                        index =i;
                        i++;
                    }else{
                        if(index+leap<=len-1){
                            int tryLeap = leapStep(index,leap,len,game);
                            if(index < tryLeap){
                                index = tryLeap;
                                tryLeap =0;
                                i = index+1;
                            }else if(index>0 && game[index-1]==0 ){
                                boolean notViable = false;
                                while(index>0){
                                    if(game[index-1]!=0)
                                        return false;
                                    index -= 1;
                                    i = index+1;
                                    tryLeap = leapStep(index,leap,len,game);
                                    if(index<tryLeap){
                                        index = tryLeap;
                                        i = index+1;
                                        tryLeap=0;
                                        notViable = false;
                                        break;
                                    }
                                    else{
                                        notViable = true;
                                    }
                                }
                                if(notViable){
                                    return false;
                                }
                            }else{
                                return false;
                            }
                        }
                        consecutiveLength=0;
                    }
                }
            }//closing for
             return true;
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int q = scan.nextInt();
            while (q-- > 0) {
                int n = scan.nextInt();
                int leap = scan.nextInt();
    
                int[] game = new int[n];
                for (int i = 0; i < n; i++) {
                    game[i] = scan.nextInt();
                }
    
                System.out.println( (canWin(leap, game)) ? "YES" : "NO" );
            }
            scan.close();
        }
    }
    
    public static boolean canWin(int[] array, int index, int leap) {
        // the only case when we lose
        if (index < 0 || array[index] > 0) {
            return false;
        } 
    
        // if you're standing in the last entry or (index + leap) >= array.length then win
        if ((index >= array.length - 1) || ((index + leap) >= array.length)) {
            return true;
        }
    
        // mark it as visited so that not to iterate over it again
        array[index] = 1;
        // check all 3 conditions then recursively again
        return canWin(array, index + 1, leap) || canWin(array, index - 1, leap) || canWin(array, index + leap, leap);
    }
    
        public static boolean check(int[] game, boolean[] memo, int index){
            if(index >= 0 && index < game.length){
                if(game[index] != 1){
                    memo[index] = true;
                }
            }
            return index >= game.length;
        }
    
        public static void solveOne(){
            int n = sc.nextInt();
            int leap = sc.nextInt();
    
            int[] game = new int[n];
            for (int i = 0; i < n; i++) {
                game[i] = sc.nextInt();
            }
    
            int index = 0;
            boolean[] memoTab = new boolean[n];
            for (int i = 0; i < n; i++) {
                memoTab[i] = false;
            }
            memoTab[0] = true;
    
            boolean rememberIndex0 = false;
            boolean gotoIndex0 = false;
            int index0 = 0;
    
            boolean finished = false;
    
            while(index < game.length){
                // we encounter the first 0 after some 1, keep it in memory !
                if(rememberIndex0 && game[index] == 0){
                    index0 = index;
                    gotoIndex0 = true;
                    rememberIndex0 = false;
                }
    
                // this index is an index we reached before, we can continue from here
                if(memoTab[index]){
                    // we previously said we need to go back to a lower position
                    if(gotoIndex0){
                        gotoIndex0 = false;
                        index = index0;
                        memoTab[index] = true;
                        continue;
                    }
                    // it's finished if either is true
                    finished = check(game, memoTab, index + 1)
                            || check(game, memoTab, index + leap);
    
                    if(finished) break;
                }
    
                //  if this position is a 1, then we will keep in memory the next 0
                if(game[index] == 1){
                    rememberIndex0= true;
                }
    
                // don't forget incrementing
                index += 1;
            }
    
            System.out.println(finished?"YES":"NO");
        }