-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3190.java
More file actions
89 lines (81 loc) · 2.58 KB
/
Copy path3190.java
File metadata and controls
89 lines (81 loc) · 2.58 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
// https://www.acmicpc.net/problem/3190
// 뱀
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, k, appleX[], appleY[], l, sec, dir, answer;
static final int[] DIRX = {-1, 0, 1, 0}, DIRY = {0, 1, 0, -1};
static List<Integer> X, Y;
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(scan.readLine());
k = Integer.parseInt(scan.readLine());
appleX = new int[k];
appleY = new int[k];
for(int i=0; i<k; i++){
StringTokenizer st = new StringTokenizer(scan.readLine());
appleX[i] = Integer.parseInt(st.nextToken()) - 1;
appleY[i] = Integer.parseInt(st.nextToken()) - 1;
}
answer = -1;
sec = 0;
dir =1;
X = new LinkedList<>();
Y = new LinkedList<>();
push(0, 0);
l = Integer.parseInt(scan.readLine());
for(int i=0; i<l; i++){
StringTokenizer st = new StringTokenizer(scan.readLine());
int next = Integer.parseInt(st.nextToken());
if(!go(next - sec)){
System.out.println(answer);
return;
}
sec = next;
switch(st.nextToken()){
case "L": if(dir == 0) dir = 3; else dir--; break;
case "D": if(dir == 3) dir = 0; else dir++; break;
}
}
if(!go(n))
System.out.println(answer);
}
public static boolean go(int n){
for(int i=0; i<n; i++){
int x = X.get(0)+DIRX[dir], y = Y.get(0)+DIRY[dir];
if(!check(x, y)){
answer = sec + i + 1;
return false;
}
push(x, y);
if(!checkApple(x, y))
pop();
}
return true;
}
public static boolean check(int x, int y){
if(x < 0 || n <= x || y < 0 || n <= y)
return false;
for(int i=0; i<X.size(); i++){
if(X.get(i) == x && Y.get(i) == y)
return false;
}
return true;
}
public static boolean checkApple(int x, int y){
for(int i=0; i<k; i++){
if(appleX[i] == x && appleY[i] == y){
appleX[i] = -1; appleY[i] = -1;
return true;
}
}
return false;
}
public static void push(int x, int y){
X.add(0, x); Y.add(0, y);
}
public static void pop(){
X.remove(X.size()-1); Y.remove(Y.size()-1);
}
}