Skip to content

Commit 6a5986f

Browse files
authored
Merge pull request #920 from AlgorithmWithGod/0224LJH
[20250918] BOJ / P5 / 뱀 / 이종환
2 parents d90908d + 5dfe72c commit 6a5986f

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

0224LJH/202509/18 BOJ 뱀.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
public class Main {
8+
9+
static final char LEFT = 'L'; // 감소
10+
static final char RIGHT = 'R'; // 증가
11+
static ArrayList<Line> lines = new ArrayList<>();
12+
13+
static int[] dy = {0,-1,0,1};
14+
static int[] dx = {1,0,-1,0};
15+
16+
static int maxLimit,minLimit,turnCnt,curDir,curX,curY;
17+
static long curTime;
18+
static Turn[] turns;
19+
20+
static class Turn{
21+
int after;
22+
char dir;
23+
24+
public Turn(int after, String dir) {
25+
this.after = after;
26+
this.dir = dir.charAt(0);
27+
}
28+
}
29+
30+
static class Line{
31+
int stX, stY, endX, endY;
32+
boolean isVertical;
33+
34+
public Line(int stX, int stY, int endX, int endY) {
35+
36+
isVertical = (stX == endX);
37+
if (isVertical) {
38+
this.stX = this.endX = stX;
39+
this.stY = Math.min(stY, endY);
40+
this.endY = Math.max(stY, endY);
41+
} else {
42+
this.stY = this.endY = stY;
43+
this.stX = Math.min(stX, endX);
44+
this.endX = Math.max(stX, endX);
45+
}
46+
}
47+
48+
public boolean check(Line l) {
49+
// 둘이 크로스되는지, 아니면 평행한지 부터 판단
50+
if (this.isVertical && l.isVertical) {
51+
if (this.stX != l.stX) return true;
52+
if (this.stY > l.endY || this.endY < l.stY) return true;
53+
return false;
54+
} else if (!this.isVertical && !l.isVertical){
55+
if (this.stY != l.stY) return true;
56+
if (this.stX > l.endX || this.endX < l.stX) return true;
57+
return false;
58+
}else {
59+
if (!this.isVertical) {
60+
if((this.stY >= l.stY && this.stY <= l.endY)&&
61+
(l.stX >= this.stX && l.stX <= this.endX))return false;
62+
return true;
63+
} else {
64+
if((l.stY >= this.stY && l.stY <= this.endY)&&
65+
(this.stX >= l.stX && this.stX <= l.endX))return false;
66+
return true;
67+
}
68+
}
69+
70+
}
71+
}
72+
73+
74+
75+
76+
77+
public static void main(String[] args) throws NumberFormatException, IOException {
78+
init();
79+
process();
80+
print();
81+
}
82+
83+
public static void init() throws NumberFormatException, IOException {
84+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
85+
maxLimit = Integer.parseInt(br.readLine());
86+
minLimit = maxLimit*(-1);
87+
turnCnt = Integer.parseInt(br.readLine());
88+
turns = new Turn[turnCnt+1];
89+
curDir = 0;
90+
curTime = 0;
91+
curY = 0;
92+
curX = 0;
93+
94+
for (int i = 0; i < turnCnt; i++) {
95+
StringTokenizer st = new StringTokenizer(br.readLine());
96+
int after = Integer.parseInt(st.nextToken());
97+
String dir = st.nextToken();
98+
turns[i] = new Turn(after,dir);
99+
}
100+
101+
102+
103+
104+
105+
}
106+
107+
public static void process() {
108+
lines.add(new Line(minLimit, minLimit-1, maxLimit, minLimit-1));
109+
lines.add(new Line(minLimit-1, minLimit, minLimit-1, maxLimit));
110+
lines.add(new Line(maxLimit+1, minLimit, maxLimit+1, maxLimit));
111+
lines.add(new Line(minLimit, maxLimit+1, maxLimit, maxLimit+1));
112+
113+
turns[turnCnt] = new Turn(1000000000,"L");
114+
115+
116+
for(int i = 0 ; i <= turnCnt; i++) {
117+
Turn t = turns[i];
118+
int nX = curX + dx[curDir] * t.after;
119+
int nY = curY + dy[curDir] * t.after;
120+
int minTime = Integer.MAX_VALUE;
121+
boolean isOkay = true;
122+
123+
124+
Line nLine = new Line(curX, curY, nX, nY);
125+
126+
for (Line l: lines) {
127+
128+
if(!l.check(nLine)) {
129+
int tempMin = Integer.MAX_VALUE;
130+
131+
if(curDir == 0) tempMin = Math.min(tempMin, l.stX - curX);
132+
if(curDir == 1) tempMin = Math.min(tempMin, curY - l.endY);
133+
if(curDir == 2) tempMin = Math.min(tempMin, curX - l.endX);
134+
if(curDir == 3) tempMin = Math.min(tempMin, l.stY - curY);
135+
136+
137+
if (tempMin == 0) continue;
138+
isOkay = false;
139+
minTime = Math.min(minTime, tempMin);
140+
}
141+
}
142+
143+
lines.add(nLine);
144+
145+
146+
if (isOkay) {
147+
curTime += t.after;
148+
if (t.dir == LEFT) curDir = (curDir +3)%4;
149+
else curDir = (curDir+1)%4;
150+
151+
curX = nX;
152+
curY = nY;
153+
} else {
154+
curTime += minTime;
155+
return;
156+
}
157+
158+
}
159+
160+
}
161+
162+
163+
164+
165+
public static void print() {
166+
System.out.println(curTime);
167+
}
168+
}

0 commit comments

Comments
 (0)