Skip to content

Commit d8ee862

Browse files
authored
[20251114] BOJ / P4 / 구슬 발사기 / 권혁준
1 parent fb95b90 commit d8ee862

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
using ll = long long;
5+
6+
const ll INF = 1e18;
7+
int N, S, E;
8+
map<int, vector<tuple<int, int, ll>>> mp[8]{};
9+
vector<vector<pair<int, ll>>> v(100001);
10+
vector<ll> dist(100001, INF);
11+
int par[100001]{};
12+
13+
int main() {
14+
cin.tie(0)->sync_with_stdio(0);
15+
16+
cin>>N>>S>>E;
17+
for(int i=1;i<=N;i++) {
18+
int x,y,c;
19+
string d;
20+
cin>>x>>y>>c>>d;
21+
22+
int dir = -1;
23+
if(d == "N") dir = 0;
24+
if(d == "NE") dir = 1;
25+
if(d == "E") dir = 2;
26+
if(d == "SE") dir = 3;
27+
if(d == "S") dir = 4;
28+
if(d == "SW") dir = 5;
29+
if(d == "W") dir = 6;
30+
if(d == "NW") dir = 7;
31+
32+
int cost = 0;
33+
for(int j=dir;j<dir+8;j++) {
34+
int k = j%8;
35+
if(k == 0) {
36+
mp[k][x].emplace_back(y,i,cost);
37+
}
38+
else if(k == 1) {
39+
mp[k][y-x].emplace_back(x,i,cost);
40+
}
41+
else if(k == 2) {
42+
mp[k][y].emplace_back(x,i,cost);
43+
}
44+
else if(k == 3) {
45+
mp[k][y+x].emplace_back(x,i,cost);
46+
}
47+
else if(k == 4) {
48+
mp[k][x].emplace_back(y,i,cost);
49+
}
50+
else if(k == 5) {
51+
mp[k][y-x].emplace_back(x,i,cost);
52+
}
53+
else if(k == 6) {
54+
mp[k][y].emplace_back(x,i,cost);
55+
}
56+
else {
57+
mp[k][y+x].emplace_back(x,i,cost);
58+
}
59+
cost += c;
60+
}
61+
62+
}
63+
64+
for(int i=0;i<8;i++) {
65+
for(auto [a,b] : mp[i]) {
66+
if(i<4) sort(b.begin(), b.end());
67+
else sort(b.begin(), b.end(), greater<>());
68+
69+
for(int j=1;j<b.size();j++) {
70+
auto [p,n1,c1] = b[j-1];
71+
auto [q,n2,c2] = b[j];
72+
v[n1].emplace_back(n2,c1);
73+
}
74+
}
75+
}
76+
77+
dist[S] = 0;
78+
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> q;
79+
q.emplace(0,S);
80+
while(!q.empty()) {
81+
auto [d,n] = q.top(); q.pop();
82+
if(d > dist[n]) continue;
83+
if(n == E) {
84+
cout<<d<<'\n';
85+
stack<int> st;
86+
while(n) {
87+
st.push(n);
88+
n = par[n];
89+
}
90+
while(!st.empty()) {
91+
cout<<st.top()<<' ';
92+
st.pop();
93+
}
94+
return 0;
95+
}
96+
for(auto [i,c]:v[n]) if(dist[i] > d+c) {
97+
dist[i] = d+c;
98+
par[i] = n;
99+
q.emplace(dist[i], i);
100+
}
101+
}
102+
cout<<-1;
103+
104+
}
105+
```

0 commit comments

Comments
 (0)