BOJ
BOJ 14169 Lasers and Mirrors
bsgreentea
2019. 7. 10. 22:05
좌표압축 후 x,y 각각에 대해 좌표값을 넣어 인접리스트를 만든 후 BFS를 실행해주었다.
(x좌표 기준 y축과 평행한 직선을 긋고, 그와 만나는 지점들을 리스트로 저장, y축 기준도 마찬가지)
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
108
109
110
111
112
113
114
115
|
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
// x,y 좌표값과 방향, 거울의 수를 저장
typedef struct {
int x, y, dir, cnt;
}st;
int n, xl, yl, xb, yb;
vector<pair<int, int>> point;
vector<int> x, y;
// 좌표압축 이후의 인덱스를 활용한 상하좌우를 잇는 인접그래프
vector<vector<int>> vt_x, vt_y;
// 방문한 점인지, 그리고 존재하는 점인지 체크
set<pair<int, int>> disc, temp;
int get_idx(vector<int>& vt, int num) {
return lower_bound(vt.begin(), vt.end(), num) - vt.begin();
}
int main() {
scanf("%d %d %d %d %d", &n, &xl, &yl, &xb, &yb);
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
point.push_back({ a,b });
temp.insert({ a,b });
x.push_back(a);
y.push_back(b);
}
if (xl == xb || yl == yb) {
puts("0"); return 0;
}
point.push_back({ xb,yb });
temp.insert({ xb,yb });
x.push_back(xb);
y.push_back(yb);
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
sort(y.begin(), y.end());
y.erase(unique(y.begin(), y.end()), y.end());
vt_x.resize(x.size());
vt_y.resize(y.size());
// 인덱스에 좌표값을 달아줌으로써 인접 리스트 생성
// x좌표 기준과 y좌표 기준 모두 만들어준다.
for (int i = 0; i < n; i++) {
a = point[i].first;
b = point[i].second;
vt_x[get_idx(x, a)].push_back(b);
vt_y[get_idx(y, b)].push_back(a);
}
vt_x[get_idx(x, xb)].push_back(yb);
vt_y[get_idx(y, yb)].push_back(xb);
queue<st> qu;
disc.insert({ xl,yl });
int res = 0, flag = 0;
while (qu.size()) {
int hx = qu.front().x;
int hy = qu.front().y;
int dir = qu.front().dir;
int cnt = qu.front().cnt;
qu.pop();
if (hx == xb && hy == yb) {
flag = 1; res = cnt; break;
}
int idx_x = get_idx(x, hx);
int idx_y = get_idx(y, hy);
int nx, ny;
if (dir == 1) {
for (int i = 0; i < vt_x[idx_x].size(); i++) {
ny = vt_x[idx_x][i];
// 방문했거나 없는 좌표라면 건너뛴다
disc.insert({ hx,ny });
}
}
else {
for (int i = 0; i < vt_y[idx_y].size(); i++) {
nx = vt_y[idx_y][i];
disc.insert({ nx,hy });
}
}
}
if (flag)
printf("%d\n", res);
else puts("-1");
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|