Java 1D Array (Part 2)

윤지현·2025년 5월 9일
0

HackerRank[Java]

목록 보기
56/57
  • 문제
  • 정답
import java.util.*;

public class Solution {

    public static boolean canWin(int leap, int[] game) {
        return isWinnable(0, leap, game);
    }
    
    private static boolean isWinnable(int i, int leap, int[] game) {
        int n = game.length;

        if (i >= n) return true;

        if (i < 0 || game[i] == 1) return false;

        game[i] = 1;

        return isWinnable(i + leap, leap, game) ||
               isWinnable(i + 1, leap, game) ||
               isWinnable(i - 1, leap, game);
    }

    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();
    }
}
  • 결과
profile
첫 시작

0개의 댓글