/* be name khoda */
// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <queue>
#include <map>
using namespace std;
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif
typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))
#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl
const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD
// -----------------------------------------------------------------------
const ll maxn = 1005;
const ll maxnlg = 20;
ll adj4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
ll n, m, A, B;
bool block[maxn][maxn];
vpii g[maxn * maxn];
priority_queue<pii, vpii, greater<pii>> pq;
bool vis[maxn * maxn];
ll d[maxn * maxn], near[maxn * maxn], q[maxn * maxn];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// ifstream cin("portals.in");
cin >> n >> m;
fori (i, n) {
string s; cin >> s;
fori (j, m) {
if (s[j] == '#') block[i][j] = 1;
else if (s[j] == 'S') A = i * m + j;
else if (s[j] == 'C') B = i * m + j;
}
}
ll l = 0, r = 0;
fori (i, n) {
fori (j, m) {
bool flag = 1;
fori (k, 4) {
ll ii = i + adj4[k][0], jj = j + adj4[k][1];
if (0 <= ii && ii < n && 0 <= jj && jj < m && !block[ii][jj]) g[i * m + j].eb(ii * m + jj, 1);
else if (flag) {
flag = 0;
near[i * m + j] = 1;
q[r++] = i * m + j;
}
}
}
}
while (r > l) {
ll x = q[l++];
for (auto [y, w] : g[x]) {
if (!near[y]) {
near[y] = near[x] + 1;
q[r++] = y;
}
}
}
fori (i, n) {
ll lst = 0;
fori (j, m) {
if (block[i][j]) lst = j + 1;
else g[i * m + j].eb(i * m + lst, near[i * m + j]);
}
lst = m - 1;
forir (j, m) {
if (block[i][j]) lst = j - 1;
else g[i * m + j].eb(i * m + lst, near[i * m + j]);
}
}
fori (j, m) {
ll lst = 0;
fori (i, n) {
if (block[i][j]) lst = i + 1;
else g[i * m + j].eb(lst * m + j, near[i * m + j]);
}
lst = n - 1;
forir (i, n) {
if (block[i][j]) lst = i - 1;
else g[i * m + j].eb(lst * m + j, near[i * m + j]);
}
}
memset(d, 60, sizeof d);
d[A] = 0;
pq.push({d[A], A});
while (!pq.empty()) {
ll x = pq.top().S;
pq.pop();
if (vis[x]) continue;
vis[x] = 1;
for (auto [y, w] : g[x]) {
if (d[y] > d[x] + w) {
d[y] = d[x] + w;
pq.push({d[y], y});
}
}
}
cout << d[B] << '\n';
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:100:24: warning: unused variable 'w' [-Wunused-variable]
for (auto [y, w] : g[x]) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28032 KB |
Output is correct |
2 |
Correct |
21 ms |
28032 KB |
Output is correct |
3 |
Correct |
20 ms |
28032 KB |
Output is correct |
4 |
Correct |
20 ms |
28032 KB |
Output is correct |
5 |
Correct |
21 ms |
28032 KB |
Output is correct |
6 |
Correct |
20 ms |
28032 KB |
Output is correct |
7 |
Correct |
21 ms |
28032 KB |
Output is correct |
8 |
Correct |
20 ms |
28032 KB |
Output is correct |
9 |
Correct |
20 ms |
28032 KB |
Output is correct |
10 |
Correct |
21 ms |
28032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28032 KB |
Output is correct |
2 |
Correct |
22 ms |
28172 KB |
Output is correct |
3 |
Correct |
20 ms |
28032 KB |
Output is correct |
4 |
Correct |
20 ms |
28032 KB |
Output is correct |
5 |
Correct |
25 ms |
28032 KB |
Output is correct |
6 |
Correct |
21 ms |
28032 KB |
Output is correct |
7 |
Correct |
21 ms |
28032 KB |
Output is correct |
8 |
Correct |
21 ms |
28032 KB |
Output is correct |
9 |
Correct |
21 ms |
28288 KB |
Output is correct |
10 |
Correct |
24 ms |
28288 KB |
Output is correct |
11 |
Correct |
25 ms |
28288 KB |
Output is correct |
12 |
Correct |
24 ms |
28288 KB |
Output is correct |
13 |
Correct |
23 ms |
28416 KB |
Output is correct |
14 |
Correct |
20 ms |
28032 KB |
Output is correct |
15 |
Correct |
21 ms |
28288 KB |
Output is correct |
16 |
Correct |
20 ms |
28032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28032 KB |
Output is correct |
2 |
Correct |
22 ms |
28032 KB |
Output is correct |
3 |
Correct |
20 ms |
28032 KB |
Output is correct |
4 |
Correct |
21 ms |
28032 KB |
Output is correct |
5 |
Correct |
36 ms |
31648 KB |
Output is correct |
6 |
Correct |
39 ms |
31616 KB |
Output is correct |
7 |
Correct |
38 ms |
31616 KB |
Output is correct |
8 |
Correct |
34 ms |
32120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
28032 KB |
Output is correct |
2 |
Correct |
20 ms |
28032 KB |
Output is correct |
3 |
Correct |
20 ms |
28032 KB |
Output is correct |
4 |
Correct |
21 ms |
28032 KB |
Output is correct |
5 |
Correct |
20 ms |
28032 KB |
Output is correct |
6 |
Correct |
21 ms |
28032 KB |
Output is correct |
7 |
Correct |
21 ms |
28032 KB |
Output is correct |
8 |
Correct |
20 ms |
28032 KB |
Output is correct |
9 |
Correct |
21 ms |
28288 KB |
Output is correct |
10 |
Correct |
21 ms |
28288 KB |
Output is correct |
11 |
Correct |
21 ms |
28288 KB |
Output is correct |
12 |
Correct |
21 ms |
28288 KB |
Output is correct |
13 |
Correct |
21 ms |
28288 KB |
Output is correct |
14 |
Correct |
37 ms |
31616 KB |
Output is correct |
15 |
Correct |
38 ms |
31692 KB |
Output is correct |
16 |
Correct |
38 ms |
31608 KB |
Output is correct |
17 |
Correct |
35 ms |
31360 KB |
Output is correct |
18 |
Correct |
38 ms |
31744 KB |
Output is correct |
19 |
Correct |
39 ms |
31608 KB |
Output is correct |
20 |
Correct |
37 ms |
31744 KB |
Output is correct |
21 |
Correct |
36 ms |
31616 KB |
Output is correct |
22 |
Correct |
38 ms |
31608 KB |
Output is correct |
23 |
Correct |
37 ms |
31672 KB |
Output is correct |
24 |
Correct |
40 ms |
31736 KB |
Output is correct |
25 |
Correct |
20 ms |
28032 KB |
Output is correct |
26 |
Correct |
22 ms |
28288 KB |
Output is correct |
27 |
Correct |
20 ms |
28032 KB |
Output is correct |
28 |
Correct |
35 ms |
32128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
28032 KB |
Output is correct |
2 |
Correct |
20 ms |
28032 KB |
Output is correct |
3 |
Correct |
20 ms |
28032 KB |
Output is correct |
4 |
Correct |
20 ms |
28032 KB |
Output is correct |
5 |
Correct |
22 ms |
28160 KB |
Output is correct |
6 |
Correct |
21 ms |
28032 KB |
Output is correct |
7 |
Correct |
21 ms |
28032 KB |
Output is correct |
8 |
Correct |
20 ms |
28032 KB |
Output is correct |
9 |
Correct |
21 ms |
28416 KB |
Output is correct |
10 |
Correct |
21 ms |
28288 KB |
Output is correct |
11 |
Correct |
22 ms |
28372 KB |
Output is correct |
12 |
Correct |
22 ms |
28288 KB |
Output is correct |
13 |
Correct |
22 ms |
28288 KB |
Output is correct |
14 |
Correct |
38 ms |
31616 KB |
Output is correct |
15 |
Correct |
37 ms |
31616 KB |
Output is correct |
16 |
Correct |
37 ms |
31608 KB |
Output is correct |
17 |
Correct |
35 ms |
31360 KB |
Output is correct |
18 |
Correct |
38 ms |
31736 KB |
Output is correct |
19 |
Correct |
38 ms |
31616 KB |
Output is correct |
20 |
Correct |
38 ms |
31744 KB |
Output is correct |
21 |
Correct |
37 ms |
31744 KB |
Output is correct |
22 |
Correct |
38 ms |
31616 KB |
Output is correct |
23 |
Correct |
37 ms |
31616 KB |
Output is correct |
24 |
Correct |
539 ms |
110688 KB |
Output is correct |
25 |
Correct |
765 ms |
116968 KB |
Output is correct |
26 |
Correct |
679 ms |
116344 KB |
Output is correct |
27 |
Correct |
648 ms |
117256 KB |
Output is correct |
28 |
Correct |
547 ms |
112248 KB |
Output is correct |
29 |
Correct |
590 ms |
113620 KB |
Output is correct |
30 |
Correct |
622 ms |
113784 KB |
Output is correct |
31 |
Correct |
37 ms |
31744 KB |
Output is correct |
32 |
Correct |
651 ms |
117240 KB |
Output is correct |
33 |
Correct |
21 ms |
28032 KB |
Output is correct |
34 |
Correct |
21 ms |
28288 KB |
Output is correct |
35 |
Correct |
572 ms |
107260 KB |
Output is correct |
36 |
Correct |
20 ms |
28032 KB |
Output is correct |
37 |
Correct |
34 ms |
32128 KB |
Output is correct |
38 |
Correct |
525 ms |
126840 KB |
Output is correct |
39 |
Correct |
433 ms |
95136 KB |
Output is correct |