-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava 1D Array (Part 2).txt
62 lines (59 loc) · 1.4 KB
/
Java 1D Array (Part 2).txt
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.*;
import java.util.*;
public class CandidateCode
{
public static boolean canWin(int leap, int[] game)
{
if (game == null)
{
return false;
}
return isSolvable(leap, game, 0);
}
private static boolean isSolvable(int leap, int[] game, int i) {
if (i >= game.length)
{
return true;
}
else if (i < 0 || game[i] == 1)
{
return false;
}
game[i] = 1; // marks as visited
// Recursive Cases (Tries +m first to try to finish game quickly)
return isSolvable(leap, game, i + leap)|| isSolvable(leap, game, i + 1)|| isSolvable(leap, game, i - 1);
}
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_num=Integer.parseInt(br.readLine());
for(int j = 0; j < test_num; j++)
{
String lines = br.readLine();
String[] strs = lines.trim().split("\\s+");
int a[]=new int[strs.length];
for (int i = 0; i < strs.length; i++)
{
a[i] = Integer.parseInt(strs[i]);
}
String quant = br.readLine();
String[] strs1 = quant.trim().split("\\s+");
int b[]=new int[strs1.length];
for (int k = 0; k < strs1.length; k++)
{
b[k] = Integer.parseInt(strs1[k]);
}
int n=b.length;
int leap=a[1];
Boolean q=canWin(leap,b);
if(q==true)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
}