#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <map>
#include <climits>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
#define speedup ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define ll long long
const ll MAXN = 810;
ll bee[MAXN][MAXN];
ll dist[MAXN][MAXN];
//pair<ll,ll> parent[MAXN][MAXN];
bool vis[MAXN][MAXN];
ll dX[] = {1, -1, 0, 0};
ll dY[] = {0, 0, 1, -1};
pair<ll, ll> mencho, den;
vector<string> grid;
ll n, s;
bool compute(ll k){
if (k >= bee[mencho.first][mencho.second]){
return false;
}
for (ll i = 0; i < MAXN; i++){
for (ll j = 0; j < MAXN; j++){
dist[i][j] = 1e9;
//parent[i][j] = {-1, -1};
vis[i][j] = false;
}
}
queue<pair<ll, ll>> q;
q.push(mencho);
dist[mencho.first][mencho.second] = 0;
while (!q.empty()){
ll x = q.front().first, y = q.front().second; q.pop();
vis[x][y] = true;
if (make_pair(x, y) == den){
break;
}
for (ll i = 0; i < 4; i++){
ll nX = x + dX[i], nY = y+dY[i];
bool b1 = nX < 0 || nX >= n || nY < 0 || nY >= n;
if (b1){
continue;
}
bool b2 = grid[nX][nY] == 'T' || grid[nX][nY] == 'H' || grid[nX][nY] == 'D';
if (b2){
continue;
}
/*if (nX < 0 || nX >= n || nY < 0 || nY >= n || grid[nX][nY] == 'T' || grid[nX][nY] == 'H' || grid[nX][nY] == 'D'){
continue;
}*/
ll cur_time = (dist[x][y] + 1) / s;
if (!vis[nX][nY]) {
if ((dist[nX][nY] > dist[x][y] + 1) && ((bee[nX][nY] - k) > cur_time)) {
dist[nX][nY] = dist[x][y] + 1;
//parent[nX][nY] = {x, y};
q.push({nX, nY});
}
}
}
}
for (int i = 0; i < 4; i++){
ll nX = den.first+dX[i], nY = den.second+dY[i];
bool b1 = nX < 0 || nX >= n || nY < 0 || nY >= n;
if (b1) {
continue;
}
bool b2 = grid[nX][nY] == 'T' || grid[nX][nY] == 'H';
if (b2){
continue;
}
/*if (nX < 0 || nX >= n || nY < 0 || nY >= n || grid[nX][nY] == 'T' || grid[nX][nY] == 'H') {
continue;
}*/
ll cur_time = dist[nX][nY]/s;
if ((bee[nX][nY]-k) > cur_time){
return true;
}
}
return false;
}
int main() {
speedup
cin >> n >> s;
grid.resize(n);
vector<pair<ll, ll>> start;
for (ll i = 0; i < n; i++){
string st; cin >> st;
grid[i] = st;
for (ll j = 0; j < n; j++){
if (st[j] == 'H'){
start.push_back({i, j});
}else if (st[j] == 'M'){
mencho = {i, j};
}else if (st[j] == 'D'){
den = {i, j};
}
}
}
for (ll i = 0; i < MAXN; i++){
for (ll j = 0; j < MAXN; j++){
bee[i][j] = 1e9;
vis[i][j] = false;
}
}
queue<pair<ll, ll>> q;
for (pair<ll, ll> p: start){
bee[p.first][p.second] = 0;
q.push({p.first, p.second});
}
while (!q.empty()){
ll x = q.front().first, y = q.front().second; q.pop();
vis[x][y] = true;
for (ll i = 0; i < 4; i++){
ll nX = x + dX[i], nY = y+dY[i];
bool b1 = nX < 0 || nX >= n || nY < 0 || nY >= n;
if (b1){
continue;
}
bool b2 = grid[nX][nY] == 'T' || grid[nX][nY] == 'D';
if (b2){
continue;
}
if ((bee[nX][nY] > (bee[x][y]+1)) && !vis[nX][nY]){
bee[nX][nY] = bee[x][y]+1;
q.push({nX, nY});
}
}
}
ll l = 0, h = n*n, mid, ans = -1;
while (l <= h){
mid = (l+h)/2;
if (compute(mid)){
ans = mid;
l = mid+1;
}else{
h = mid-1;
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11356 KB |
Output is correct |
2 |
Correct |
3 ms |
11352 KB |
Output is correct |
3 |
Correct |
6 ms |
11512 KB |
Output is correct |
4 |
Correct |
5 ms |
11356 KB |
Output is correct |
5 |
Incorrect |
3 ms |
11356 KB |
Output isn't correct |
6 |
Correct |
3 ms |
11352 KB |
Output is correct |
7 |
Correct |
70 ms |
12120 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
3 ms |
11356 KB |
Output is correct |
10 |
Correct |
3 ms |
11352 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
3 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11352 KB |
Output is correct |
15 |
Correct |
4 ms |
11352 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
4 ms |
11356 KB |
Output is correct |
18 |
Correct |
3 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
3 ms |
11352 KB |
Output is correct |
21 |
Correct |
4 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11352 KB |
Output is correct |
25 |
Correct |
5 ms |
11356 KB |
Output is correct |
26 |
Correct |
4 ms |
11356 KB |
Output is correct |
27 |
Correct |
4 ms |
11352 KB |
Output is correct |
28 |
Correct |
5 ms |
11356 KB |
Output is correct |
29 |
Correct |
4 ms |
11608 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
5 ms |
11356 KB |
Output is correct |
33 |
Correct |
9 ms |
11356 KB |
Output is correct |
34 |
Correct |
7 ms |
11356 KB |
Output is correct |
35 |
Correct |
21 ms |
11516 KB |
Output is correct |
36 |
Correct |
10 ms |
11356 KB |
Output is correct |
37 |
Correct |
8 ms |
11356 KB |
Output is correct |
38 |
Correct |
25 ms |
11556 KB |
Output is correct |
39 |
Correct |
8 ms |
11356 KB |
Output is correct |
40 |
Correct |
8 ms |
11356 KB |
Output is correct |
41 |
Correct |
33 ms |
11612 KB |
Output is correct |
42 |
Correct |
10 ms |
11808 KB |
Output is correct |
43 |
Correct |
10 ms |
11612 KB |
Output is correct |
44 |
Correct |
37 ms |
11640 KB |
Output is correct |
45 |
Correct |
11 ms |
11612 KB |
Output is correct |
46 |
Correct |
11 ms |
11868 KB |
Output is correct |
47 |
Correct |
39 ms |
11612 KB |
Output is correct |
48 |
Correct |
14 ms |
11612 KB |
Output is correct |
49 |
Correct |
11 ms |
11608 KB |
Output is correct |
50 |
Correct |
55 ms |
11612 KB |
Output is correct |
51 |
Correct |
13 ms |
11608 KB |
Output is correct |
52 |
Correct |
13 ms |
11612 KB |
Output is correct |
53 |
Correct |
65 ms |
11852 KB |
Output is correct |
54 |
Correct |
15 ms |
11868 KB |
Output is correct |
55 |
Correct |
14 ms |
11868 KB |
Output is correct |
56 |
Correct |
85 ms |
11864 KB |
Output is correct |
57 |
Correct |
15 ms |
11864 KB |
Output is correct |
58 |
Correct |
16 ms |
11868 KB |
Output is correct |
59 |
Correct |
86 ms |
11980 KB |
Output is correct |
60 |
Correct |
16 ms |
11864 KB |
Output is correct |
61 |
Correct |
23 ms |
11868 KB |
Output is correct |
62 |
Correct |
92 ms |
11868 KB |
Output is correct |
63 |
Correct |
85 ms |
11860 KB |
Output is correct |
64 |
Correct |
150 ms |
11868 KB |
Output is correct |
65 |
Correct |
128 ms |
12068 KB |
Output is correct |
66 |
Correct |
100 ms |
11868 KB |
Output is correct |
67 |
Correct |
87 ms |
11868 KB |
Output is correct |
68 |
Correct |
37 ms |
12120 KB |
Output is correct |
69 |
Correct |
36 ms |
12108 KB |
Output is correct |
70 |
Correct |
29 ms |
11856 KB |
Output is correct |
71 |
Correct |
34 ms |
12124 KB |
Output is correct |
72 |
Correct |
25 ms |
12124 KB |
Output is correct |
73 |
Correct |
24 ms |
12636 KB |
Output is correct |
74 |
Correct |
42 ms |
12604 KB |
Output is correct |
75 |
Correct |
65 ms |
12636 KB |
Output is correct |
76 |
Correct |
51 ms |
12636 KB |
Output is correct |
77 |
Correct |
47 ms |
12636 KB |
Output is correct |
78 |
Correct |
72 ms |
12564 KB |
Output is correct |
79 |
Correct |
38 ms |
12632 KB |
Output is correct |
80 |
Correct |
49 ms |
12568 KB |
Output is correct |
81 |
Correct |
56 ms |
12376 KB |
Output is correct |
82 |
Correct |
48 ms |
12380 KB |
Output is correct |
83 |
Correct |
65 ms |
12380 KB |
Output is correct |
84 |
Correct |
52 ms |
12380 KB |
Output is correct |
85 |
Correct |
62 ms |
12508 KB |
Output is correct |
86 |
Correct |
67 ms |
12376 KB |
Output is correct |
87 |
Correct |
61 ms |
12484 KB |
Output is correct |
88 |
Correct |
65 ms |
12400 KB |
Output is correct |
89 |
Correct |
62 ms |
12412 KB |
Output is correct |
90 |
Correct |
65 ms |
12376 KB |
Output is correct |
91 |
Correct |
58 ms |
12376 KB |
Output is correct |
92 |
Correct |
89 ms |
12380 KB |
Output is correct |