#pragma warning(suppress : 4996)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
// char buf[1 << 23], * p1 = buf, * p2 = buf;
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template<typename T> inline void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * (1 << 1) + x * (1 << 3) + (ch - 48); ch = getchar(); } x *= f; }
template<typename x, typename... y>void read(x& a, y&... b) { read(a); read(b...); }
template<typename T> inline void print(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0'); }
template<typename x, typename... y>void print(x& a, y&... b) { print(a); putchar(' '); print(b...); }
#define spa putchar(' ')
#define nl putchar('\n')
#define printv(a) for(int p = 0; p < a.size(); p++) print(a[p]), (p == a.size() - 1? nl : sp);
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)
#define all(x) x.begin(),x.end()
#define me(x,a) memset(x,a,sizeof(x))
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int N, S; cin >> N >> S;
vector<vector<char>> G(N + 1, vector<char>(N + 1));
vector<vector<int>> dis(N + 1, vector<int>(N + 1, -1));
int init_x, init_y, end_x, end_y;
queue<pair<int, int>> q;
L(i, 1, N) L(j, 1, N) {
cin >> G[i][j];
if (G[i][j] == 'M') {
init_x = i, init_y = j;
G[i][j] == 'G';
}
if (G[i][j] == 'D') {
end_x = i, end_y = j;
}
if (G[i][j] == 'H') {
dis[i][j] = 0;
q.push(make_pair(i, j));
}
}
auto can_go = [&](int x, int y) { return x >= 1 && x <= N && y >= 1 && y <= N && G[x][y] != 'T'; };
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1, -1, 0,0 };
while (q.size()) {
int x = q.front().first, y = q.front().second; q.pop();
for (int dir = 0; dir < 4; dir++) {
int xx = x + dx[dir], yy = y + dy[dir];
if (can_go(xx, yy) && G[xx][yy] != 'D' && dis[xx][yy] == -1) {
dis[xx][yy] = dis[x][y] + S;
q.push(make_pair(xx, yy));
}
}
}
dis[end_x][end_y] = N * N * S + 1000;
function<bool(int)> check = [&](int T) {
if (T * S >= dis[init_x][init_y]) return false;
queue<pair<int, pair<int, int>>> Q;
vector<vector<bool>> vis(N + 1, vector<bool>(N + 1));
Q.push(make_pair(T * S, make_pair(init_x, init_y)));
vis[init_x][init_y] = true;
while (Q.size()) {
int d = Q.front().first;
int x = Q.front().second.first, y = Q.front().second.second;
Q.pop();
if (x == end_x && y == end_y) return true;
for (int dir = 0; dir < 4; dir++) {
int xx = x + dx[dir], yy = y + dy[dir];
if (can_go(xx, yy) && !vis[xx][yy] && (d + 1 < dis[xx][yy])) {
Q.push(make_pair(d + 1, make_pair(xx, yy)));
vis[xx][yy] = true;
}
}
}
return false;
};
int lo = 0, hi = 2 * N * N + 5000, ans = -1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (check(mid)) {
ans = mid;
lo = mid + 1;
}
else hi = mid - 1;
}
cout << ans << '\n';
}
Compilation message
mecho.cpp:1: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
1 | #pragma warning(suppress : 4996)
|
mecho.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
mecho.cpp:29:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
29 | #pragma GCC optimize("-fwhole-program")
| ^
mecho.cpp:36:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
36 | #pragma GCC optimize("-fstrict-overflow")
| ^
mecho.cpp:38:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
38 | #pragma GCC optimize("-fcse-skip-blocks")
| ^
mecho.cpp:52:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
52 | #pragma GCC optimize("-funsafe-loop-optimizations")
| ^
mecho.cpp:57:43: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
57 | template<typename T> inline void read(T& x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * (1 << 1) + x * (1 << 3) + (ch - 48); ch = getchar(); } x *= f; }
| ^
mecho.cpp:57:43: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:57:43: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:57:43: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp:58:59: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
58 | template<typename x, typename... y>void read(x& a, y&... b) { read(a); read(b...); }
| ^
mecho.cpp:58:59: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:58:59: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:58:59: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp:59:43: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
59 | template<typename T> inline void print(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0'); }
| ^
mecho.cpp:59:43: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:59:43: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:59:43: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp:60:60: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
60 | template<typename x, typename... y>void print(x& a, y&... b) { print(a); putchar(' '); print(b...); }
| ^
mecho.cpp:60:60: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:60:60: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:60:60: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp:70:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
70 | int main() {
| ^
mecho.cpp:70:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:70:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:70:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp: In function 'int main()':
mecho.cpp:83:12: warning: value computed is not used [-Wunused-value]
83 | G[i][j] == 'G';
mecho.cpp:93:32: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
93 | auto can_go = [&](int x, int y) { return x >= 1 && x <= N && y >= 1 && y <= N && G[x][y] != 'T'; };
| ^
mecho.cpp:93:32: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:93:32: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:93:32: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
mecho.cpp:108:39: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
108 | function<bool(int)> check = [&](int T) {
| ^
mecho.cpp:108:39: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
mecho.cpp:108:39: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
mecho.cpp:108:39: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
150 ms |
3772 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
4 ms |
1112 KB |
Output is correct |
34 |
Correct |
4 ms |
1116 KB |
Output is correct |
35 |
Correct |
33 ms |
1116 KB |
Output is correct |
36 |
Correct |
4 ms |
1116 KB |
Output is correct |
37 |
Correct |
5 ms |
1112 KB |
Output is correct |
38 |
Correct |
49 ms |
1368 KB |
Output is correct |
39 |
Correct |
5 ms |
1372 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
60 ms |
1548 KB |
Output is correct |
42 |
Correct |
7 ms |
1628 KB |
Output is correct |
43 |
Correct |
7 ms |
1628 KB |
Output is correct |
44 |
Correct |
76 ms |
1628 KB |
Output is correct |
45 |
Correct |
8 ms |
1884 KB |
Output is correct |
46 |
Correct |
8 ms |
1984 KB |
Output is correct |
47 |
Correct |
85 ms |
2036 KB |
Output is correct |
48 |
Correct |
11 ms |
2140 KB |
Output is correct |
49 |
Correct |
9 ms |
2140 KB |
Output is correct |
50 |
Correct |
103 ms |
2140 KB |
Output is correct |
51 |
Correct |
11 ms |
2652 KB |
Output is correct |
52 |
Correct |
11 ms |
2588 KB |
Output is correct |
53 |
Correct |
128 ms |
2652 KB |
Output is correct |
54 |
Correct |
12 ms |
2908 KB |
Output is correct |
55 |
Correct |
13 ms |
2908 KB |
Output is correct |
56 |
Correct |
158 ms |
3156 KB |
Output is correct |
57 |
Correct |
14 ms |
3164 KB |
Output is correct |
58 |
Correct |
15 ms |
3164 KB |
Output is correct |
59 |
Correct |
173 ms |
3164 KB |
Output is correct |
60 |
Correct |
16 ms |
3676 KB |
Output is correct |
61 |
Correct |
17 ms |
3676 KB |
Output is correct |
62 |
Correct |
196 ms |
3764 KB |
Output is correct |
63 |
Correct |
127 ms |
3664 KB |
Output is correct |
64 |
Correct |
220 ms |
3676 KB |
Output is correct |
65 |
Correct |
180 ms |
3756 KB |
Output is correct |
66 |
Correct |
151 ms |
3668 KB |
Output is correct |
67 |
Correct |
142 ms |
3672 KB |
Output is correct |
68 |
Correct |
52 ms |
3676 KB |
Output is correct |
69 |
Correct |
51 ms |
3672 KB |
Output is correct |
70 |
Correct |
37 ms |
3672 KB |
Output is correct |
71 |
Correct |
45 ms |
3872 KB |
Output is correct |
72 |
Correct |
29 ms |
3676 KB |
Output is correct |
73 |
Correct |
38 ms |
3932 KB |
Output is correct |
74 |
Correct |
87 ms |
3924 KB |
Output is correct |
75 |
Correct |
111 ms |
3932 KB |
Output is correct |
76 |
Correct |
93 ms |
3932 KB |
Output is correct |
77 |
Correct |
93 ms |
3928 KB |
Output is correct |
78 |
Correct |
121 ms |
3676 KB |
Output is correct |
79 |
Correct |
67 ms |
3884 KB |
Output is correct |
80 |
Correct |
93 ms |
3672 KB |
Output is correct |
81 |
Correct |
105 ms |
3676 KB |
Output is correct |
82 |
Correct |
89 ms |
3892 KB |
Output is correct |
83 |
Correct |
124 ms |
3852 KB |
Output is correct |
84 |
Correct |
112 ms |
3920 KB |
Output is correct |
85 |
Correct |
122 ms |
3676 KB |
Output is correct |
86 |
Correct |
119 ms |
3848 KB |
Output is correct |
87 |
Correct |
123 ms |
3848 KB |
Output is correct |
88 |
Correct |
132 ms |
3812 KB |
Output is correct |
89 |
Correct |
129 ms |
3676 KB |
Output is correct |
90 |
Correct |
135 ms |
3676 KB |
Output is correct |
91 |
Correct |
132 ms |
3792 KB |
Output is correct |
92 |
Correct |
143 ms |
3672 KB |
Output is correct |