-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeapGame.java
47 lines (39 loc) · 852 Bytes
/
LeapGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
https://www.hackerrank.com/challenges/java-1d-array
*/
import java.util.*;
public class Main{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int q = input.nextInt();
while(q>0){
leapGame();
q--;
}
}
public static void leapGame(){
int n = input.nextInt();
int leap = input.nextInt();
int[] game = new int[n];
for(int i=0;i<n;i++)
game[i] = input.nextInt();
if(solved(game,leap,0))
System.out.println("YES");
else
System.out.println("NO");
}
public static boolean solved(int[] game,int leap,int pos){
if(pos >= game.length)
return true;
else if(pos<0)
return false;
else if(game[pos]==1)
return false;
game[pos] = 1;
return (
solved(game,leap,pos+leap) ||
solved(game,leap,pos+1) ||
solved(game,leap,pos-1)
);
}
}