Skip to content

Commit 4b4b7f6

Browse files
authored
Merge pull request #902 from AlgorithmWithGod/0224LJH
[20250916] BOJ / P4 / 개구리 공주 / 이종환
2 parents 0542c75 + 1f29629 commit 4b4b7f6

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
```java
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class Main {
9+
10+
11+
static HashMap<Integer,TreeSet<Plant>> sumMap = new HashMap<>(); // X+Y가 같은 애들끼리. 여기선 차이가 줄 기준
12+
static HashMap<Integer,TreeSet<Plant>> diffMap = new HashMap<>();// X-Y가 같은 애들끼리. 여기선 합이 클수록 뒤.
13+
static Plant[] plants;
14+
static int[] dy = {1,-1,1,-1};
15+
static int[] dx = {1,1,-1,-1};
16+
static int[] jumpDir; // 한글자 받아서 -'A'로 int로 바꿔서 저장
17+
18+
static int plantCnt, jumpCnt,ansX,ansY;
19+
20+
static final int DIFF_PLUS = 0;
21+
static final int SUM_PLUS = 1;
22+
static final int SUM_MINUS = 2;
23+
static final int DIFF_MINUS = 3;
24+
25+
static class Plant{
26+
int x,y,diff,sum;
27+
28+
public Plant(int x, int y) {
29+
this.x = x;
30+
this.y = y;
31+
this.diff = x-y;
32+
this.sum = x+y;
33+
}
34+
35+
public void remove() {
36+
sumMap.get(sum).remove(this);
37+
diffMap.get(diff).remove(this);
38+
}
39+
}
40+
41+
public static void main(String[] args) throws NumberFormatException, IOException {
42+
init();
43+
process();
44+
print();
45+
}
46+
47+
public static void init() throws NumberFormatException, IOException {
48+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
49+
StringTokenizer st = new StringTokenizer(br.readLine());
50+
plantCnt = Integer.parseInt(st.nextToken());
51+
jumpCnt = Integer.parseInt(st.nextToken());
52+
ansX = -1;
53+
ansY = -1;
54+
55+
plants = new Plant[plantCnt];
56+
jumpDir = new int[jumpCnt];
57+
58+
String input = br.readLine();
59+
for (int i = 0; i < jumpCnt; i++) {
60+
jumpDir[i] = input.charAt(i) - 'A';
61+
}
62+
63+
for (int i = 0; i < plantCnt; i++) {
64+
st = new StringTokenizer(br.readLine());
65+
int x = Integer.parseInt(st.nextToken());
66+
int y = Integer.parseInt(st.nextToken());
67+
68+
plants[i] = new Plant(x,y);
69+
70+
71+
72+
}
73+
74+
75+
}
76+
77+
public static void process() {
78+
for (int i = 0; i < plantCnt; i++) {
79+
Plant p = plants[i];
80+
addToMap(p);
81+
}
82+
83+
Plant cur = plants[0];
84+
85+
for (int i = 0; i< jumpCnt; i++) {
86+
int dir = jumpDir[i];
87+
88+
TreeSet<Plant> sumTree = sumMap.get(cur.sum);
89+
TreeSet<Plant> diffTree = diffMap.get(cur.diff);
90+
Plant temp = null;
91+
92+
switch(dir) {
93+
case(SUM_PLUS):
94+
temp = sumTree.higher(cur);
95+
break;
96+
case(SUM_MINUS):
97+
temp = sumTree.lower(cur);
98+
break;
99+
case(DIFF_PLUS):
100+
temp = diffTree.higher(cur);
101+
break;
102+
case(DIFF_MINUS):
103+
temp = diffTree.lower(cur);
104+
break;
105+
}
106+
107+
if (temp == null) continue;
108+
109+
cur.remove();
110+
cur = temp;
111+
112+
}
113+
114+
ansX = cur.x;
115+
ansY = cur.y;
116+
117+
}
118+
119+
120+
private static void addToMap(Plant p) {
121+
if (!sumMap.containsKey(p.sum)) {
122+
sumMap.put(p.sum, new TreeSet<Plant>((p1,p2) -> p1.diff-p2.diff));
123+
}
124+
sumMap.get(p.sum).add(p);
125+
126+
127+
if (!diffMap.containsKey(p.diff)) {
128+
diffMap.put(p.diff, new TreeSet<Plant>((p1,p2) -> p1.sum-p2.sum));
129+
}
130+
diffMap.get(p.diff).add(p);
131+
132+
}
133+
134+
public static void print() {
135+
System.out.println(ansX + " " + ansY);
136+
}
137+
}
138+
```

0 commit comments

Comments
 (0)