#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int,int> pii;
int n;
int s;
char gazar[810][810];
bool vis[810][810];
int beed[810][810];
int mei, mej;
bool feasible (int x){
memset(vis, 0, sizeof vis);
queue<pii> q;
int depth = x * s;
q.push(mp(mei, mej));
vis[mei][mej] = 1;
while(!q.empty()){
int sz = q.size();
for (int cnt = 0; cnt < sz; cnt++){
pii nw = q.front();
if (gazar[nw.xx][nw.yy] == 'D') return 1;
q.pop();
if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'D' || (gazar[nw.xx + 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx + 1][nw.yy]) ) ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'D' || (gazar[nw.xx - 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx - 1][nw.yy]) ) ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'D' || (gazar[nw.xx][nw.yy + 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy + 1]) ) ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'D' || (gazar[nw.xx][nw.yy - 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy - 1]) ) ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
}
depth ++;
}
return 0;
}
int main (){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("in.txt", "r", stdin);
cin >> n;
cin >> s;
for (int i = 1; i <= n; i++){
string tmp;
cin >> tmp;
// cout << tmp;
for (int j = 1; j <= n; j++){
gazar[i][j] = tmp[j - 1];
if (tmp[j - 1] == 'M') mei = i, mej = j;
}
}
queue<pii> q;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (gazar[i][j] == 'H') q.push( mp(i, j) ), vis[i][j] = 1;
}
}
int depth = 0;
// cout << "QQQ";
while (!q.empty()){
int sz = q.size();
// cout << sz << ' ';
for (int cnt = 0; cnt < sz; cnt++){
pii nw = q.front();
q.pop();
beed[nw.xx][nw.yy] = depth;
if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'M' || gazar[nw.xx + 1][nw.yy] == 'G') ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'M' || gazar[nw.xx - 1][nw.yy] == 'G') ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'M' || gazar[nw.xx][nw.yy + 1] == 'G') ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'M' || gazar[nw.xx][nw.yy - 1] == 'G') ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
}
depth++;
}
int lo = 0, hi = beed[mei][mej] - 1;
while (lo != hi){
int mid = (lo + hi + 1) >> 1;
if (feasible(mid)) lo = mid;
else hi = mid - 1;
}
if(feasible(lo)) cout << lo;
else cout << -1;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:38:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("in.txt", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1082 ms |
1016 KB |
Time limit exceeded |
2 |
Execution timed out |
1084 ms |
1016 KB |
Time limit exceeded |
3 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |
4 |
Execution timed out |
1086 ms |
1016 KB |
Time limit exceeded |
5 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
6 |
Execution timed out |
1089 ms |
1016 KB |
Time limit exceeded |
7 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
8 |
Execution timed out |
1089 ms |
1016 KB |
Time limit exceeded |
9 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
10 |
Execution timed out |
1086 ms |
1016 KB |
Time limit exceeded |
11 |
Execution timed out |
1088 ms |
1020 KB |
Time limit exceeded |
12 |
Execution timed out |
1068 ms |
1016 KB |
Time limit exceeded |
13 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |
14 |
Execution timed out |
1077 ms |
1016 KB |
Time limit exceeded |
15 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
16 |
Execution timed out |
1092 ms |
1016 KB |
Time limit exceeded |
17 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
18 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
19 |
Execution timed out |
1082 ms |
1016 KB |
Time limit exceeded |
20 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
21 |
Execution timed out |
1093 ms |
1016 KB |
Time limit exceeded |
22 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
23 |
Execution timed out |
1087 ms |
1020 KB |
Time limit exceeded |
24 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
25 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
26 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
27 |
Execution timed out |
1074 ms |
1016 KB |
Time limit exceeded |
28 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
29 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
30 |
Execution timed out |
1078 ms |
1016 KB |
Time limit exceeded |
31 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
32 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
33 |
Execution timed out |
1075 ms |
1016 KB |
Time limit exceeded |
34 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
35 |
Execution timed out |
1079 ms |
988 KB |
Time limit exceeded |
36 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
37 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |
38 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
39 |
Execution timed out |
1075 ms |
1016 KB |
Time limit exceeded |
40 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
41 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
42 |
Execution timed out |
1077 ms |
1016 KB |
Time limit exceeded |
43 |
Execution timed out |
1072 ms |
1016 KB |
Time limit exceeded |
44 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
45 |
Execution timed out |
1065 ms |
1016 KB |
Time limit exceeded |
46 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
47 |
Execution timed out |
1074 ms |
1016 KB |
Time limit exceeded |
48 |
Execution timed out |
1083 ms |
1020 KB |
Time limit exceeded |
49 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
50 |
Execution timed out |
1084 ms |
1016 KB |
Time limit exceeded |
51 |
Execution timed out |
1090 ms |
1016 KB |
Time limit exceeded |
52 |
Execution timed out |
1083 ms |
1016 KB |
Time limit exceeded |
53 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
54 |
Execution timed out |
1090 ms |
1016 KB |
Time limit exceeded |
55 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
56 |
Execution timed out |
1089 ms |
1016 KB |
Time limit exceeded |
57 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |
58 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
59 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
60 |
Execution timed out |
1072 ms |
1020 KB |
Time limit exceeded |
61 |
Execution timed out |
1082 ms |
1016 KB |
Time limit exceeded |
62 |
Execution timed out |
1078 ms |
1020 KB |
Time limit exceeded |
63 |
Execution timed out |
1077 ms |
1016 KB |
Time limit exceeded |
64 |
Execution timed out |
1075 ms |
1016 KB |
Time limit exceeded |
65 |
Execution timed out |
1087 ms |
1016 KB |
Time limit exceeded |
66 |
Execution timed out |
1082 ms |
1016 KB |
Time limit exceeded |
67 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
68 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
69 |
Execution timed out |
1069 ms |
1016 KB |
Time limit exceeded |
70 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
71 |
Execution timed out |
1091 ms |
1016 KB |
Time limit exceeded |
72 |
Execution timed out |
1090 ms |
1016 KB |
Time limit exceeded |
73 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |
74 |
Execution timed out |
1070 ms |
1016 KB |
Time limit exceeded |
75 |
Execution timed out |
1073 ms |
1016 KB |
Time limit exceeded |
76 |
Execution timed out |
1089 ms |
1016 KB |
Time limit exceeded |
77 |
Execution timed out |
1079 ms |
1016 KB |
Time limit exceeded |
78 |
Execution timed out |
1071 ms |
1144 KB |
Time limit exceeded |
79 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
80 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
81 |
Execution timed out |
1080 ms |
1020 KB |
Time limit exceeded |
82 |
Execution timed out |
1069 ms |
1016 KB |
Time limit exceeded |
83 |
Execution timed out |
1076 ms |
1016 KB |
Time limit exceeded |
84 |
Execution timed out |
1072 ms |
1008 KB |
Time limit exceeded |
85 |
Execution timed out |
1077 ms |
1016 KB |
Time limit exceeded |
86 |
Execution timed out |
1080 ms |
1016 KB |
Time limit exceeded |
87 |
Execution timed out |
1084 ms |
1016 KB |
Time limit exceeded |
88 |
Execution timed out |
1074 ms |
1016 KB |
Time limit exceeded |
89 |
Execution timed out |
1072 ms |
1016 KB |
Time limit exceeded |
90 |
Execution timed out |
1081 ms |
1016 KB |
Time limit exceeded |
91 |
Execution timed out |
1084 ms |
1016 KB |
Time limit exceeded |
92 |
Execution timed out |
1085 ms |
1016 KB |
Time limit exceeded |