JumpGame 문제 오답케이스 확인이 가능할까요?

  • yeoshim
    yeoshim

    JumpGame 문제 오답케이스 확인이 가능할까요?
    JUMPGAME

    동적 프로그래밍으로 작성했는데 계속 오답으로 나옵니다.

    import java.util.Scanner;
    
    public class Main {
        private static Scanner sc;
    
        public static void main(String[] args) {
            sc = new Scanner(System.in);
            int cases = sc.nextInt();
            while(cases-- > 0) {
                int size = sc.nextInt();
    
                int[][] matrix = new int[size][size];
                for (int i = 0; i < matrix.length; i++) {
                    for (int j = 0; j < matrix[0].length; j++) {
                        matrix[i][j] = sc.nextInt();
                    }
                }
    
                int[][] cache = new int[size][size];
                for (int i = 0; i < cache.length; i++) {
                    for (int j = 0; j < cache[0].length; j++) {
                        cache[i][j] = -1;
                    }
                }
    
                if( jump(cache, matrix, 0, 0) > 0 ) System.out.println("YES");
                else    System.out.println("NO");
            }
        }
    
        //  base: x == y == matrix.size return true, x > matrix.size || y > matrix.size return false
        //  induction: jump(x+matrix[x][y], y) || jump(x, y+matrix[x][y]
        private static int jump(int[][] cache, int[][] matrix, int y, int x) {
            int len = matrix.length - 1;
            if( x > len || y > len )    return 0;
            if( x == len && y == len )  return 1;
    
            if( cache[y][x] != -1 ) return cache[y][x];
    
            return cache[y][x] = jump(cache, matrix, y+matrix[y][x], x) + jump(cache, matrix, y, x+matrix[y][x]);
        }
    }
    

    8년 전
1개의 댓글이 있습니다.
  • cutececil
    cutececil

    덧셈을 하셔서 그렇습니다.입력이 커질 경우 int 오버플로우가 일어납니다.
    라고 그분이?(원작자) 다른 글에 다신 댓글이 있습니다.
    자바가 int는 || 연산이 안되어서 아마 + 를 쓰신듯한데요. 그냥 | 를 쓰셔도 될 듯해요. 어차피 0 | 1 이나 1 | 1 이나 0 | 0 의 경우밖에 없으므로..


    7년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.