pji3016   3년 전

테스트 케이스는 전부 맞습니다.

런타임에러 해결방법을 모르겠습니다.ㅠㅠ 도와주세요.

import java.util.Scanner;

public class _7_10875_Sneak {


static int L;
static int N;
static int[][] map;
static int[][] isvisited;
static int[] second;
static char[] direct;

public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      L = scan.nextInt();
      map = new int[2*L+1][2*L+1];
      isvisited = new int[2*L+1][2*L+1];

      N = scan.nextInt();
      second = new int[N];
      direct = new char[N];

      for(int i=0; i<N; i++){
            if(i == 0) second[i] = scan.nextInt();
            else second[i] = scan.nextInt() + second[i-1];

            direct[i] = scan.next().charAt(0);
      }

      int start_x = L; // 시작점
      int start_y = L; // 시작점
      int direction = 3; // 초기 방향 - 오른쪽
      

      solve(start_x, start_y, direction);
      System.out.println(time + 1);
}

/*             L  R
왼쪽(1)     4  2
위(2)      1  3
오른쪽(3)   2  4
아래(4)    3  1 
*/

static int counter = 0; // 0 ~ N-1
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
static int time = 0;

private static void solve(int start_x, int start_y, int direction){
      isvisited[start_x][start_y] = 1;

      if(time != second[counter]){
            start_x += dx[direction-1];
            start_y += dy[direction-1];

            if(start_x<0 || start_y<0 || start_x>2*L || start_y>2*L) return;
            if(isvisited[start_x][start_y] == 1) return;

            time += 1;
            solve(start_x, start_y, direction);
      }else if(time == second[counter]){
            if(direction == 1){
                  direction = direct[counter] == 'L' ? 4 : 2;
            }else if(direction == 2){
                  direction = direct[counter] == 'L' ? 1 : 3;
            }else if(direction == 3){
                  direction = direct[counter] == 'L' ? 2 : 4;
            }else if(direction == 4){
                  direction = direct[counter] == 'L' ? 3 : 1;
            }

            start_x += dx[direction-1];
            start_y += dy[direction-1];

            if(start_x<0 || start_y<0 || start_x>2*L || start_y>2*L) return;
            if(isvisited[start_x][start_y] == 1) return;

            if(counter<N) counter += 1;
            time += 1;
            solve(start_x, start_y, direction);
      }
}


}

댓글을 작성하려면 로그인해야 합니다.