#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 810;
char grid[MAXN][MAXN];
int dr[4] = {0,0,1,-1}, dc[4] = {1,-1,0,0},N;
bool goodbees(int r, int c){
return r >= 0 && c >= 0 && r <= N-1 && c <= N-1 && grid[r][c] != 'T' && grid[r][c] != 'D';
}
bool goodmecho(int r, int c){
return r >= 0 && c >= 0 && r <= N-1 && c <= N-1 && grid[r][c] != 'T';
}
const int INF = 1e9;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int S; cin >> N >> S;
queue<pair<int,int>> q;
vector<vector<int>> dist(N,vector<int>(N,INF));
pair<int,int> m,home;
for(int i = 0;i<N;++i){
for(int j = 0;j<N;++j){
cin >> grid[i][j];
if(grid[i][j] == 'H'){
q.push({i,j});
dist[i][j] = 0;
}
else if(grid[i][j] == 'M'){
m.f = i, m.s = j;
}
else if(grid[i][j] == 'D'){
home.f = i, home.s = j;
}
}
}
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodbees(nr,nc)){
if(dist[nr][nc] > dist[r][c]+1){
dist[nr][nc] = dist[r][c]+1;
q.push({nr,nc});
}
}
}
}
/*vector<vector<int>> mecho(N,vector<int>(N,INF));
mecho[m.f][m.s] = 0; q.push(m);
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodmecho(nr,nc)){
if(mecho[nr][nc] > mecho[r][c]+1){
mecho[nr][nc] = mecho[r][c]+1;
q.push({nr,nc});
}
}
}
}*/
int lo = 0, hi = INF;
if(hi < 0){
cout << "-1\n";
return 0;
}
vector<vector<int>> D(N,vector<int>(N,INF));
while(lo < hi){
int mid = lo+(hi-lo+1)/2; //time
//cout << mid << "\n";
D = vector<vector<int>>(N,vector<int>(N,INF));
q.push(m); D[m.f][m.s] = 0;
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
//cout << r << " " << c << "\n";
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodmecho(nr,nc)){
if(D[r][c]/S +mid < dist[nr][nc] && D[r][c]+1 < D[nr][nc]){
D[nr][nc] = D[r][c]+1;
q.push({nr,nc});
}
}
}
}
if(D[home.f][home.s] == INF){
hi = mid-1;
}
else{
lo = mid;
}
}
cout << lo << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
92 ms |
8764 KB |
Output is correct |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Incorrect |
6 ms |
2196 KB |
Output isn't correct |
34 |
Correct |
6 ms |
2228 KB |
Output is correct |
35 |
Correct |
20 ms |
2192 KB |
Output is correct |
36 |
Incorrect |
7 ms |
2652 KB |
Output isn't correct |
37 |
Correct |
9 ms |
2940 KB |
Output is correct |
38 |
Correct |
32 ms |
2648 KB |
Output is correct |
39 |
Incorrect |
12 ms |
3240 KB |
Output isn't correct |
40 |
Correct |
9 ms |
3224 KB |
Output is correct |
41 |
Correct |
35 ms |
3192 KB |
Output is correct |
42 |
Incorrect |
10 ms |
3856 KB |
Output isn't correct |
43 |
Correct |
10 ms |
3840 KB |
Output is correct |
44 |
Correct |
43 ms |
3856 KB |
Output is correct |
45 |
Incorrect |
13 ms |
4524 KB |
Output isn't correct |
46 |
Correct |
13 ms |
4492 KB |
Output is correct |
47 |
Correct |
54 ms |
4480 KB |
Output is correct |
48 |
Incorrect |
21 ms |
5264 KB |
Output isn't correct |
49 |
Correct |
18 ms |
5236 KB |
Output is correct |
50 |
Correct |
61 ms |
5288 KB |
Output is correct |
51 |
Incorrect |
18 ms |
5980 KB |
Output isn't correct |
52 |
Correct |
18 ms |
5980 KB |
Output is correct |
53 |
Correct |
72 ms |
5980 KB |
Output is correct |
54 |
Incorrect |
22 ms |
6856 KB |
Output isn't correct |
55 |
Correct |
22 ms |
6860 KB |
Output is correct |
56 |
Correct |
87 ms |
6864 KB |
Output is correct |
57 |
Incorrect |
24 ms |
7756 KB |
Output isn't correct |
58 |
Correct |
22 ms |
7748 KB |
Output is correct |
59 |
Correct |
106 ms |
7960 KB |
Output is correct |
60 |
Incorrect |
26 ms |
8708 KB |
Output isn't correct |
61 |
Correct |
25 ms |
8716 KB |
Output is correct |
62 |
Correct |
136 ms |
8764 KB |
Output is correct |
63 |
Correct |
82 ms |
8700 KB |
Output is correct |
64 |
Correct |
141 ms |
8732 KB |
Output is correct |
65 |
Correct |
146 ms |
8692 KB |
Output is correct |
66 |
Incorrect |
104 ms |
8696 KB |
Output isn't correct |
67 |
Incorrect |
92 ms |
8724 KB |
Output isn't correct |
68 |
Correct |
45 ms |
8712 KB |
Output is correct |
69 |
Correct |
48 ms |
8760 KB |
Output is correct |
70 |
Correct |
44 ms |
8736 KB |
Output is correct |
71 |
Correct |
38 ms |
8812 KB |
Output is correct |
72 |
Incorrect |
36 ms |
8764 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
8764 KB |
Output isn't correct |
74 |
Correct |
76 ms |
8720 KB |
Output is correct |
75 |
Correct |
83 ms |
8712 KB |
Output is correct |
76 |
Correct |
64 ms |
9016 KB |
Output is correct |
77 |
Correct |
71 ms |
8772 KB |
Output is correct |
78 |
Incorrect |
72 ms |
8732 KB |
Output isn't correct |
79 |
Correct |
62 ms |
8756 KB |
Output is correct |
80 |
Correct |
68 ms |
8752 KB |
Output is correct |
81 |
Correct |
73 ms |
8768 KB |
Output is correct |
82 |
Correct |
63 ms |
8736 KB |
Output is correct |
83 |
Correct |
75 ms |
8716 KB |
Output is correct |
84 |
Correct |
77 ms |
8760 KB |
Output is correct |
85 |
Correct |
72 ms |
8740 KB |
Output is correct |
86 |
Correct |
82 ms |
8768 KB |
Output is correct |
87 |
Correct |
77 ms |
8740 KB |
Output is correct |
88 |
Correct |
77 ms |
8752 KB |
Output is correct |
89 |
Correct |
88 ms |
8764 KB |
Output is correct |
90 |
Correct |
93 ms |
8720 KB |
Output is correct |
91 |
Correct |
93 ms |
8752 KB |
Output is correct |
92 |
Correct |
88 ms |
8764 KB |
Output is correct |