-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgrammers_340211.java
More file actions
69 lines (57 loc) · 2.12 KB
/
Copy pathProgrammers_340211.java
File metadata and controls
69 lines (57 loc) · 2.12 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
package programmers.lv2;
import java.util.*;
public class Programmers_340211 {
public static void main(String[] args) {
int[][] points = { { 3, 2 }, { 6, 4 }, { 4, 7 }, { 1, 4 } };
int[][] routes = { { 4, 2 }, { 1, 3 }, { 4, 2 }, { 4, 3 } };
Programmers_340211 test = new Programmers_340211();
int result = test.solution(points, routes);
System.out.println("result: " + result);
}
public int solution(int[][] points, int[][] routes) {
int answer = 0;
Map<Integer, int[]> pointMap = new HashMap<>();
for (int i = 0; i < points.length; i++) {
pointMap.put(i + 1, points[i]);
}
Map<Integer, Map<String, Integer>> timeMap = new HashMap<>();
for (int[] route : routes) {
int t = 0;
int[] start = pointMap.get(route[0]);
int r = start[0];
int c = start[1];
recordMap(t, timeMap, r, c);
for (int i = 1; i < route.length; i++) {
int[] target = pointMap.get(route[i]);
int tr = target[0];
int tc = target[1];
while (r != tr) {
t++;
r += (tr > r ? 1 : -1);
recordMap(t, timeMap, r, c);
}
while (c != tc) {
t++;
c += (tc > c ? 1 : -1);
recordMap(t, timeMap, r, c);
}
}
}
for (Map<String, Integer> position : timeMap.values()) {
// System.out.println(position);
for (Integer count : position.values()) {
if (count > 1) {
answer++;
}
}
}
return answer;
}
// 시간별로 로봇 위치 및 만난 횟수 저장
public void recordMap(int t, Map<Integer, Map<String, Integer>> timeMap, int r, int c) {
timeMap.putIfAbsent(t, new HashMap<>());
Map<String, Integer> positions = timeMap.get(t);
String key = r + "," + c;
positions.put(key, positions.getOrDefault(key, 0) + 1);
}
}