#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int N, S;
char arr[1024][1024];
int dist[1024][1024];
bool vis[1024][1024];
bool valid(int x, int y)
{
return 0<=x&&x<N&&0<=y&&y<N;
}
bool can(int t)
{
queue<tuple<int, int, int> > Q;
memset(vis, false, sizeof(vis));
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
if(arr[i][j] == 'M')
{
if(dist[i][j] <= t*S) return false;
Q.emplace(t*S, i, j);
}
while(!Q.empty())
{
int di, x, y; tie(di, x, y) = Q.front(); Q.pop();
if(arr[x][y] == 'D') return true;
for(int d=0; d<4; ++d)
{
int nx = x+"1210"[d]-'1';
int ny = y+"0121"[d]-'1';
if(!valid(nx, ny)) continue;
if(arr[nx][ny] != 'G' && arr[nx][ny] != 'D') continue;
if(dist[nx][ny] <= di+1) continue;
if(vis[nx][ny]) continue;
Q.emplace(di+1, nx, ny);
vis[nx][ny] = true;
}
}
return false;
}
void init()
{
memset(dist, 0x3f, sizeof dist);
queue<pair<int, int> > Q;
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
{
if(arr[i][j] == 'H')
{
Q.emplace(i, j);
dist[i][j] = 0;
}
}
while(!Q.empty())
{
int x, y; tie(x, y) = Q.front(); Q.pop();
for(int d=0;d<4;++d)
{
int nx = x+"1210"[d] - '1';
int ny = y+"0121"[d] - '1';
if(!valid(nx, ny)) continue;
if(arr[nx][ny] != 'G' && arr[nx][ny] != 'M') continue;
if(dist[nx][ny] != INF) continue;
dist[nx][ny] = dist[x][y]+S;
Q.emplace(nx, ny);
}
}
}
int main()
{
scanf("%d%d", &N, &S);
for(int i=0; i<N; ++i)
scanf("%s", arr[i]);
int lo = -1; //pos
int hi = N*N; //impos
init();
while(lo+1!=hi)
{
int mi = (lo+hi)/2;
if(can(mi)) lo = mi;
else hi = mi;
}
printf("%d\n", lo);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &S);
~~~~~^~~~~~~~~~~~~~~~
mecho.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", arr[i]);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5368 KB |
Output is correct |
2 |
Correct |
6 ms |
5476 KB |
Output is correct |
3 |
Correct |
6 ms |
5528 KB |
Output is correct |
4 |
Correct |
7 ms |
5528 KB |
Output is correct |
5 |
Correct |
6 ms |
5584 KB |
Output is correct |
6 |
Correct |
6 ms |
5588 KB |
Output is correct |
7 |
Correct |
146 ms |
6484 KB |
Output is correct |
8 |
Correct |
7 ms |
6484 KB |
Output is correct |
9 |
Correct |
6 ms |
6484 KB |
Output is correct |
10 |
Correct |
7 ms |
6484 KB |
Output is correct |
11 |
Correct |
7 ms |
6484 KB |
Output is correct |
12 |
Correct |
7 ms |
6484 KB |
Output is correct |
13 |
Correct |
7 ms |
6484 KB |
Output is correct |
14 |
Correct |
7 ms |
6484 KB |
Output is correct |
15 |
Correct |
7 ms |
6484 KB |
Output is correct |
16 |
Correct |
6 ms |
6484 KB |
Output is correct |
17 |
Correct |
6 ms |
6484 KB |
Output is correct |
18 |
Correct |
6 ms |
6484 KB |
Output is correct |
19 |
Correct |
6 ms |
6484 KB |
Output is correct |
20 |
Correct |
6 ms |
6484 KB |
Output is correct |
21 |
Correct |
6 ms |
6484 KB |
Output is correct |
22 |
Correct |
6 ms |
6484 KB |
Output is correct |
23 |
Correct |
6 ms |
6484 KB |
Output is correct |
24 |
Correct |
7 ms |
6484 KB |
Output is correct |
25 |
Correct |
6 ms |
6484 KB |
Output is correct |
26 |
Correct |
7 ms |
6484 KB |
Output is correct |
27 |
Correct |
7 ms |
6484 KB |
Output is correct |
28 |
Correct |
7 ms |
6484 KB |
Output is correct |
29 |
Correct |
8 ms |
6484 KB |
Output is correct |
30 |
Correct |
6 ms |
6484 KB |
Output is correct |
31 |
Correct |
7 ms |
6484 KB |
Output is correct |
32 |
Correct |
7 ms |
6484 KB |
Output is correct |
33 |
Correct |
13 ms |
6484 KB |
Output is correct |
34 |
Correct |
15 ms |
6484 KB |
Output is correct |
35 |
Correct |
35 ms |
6484 KB |
Output is correct |
36 |
Correct |
15 ms |
6484 KB |
Output is correct |
37 |
Correct |
18 ms |
6484 KB |
Output is correct |
38 |
Correct |
69 ms |
6484 KB |
Output is correct |
39 |
Correct |
20 ms |
6484 KB |
Output is correct |
40 |
Correct |
21 ms |
6484 KB |
Output is correct |
41 |
Correct |
57 ms |
6484 KB |
Output is correct |
42 |
Correct |
27 ms |
6484 KB |
Output is correct |
43 |
Correct |
28 ms |
6484 KB |
Output is correct |
44 |
Correct |
97 ms |
6484 KB |
Output is correct |
45 |
Correct |
26 ms |
6484 KB |
Output is correct |
46 |
Correct |
28 ms |
6484 KB |
Output is correct |
47 |
Correct |
81 ms |
6484 KB |
Output is correct |
48 |
Correct |
28 ms |
6484 KB |
Output is correct |
49 |
Correct |
35 ms |
6484 KB |
Output is correct |
50 |
Correct |
94 ms |
6484 KB |
Output is correct |
51 |
Correct |
30 ms |
6484 KB |
Output is correct |
52 |
Correct |
36 ms |
6484 KB |
Output is correct |
53 |
Correct |
114 ms |
6484 KB |
Output is correct |
54 |
Correct |
37 ms |
6484 KB |
Output is correct |
55 |
Correct |
47 ms |
6484 KB |
Output is correct |
56 |
Correct |
209 ms |
6524 KB |
Output is correct |
57 |
Correct |
51 ms |
6524 KB |
Output is correct |
58 |
Correct |
61 ms |
6524 KB |
Output is correct |
59 |
Correct |
228 ms |
6524 KB |
Output is correct |
60 |
Correct |
44 ms |
6524 KB |
Output is correct |
61 |
Correct |
58 ms |
6524 KB |
Output is correct |
62 |
Correct |
194 ms |
6564 KB |
Output is correct |
63 |
Correct |
174 ms |
6564 KB |
Output is correct |
64 |
Correct |
310 ms |
6652 KB |
Output is correct |
65 |
Correct |
237 ms |
6652 KB |
Output is correct |
66 |
Correct |
198 ms |
6652 KB |
Output is correct |
67 |
Correct |
191 ms |
6652 KB |
Output is correct |
68 |
Correct |
107 ms |
6652 KB |
Output is correct |
69 |
Correct |
91 ms |
6652 KB |
Output is correct |
70 |
Correct |
84 ms |
6652 KB |
Output is correct |
71 |
Correct |
83 ms |
6652 KB |
Output is correct |
72 |
Correct |
76 ms |
6652 KB |
Output is correct |
73 |
Correct |
47 ms |
6652 KB |
Output is correct |
74 |
Correct |
122 ms |
6688 KB |
Output is correct |
75 |
Correct |
143 ms |
6688 KB |
Output is correct |
76 |
Correct |
138 ms |
6688 KB |
Output is correct |
77 |
Correct |
98 ms |
6688 KB |
Output is correct |
78 |
Correct |
134 ms |
6688 KB |
Output is correct |
79 |
Correct |
97 ms |
6688 KB |
Output is correct |
80 |
Correct |
106 ms |
6688 KB |
Output is correct |
81 |
Correct |
123 ms |
6688 KB |
Output is correct |
82 |
Correct |
107 ms |
6688 KB |
Output is correct |
83 |
Correct |
122 ms |
6692 KB |
Output is correct |
84 |
Correct |
103 ms |
6692 KB |
Output is correct |
85 |
Correct |
123 ms |
6692 KB |
Output is correct |
86 |
Correct |
114 ms |
6692 KB |
Output is correct |
87 |
Correct |
146 ms |
6692 KB |
Output is correct |
88 |
Correct |
165 ms |
6692 KB |
Output is correct |
89 |
Correct |
123 ms |
6692 KB |
Output is correct |
90 |
Correct |
123 ms |
6692 KB |
Output is correct |
91 |
Correct |
122 ms |
6692 KB |
Output is correct |
92 |
Correct |
133 ms |
6744 KB |
Output is correct |