-
Notifications
You must be signed in to change notification settings - Fork 0
/
길찾기게임.cpp
97 lines (88 loc) · 1.66 KB
/
길찾기게임.cpp
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
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
int num;
int x;
int y;
node *left;
node *right;
void init(int num, int x, int y)
{
this->num = num;
this->x = x;
this->y = y;
this->left = NULL;
this->right = NULL;
}
};
int cmp(node a, node b)
{
if (a.y != b.y)
return a.y > b.y;
return a.x < b.x;
}
void add(node *parent, node *child)
{
if (parent->x < child->x)
{
if (parent->right)
add(parent->right, child);
else
parent->right = child;
}
else
{
if (parent->left)
add(parent->left, child);
else
parent->left = child;
}
}
void preorder(node *tmp, vector<int> &v)
{
v.push_back(tmp->num);
if (tmp->left)
{
preorder(tmp->left, v);
}
if (tmp->right)
{
preorder(tmp->right, v);
}
}
void postorder(node *tmp, vector<int> &v)
{
if (tmp->left)
{
postorder(tmp->left, v);
}
if (tmp->right)
{
postorder(tmp->right, v);
}
v.push_back(tmp->num);
}
vector<vector<int>> solution(vector<vector<int>> nodeinfo)
{
vector<vector<int>> answer(2);
vector<node> tree;
node n, *head;
int i;
for (i = 0; i < nodeinfo.size(); i++)
{
n.init(i + 1, nodeinfo[i][0], nodeinfo[i][1]);
tree.push_back(n);
}
sort(tree.begin(), tree.end(), cmp);
head = &tree.front();
for (i = 1; i < tree.size(); i++)
{
add(head, &tree[i]);
}
preorder(head, answer[0]);
postorder(head, answer[1]);
return answer;
}