1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ public static int n , m , x , y , direction ;
6
+ // 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
7
+ public static int [][] d = new int [50 ][50 ];
8
+ // 전체 맵 정보
9
+ public static int [][] arr = new int [50 ][50 ];
10
+
11
+ // 북, 동, 남, 서 방향 정의
12
+ public static int dx [] = {-1 , 0 , 1 , 0 };
13
+ public static int dy [] = {0 , 1 , 0 , -1 };
14
+
15
+ // 왼쪽으로 회전
16
+ public static void turn_left () {
17
+ direction -= 1 ;
18
+ if (direction == -1 ) direction = 3 ;
19
+ }
20
+
21
+ public static void main (String [] args ) {
22
+ Scanner sc = new Scanner (System .in );
23
+
24
+ // N, M을 공백을 기준으로 구분하여 입력받기
25
+ n = sc .nextInt ();
26
+ m = sc .nextInt ();
27
+
28
+ // 현재 캐릭터의 X 좌표, Y 좌표, 방향을 입력받기
29
+ x = sc .nextInt ();
30
+ y = sc .nextInt ();
31
+ direction = sc .nextInt ();
32
+ d [x ][y ] = 1 ; // 현재 좌표 방문 처리
33
+
34
+ // 전체 맵 정보를 입력 받기
35
+ for (int i = 0 ; i < n ; i ++) {
36
+ for (int j = 0 ; j < m ; j ++) {
37
+ arr [i ][j ] = sc .nextInt ();
38
+ }
39
+ }
40
+
41
+ // 시뮬레이션 시작
42
+ int cnt = 1 ;
43
+ int turn_time = 0 ;
44
+ while (true ) {
45
+ // 왼쪽으로 회전
46
+ turn_left ();
47
+ int nx = x + dx [direction ];
48
+ int ny = y + dy [direction ];
49
+ // 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
50
+ if (d [nx ][ny ] == 0 && arr [nx ][ny ] == 0 ) {
51
+ d [nx ][ny ] = 1 ;
52
+ x = nx ;
53
+ y = ny ;
54
+ cnt += 1 ;
55
+ turn_time = 0 ;
56
+ continue ;
57
+ }
58
+ // 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
59
+ else turn_time += 1 ;
60
+ // 네 방향 모두 갈 수 없는 경우
61
+ if (turn_time == 4 ) {
62
+ nx = x - dx [direction ];
63
+ ny = y - dy [direction ];
64
+ // 뒤로 갈 수 있다면 이동하기
65
+ if (arr [nx ][ny ] == 0 ) {
66
+ x = nx ;
67
+ y = ny ;
68
+ }
69
+ // 뒤가 바다로 막혀있는 경우
70
+ else break ;
71
+ turn_time = 0 ;
72
+ }
73
+ }
74
+
75
+ System .out .println (cnt );
76
+ }
77
+
78
+ }
0 commit comments