-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathYJ_340211.java
More file actions
107 lines (95 loc) Β· 3.01 KB
/
YJ_340211.java
File metadata and controls
107 lines (95 loc) Β· 3.01 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
import java.util.*;
// ꡬν λ¬Έμ
// FIXME: ν
μ€νΈμΌμ΄μ€ μ 체λ ν΅κ³Ό λͺ»ν΄μ μ€λ₯ μμ μ€μ
λλ€...
public class YJ_340211 {
class Robot {
List<Pos> route;
int start;
int end;
public Robot(int start, int end){
this.route = new ArrayList<>();
this.start = start;
this.end = end;
}
}
class Pos {
int x;
int y;
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if(obj == null || getClass() != obj.getClass()){
return false;
}
if(this == obj){
return true;
}
Pos pos = (Pos) obj;
return this.x == pos.x && this.y == pos.y;
}
@Override
public int hashCode() {
return Objects.hash(x,y);
}
}
List<Robot> robotList = new ArrayList<>();
public int solution(int[][] points, int[][] routes) {
//μ΄κΈ°ν
for (int i = 0; i < routes.length; i++) {
int start = routes[i][0] - 1;
int end = routes[i][1] - 1;
robotList.add(new Robot(start, end));
//λ‘λ΄μ μμμμΉ μ μ₯
int[] point = points[start];
robotList.get(i).route.add(new Pos(point[0], point[1]));
}
//κ° λ‘λ΄λ€μ λͺ¨λ μ΄λκ²½λ‘ μ μ₯
int longestRoute = 0;
for(Robot robot : robotList){
saveRoute(points, robot);
longestRoute = Math.max(longestRoute, robot.route.size());
}
//κ²½λ‘ μΆ©λ μ°ΎκΈ°
return findCollision(longestRoute);
}
//λͺ¨λ λ‘λ΄μ μ΄ λ§λ€ κ²½λ‘λ₯Ό μ μ₯
void saveRoute(int[][] points, Robot robot){
int[] startPoint = points[robot.start];
int[] endPoint = points[robot.end];
//x μ’ν λ¨Όμ μ΄λ
int x = startPoint[0];
while(x != endPoint[0]){
x = x < endPoint[0] ? x+1 : x-1;
robot.route.add(new Pos(x,startPoint[1]));
}
//y μ’ν μ΄λ
int y = startPoint[1];
while(y != endPoint[1]){
y = y < endPoint[1] ? y+1 : y-1;
robot.route.add(new Pos(x,y));
}
}
int findCollision(int longestRoute){
int collision = 0;
for(int i=0; i<longestRoute; i++){
Map<Pos, Integer> map = new HashMap<>();
//i μ΄λμ μμ§μμ λ λͺ¨λ λ‘λ΄λ€μ μΆ©λ μΉ΄μ΄ν
for(Robot robot : robotList){
Pos pos = robot.route.size() > i ? robot.route.get(i) : null;
if(Objects.isNull(pos)){
continue;
}
map.put(pos, map.getOrDefault(pos, 0) + 1);
}
for(Integer count : map.values()){
if(count > 1){
collision++;
}
}
}
return collision;
}
}