-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_42892.java
More file actions
86 lines (68 loc) · 1.7 KB
/
DH_42892.java
File metadata and controls
86 lines (68 loc) · 1.7 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
import java.util.*;
/*
* 길 찾기 게임
*/
public class DH_42892 {
static class Node {
int v, x, y;
Node l, r;
public Node(int v, int x, int y) {
this.v = v;
this.x = x;
this.y = y;
}
}
static Node[] nodes;
static int idx;
static int[][] solution(int[][] nodeInfo) {
nodes = new Node[nodeInfo.length];
for(int i = 0; i < nodeInfo.length; i++) {
nodes[i] = new Node(i + 1, nodeInfo[i][0], nodeInfo[i][1]);
}
// 우선순위에 따라 정렬
Arrays.sort(nodes, (o1, o2)-> {
if(o1.y != o2.y) return -1 * Integer.compare(o1.y, o2.y);
return Integer.compare(o1.x, o2.x);
});
Node root = nodes[0];
// 트리 만들기
for(int i = 1; i < nodes.length; i++) {
Node current = nodes[i];
makeTree(root, current);
}
int[][] answer = new int[2][nodeInfo.length];
preOrder(root, answer);
idx = 0;
postOrder(root, answer);
return answer;
}
// 전위순회
static void preOrder(Node p, int[][] answer) {
if(p == null) return;
answer[0][idx++] = p.v;
preOrder(p.l, answer);
preOrder(p.r, answer);
}
// 후외순회
static void postOrder(Node p, int[][] answer) {
if(p == null) return;
postOrder(p.l, answer);
postOrder(p.r, answer);
answer[1][idx++] = p.v;
}
// [1]: x 좌표, [2]: y좌표
static void makeTree(Node p, Node current) {
if(p.x < current.x) {
if(p.r == null) p.r = current;
else makeTree(p.r, current);
}
else {
if(p.l == null) p.l = current;
else makeTree(p.l, current);
}
}
public static void main(String[] args) throws Exception {
int[][] nodeInfo = {{5, 3}, {11, 5}, {13, 3}, {3, 5}, {6, 1}, {1, 3}, {8, 6}, {7, 2}, {2, 2}};
System.out.println(solution(nodeInfo));
}
}