개요[ | ]
- HR자바 Java 1D Array (Part 2)
Java
Copy
import java.util.*;
public class Solution {
public static boolean canWin(int leap, int[] game) {
// Return true if you can win the game; otherwise, return false.
return canWin(leap, game, 0);
}
public static boolean canWin(int leap, int[] game, int pos) {
if( pos < 0 ) return false;
if( game[pos] == 1 ) return false;
if( pos+leap >= game.length ) return true;
if( pos+1 >= game.length ) return true;
game[pos] = 1;
return canWin(leap, game, pos+leap)
|| canWin(leap, game, pos+1)
|| canWin(leap, game, pos-1);
}
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();
}
}
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.