#include <bits/stdc++.h>
/*--------------------------------------------------------------------*/
#define INF 3*100*1000 + 5
#define MAX 1000000
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define intt int64_t
#define piic pair<int64_t, int64_t>
typedef long long ll;
using namespace std;
/*--------------------------------------------------------------------*/
int n, s;
pii b, home;
queue<pii> hives;
vector<string> grid;
int filltime[500][500]{INT_MAX}, beetime[500][500], visited[500][500];
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int inside(int x, int y){
return (x> -1 && x < n && y > -1 && y < n && grid[x][y] != 'T');
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(0); cin.tie(0);
fill(filltime[0], filltime[0] + 500*500, INT_MAX);
if(2 >= INT_MAX) cout << "Error\n";
cin >> n >> s;
for(int i = 0; i < n; ++i){
string s;
cin >> s;
grid.pb(s);
for(int j = 0; j < s.length(); ++j){
if (s[j] == 'M') b = make_pair(i, j);
if (s[j] == 'D') home = make_pair(i, j);
if(s[j] == 'H'){
hives.push(make_pair(i, j));
filltime[i][j] = 0;
}
}
}
while(!hives.empty()){
pii bee = hives.front();
hives.pop();
int x,y;
x = bee.fi; y = bee.se;
for(int i = 0; i < 4; ++i){
if(!inside(x + dx[i], y + dy[i]) || grid[x+dx[i]][y+dy[i]] == 'D') continue;
if(filltime[x+dx[i]][y+dy[i]] != INT_MAX)
filltime[x+dx[i]][y+dy[i]] = min(filltime[x+dx[i]][y+dy[i]], filltime[x][y] + 1);
else {
filltime[x+dx[i]][y+dy[i]] = filltime[x][y] + 1;
hives.push(make_pair(x + dx[i], y + dy[i]));
}
}
}
int lo = 0, hi = filltime[b.fi][b.se];
int ans = 0;
while(lo < hi){
queue<tuple<int, int, int, int>> q;
int tempans = lo + (hi-lo)/2;
bool pos = false;
fill(visited[0], visited[0] + 500*500, 0);
visited[b.fi][b.se] = 1;
q.push(make_tuple(b.fi, b.se, tempans, 0));
// cout << "New Round " << tempans << '\n';
while(!q.empty()){
tuple<int, int, int, int> cur = q.front();
q.pop();
int x = get<0>(cur);
int y = get<1> (cur);
int a = get<2> (cur);
int res = get<3> (cur);
//cout << x << ' ' << y << ' ' << a << ' ' << res << ' ' << q.size() << ' ' << filltime[x][y] << '\n';
if(a + (double)res/s >= filltime[x][y]) continue;
if(x == home.fi && y == home.se) {
pos = true;
// cout << "Success!\n";
break;
}
int x1, y1;
if(res == s-1) {
y1 = 0;
x1 = a + 1;
} else {
x1 = a;
y1 = res + 1;
}
for(int i = 0; i < 4; ++i) {
if(!inside(x+dx[i],y+dy[i])) continue;
if(visited[x+dx[i]][y+dy[i]]) continue;
visited[x+dx[i]][y+dy[i]] = 1;
q.push(make_tuple(x+dx[i], y+dy[i], x1, y1));
}
}
// cout << pos << '\n';
if(pos) {
ans = max(ans, tempans);
lo = tempans + 1;
} else {
hi = tempans;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main(int, const char**)':
mecho.cpp:41:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int j = 0; j < s.length(); ++j){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
2 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Runtime error |
50 ms |
6428 KB |
Execution killed with signal 11 |
8 |
Incorrect |
2 ms |
2260 KB |
Output isn't correct |
9 |
Correct |
2 ms |
2208 KB |
Output is correct |
10 |
Correct |
2 ms |
2260 KB |
Output is correct |
11 |
Correct |
2 ms |
2260 KB |
Output is correct |
12 |
Correct |
2 ms |
2212 KB |
Output is correct |
13 |
Correct |
2 ms |
2204 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2204 KB |
Output isn't correct |
15 |
Correct |
2 ms |
2260 KB |
Output is correct |
16 |
Correct |
2 ms |
2260 KB |
Output is correct |
17 |
Correct |
2 ms |
2260 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2260 KB |
Output is correct |
20 |
Correct |
2 ms |
2204 KB |
Output is correct |
21 |
Correct |
2 ms |
2260 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2204 KB |
Output is correct |
24 |
Correct |
2 ms |
2204 KB |
Output is correct |
25 |
Correct |
2 ms |
2208 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2260 KB |
Output is correct |
28 |
Correct |
2 ms |
2208 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
2 ms |
2212 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
4 ms |
2476 KB |
Output is correct |
34 |
Correct |
4 ms |
2452 KB |
Output is correct |
35 |
Correct |
15 ms |
2560 KB |
Output is correct |
36 |
Correct |
4 ms |
2516 KB |
Output is correct |
37 |
Correct |
4 ms |
2516 KB |
Output is correct |
38 |
Correct |
21 ms |
2604 KB |
Output is correct |
39 |
Correct |
5 ms |
2704 KB |
Output is correct |
40 |
Correct |
5 ms |
2644 KB |
Output is correct |
41 |
Correct |
28 ms |
2656 KB |
Output is correct |
42 |
Correct |
6 ms |
2708 KB |
Output is correct |
43 |
Correct |
6 ms |
2736 KB |
Output is correct |
44 |
Correct |
31 ms |
2828 KB |
Output is correct |
45 |
Incorrect |
4 ms |
2900 KB |
Output isn't correct |
46 |
Incorrect |
4 ms |
1836 KB |
Output isn't correct |
47 |
Incorrect |
18 ms |
2856 KB |
Output isn't correct |
48 |
Incorrect |
3 ms |
2988 KB |
Output isn't correct |
49 |
Incorrect |
5 ms |
1988 KB |
Output isn't correct |
50 |
Incorrect |
18 ms |
2952 KB |
Output isn't correct |
51 |
Incorrect |
4 ms |
3096 KB |
Output isn't correct |
52 |
Incorrect |
5 ms |
2132 KB |
Output isn't correct |
53 |
Incorrect |
19 ms |
3156 KB |
Output isn't correct |
54 |
Incorrect |
4 ms |
3228 KB |
Output isn't correct |
55 |
Incorrect |
5 ms |
2332 KB |
Output isn't correct |
56 |
Incorrect |
21 ms |
3284 KB |
Output isn't correct |
57 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
58 |
Incorrect |
6 ms |
2388 KB |
Output isn't correct |
59 |
Incorrect |
21 ms |
3412 KB |
Output isn't correct |
60 |
Incorrect |
5 ms |
3540 KB |
Output isn't correct |
61 |
Incorrect |
6 ms |
2604 KB |
Output isn't correct |
62 |
Incorrect |
22 ms |
3504 KB |
Output isn't correct |
63 |
Correct |
5 ms |
2572 KB |
Output is correct |
64 |
Incorrect |
5 ms |
2516 KB |
Output isn't correct |
65 |
Incorrect |
6 ms |
2656 KB |
Output isn't correct |
66 |
Incorrect |
7 ms |
2580 KB |
Output isn't correct |
67 |
Incorrect |
6 ms |
2516 KB |
Output isn't correct |
68 |
Correct |
10 ms |
2976 KB |
Output is correct |
69 |
Incorrect |
11 ms |
3028 KB |
Output isn't correct |
70 |
Incorrect |
11 ms |
3028 KB |
Output isn't correct |
71 |
Incorrect |
10 ms |
2984 KB |
Output isn't correct |
72 |
Incorrect |
9 ms |
2984 KB |
Output isn't correct |
73 |
Runtime error |
6 ms |
5128 KB |
Execution killed with signal 11 |
74 |
Runtime error |
7 ms |
5792 KB |
Execution killed with signal 11 |
75 |
Runtime error |
5 ms |
5160 KB |
Execution killed with signal 11 |
76 |
Runtime error |
5 ms |
5168 KB |
Execution killed with signal 11 |
77 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 11 |
78 |
Runtime error |
15 ms |
4824 KB |
Execution killed with signal 11 |
79 |
Runtime error |
14 ms |
4868 KB |
Execution killed with signal 11 |
80 |
Runtime error |
14 ms |
4852 KB |
Execution killed with signal 11 |
81 |
Runtime error |
14 ms |
4912 KB |
Execution killed with signal 11 |
82 |
Runtime error |
13 ms |
4908 KB |
Execution killed with signal 11 |
83 |
Runtime error |
42 ms |
6436 KB |
Execution killed with signal 11 |
84 |
Runtime error |
48 ms |
6456 KB |
Execution killed with signal 11 |
85 |
Runtime error |
39 ms |
6512 KB |
Execution killed with signal 11 |
86 |
Runtime error |
38 ms |
6432 KB |
Execution killed with signal 11 |
87 |
Runtime error |
44 ms |
6424 KB |
Execution killed with signal 11 |
88 |
Runtime error |
5 ms |
5664 KB |
Execution killed with signal 6 |
89 |
Runtime error |
6 ms |
5768 KB |
Execution killed with signal 6 |
90 |
Runtime error |
6 ms |
5672 KB |
Execution killed with signal 6 |
91 |
Runtime error |
6 ms |
5644 KB |
Execution killed with signal 6 |
92 |
Runtime error |
6 ms |
5588 KB |
Execution killed with signal 6 |