-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava1darray -2
More file actions
130 lines (109 loc) · 3.33 KB
/
Copy pathjava1darray -2
File metadata and controls
130 lines (109 loc) · 3.33 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.*;
public class Solution {
//public static boolean flg = false;
public static boolean canWin(int leap, int[] game) {
boolean ans = helper(leap,game,0,false);
return ans;
}
public static boolean helper(int leap, int[] game,int pos, boolean flg)
{
if(flg)
{
return true;
}
System.out.println(pos+" , "+flg);
int l = game.length-1;
if(pos == l || (pos+leap) >=l )
{
flg=true;
return flg;
}
if(game[pos+1]==0)
{
helper(leap,game,pos+1,flg);
}
if(game[pos+leap]==0)
{
helper(leap,game,pos+leap,flg);
}
return flg;
}
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();
}
}
#########LOOKS GOOD#######
import java.util.*;
public class Solution {
//public static boolean flg = false;
public static boolean canWin(int leap, int[] game) {
boolean ans = helper(leap,game,0,false,0);
return ans;
}
public static boolean helper(int leap, int[] game,int pos, boolean flg,int fl)
{
if(game[pos]==1 || fl==1)
{
fl=1;
return false;
}
if(flg)
{
return true;
}
//System.out.println(pos+" , "+flg);
int l = game.length-1;
if(pos == l || (pos+leap) >=l )
{
//System.out.println("i'm in");
return true;
}
if(game[pos+1]==0 && flg!=true)
{
//System.out.println("i'm in again");
game[pos]=1;
flg= helper(leap,game,pos+1,flg,0);
}
if(game[pos+leap]==0 && flg!=true)
{
//System.out.println("i'm in again");
game[pos]=1;
flg = helper(leap,game,pos+leap,flg,0);
}
if(pos>0)
{
if(game[pos-1]==0 && flg!=true)
{
//System.out.println("i'm in again");
game[pos]=1;
flg = helper(leap,game,pos-1,flg,0);
}
}
return flg;
}
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();
}
}