forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.java
More file actions
126 lines (104 loc) Β· 3.52 KB
/
5.java
File metadata and controls
126 lines (104 loc) Β· 3.52 KB
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());
}
}