Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
126 lines (104 loc) · 3.52 KB

File metadata and controls

126 lines (104 loc) · 3.52 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
class Node {
private int time;
private char direction;
public Node(int time, char direction) {
this.time = time;
this.direction = direction;
}
public int getTime() {
return this.time;
}
public char getDirection() {
return this.direction;
}
}
class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}
public class Main {
public static int n, k, l;
public static int[][] arr = new int[101][101]; // 맵 정보
public static ArrayList<Node> info = new ArrayList<>(); // 방향 회전 정보
// 처음에는 오른쪽을 보고 있으므로(동, 남, 서, 북)
public static int dx[] = {0, 1, 0, -1};
public static int dy[] = {1, 0, -1, 0};
public static int turn(int direction, char c) {
if (c == 'L') direction = (direction == 0)? 3 : direction - 1;
else direction = (direction + 1) % 4;
return direction;
}
public static int simulate() {
int x = 1, y = 1; // 뱀의 머리 위치
arr[x][y] = 2; // 뱀이 존재하는 위치는 2로 표시
int direction = 0; // 처음에는 동쪽을 보고 있음
int time = 0; // 시작한 뒤에 지난 '초' 시간
int index = 0; // 다음에 회전할 정보
// 뱀이 차지하고 있는 위치 정보(꼬리가 앞쪽)
Queue<Position> q = new LinkedList<>();
q.offer(new Position(x, y));
while (true) {
int nx = x + dx[direction];
int ny = y + dy[direction];
// 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && arr[nx][ny] != 2) {
// 사과가 없다면 이동 후에 꼬리 제거
if (arr[nx][ny] == 0) {
arr[nx][ny] = 2;
q.offer(new Position(nx, ny));
Position prev = q.poll();
arr[prev.getX()][prev.getY()] = 0;
}
// 사과가 있다면 이동 후에 꼬리 그대로 두기
if (arr[nx][ny] == 1) {
arr[nx][ny] = 2;
q.offer(new Position(nx, ny));
}
}
// 벽이나 뱀의 몸통과 부딪혔다면
else {
time += 1;
break;
}
// 다음 위치로 머리를 이동
x = nx;
y = ny;
time += 1;
if (index < l && time == info.get(index).getTime()) { // 회전할 시간인 경우 회전
direction = turn(direction, info.get(index).getDirection());
index += 1;
}
}
return time;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
// 맵 정보(사과 있는 곳은 1로 표시)
for (int i = 0; i < k; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
arr[a][b] = 1;
}
// 방향 회전 정보 입력
l = sc.nextInt();
for (int i = 0; i < l; i++) {
int x = sc.nextInt();
char c = sc.next().charAt(0);
info.add(new Node(x, c));
}
System.out.println(simulate());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.