#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>
#define MAX_N 800
struct S{
int x, y;
bool operator ==(const S &a)const{
return (x==a.x) && (y==a.y);
}
};
using namespace std;
int N, K;
string str[MAX_N+1];
bool type[MAX_N+1][MAX_N+1];
S M, D, H;
bool visited[MAX_N+1][MAX_N+1];
int dist[MAX_N+1][MAX_N+1];
bool chk(int t, int p){
S tmp={t, p};
if(tmp.x<0 || tmp.x>=N || tmp.y<0 || tmp.y>=N) return false;
if(visited[t][p]) return false;
if(type[t][p]==1) return false;
return true;
}
int dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};
deque<S> dq;
void BFS(){
dq.push_back(H);
visited[H.x][H.y]=true;
while(!dq.empty()){
S now = dq.front();
dq.pop_front();
for(int k=0; k<4; k++){
if(chk(now.x+dx[k], now.y+dy[k])){
dist[now.x+dx[k]][now.y+dy[k]]=dist[now.x][now.y]+1;
visited[now.x+dx[k]][now.y+dy[k]]=true;
dq.push_back({now.x+dx[k], now.y+dy[k]});
}
}
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
visited[i][j]=false;
}
}
}
bool can(int T){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
visited[i][j]=false;
}
}
int t=0;
dq.clear();
dq.push_back(M);
visited[M.x][M.y]=true;
while(!dq.empty()){
int sz=(int)dq.size();
while(sz--){
S now = dq.front();
dq.pop_front();
if(dist[now.x][now.y]>T){
if(now==D) return true;
for(int k=0; k<4; k++){
if(chk(now.x+dx[k], now.y+dy[k]) && dist[now.x+dx[k]][now.y+dy[k]]>T){
visited[now.x+dx[k]][now.y+dy[k]]=true;
dq.push_back({now.x+dx[k], now.y+dy[k]});
}
}
}
}
t++;
if(t==K){
t=0; T++;
}
}
return false;
}
int main(){
scanf("%d%d", &N, &K);
for(int i=0; i<N; i++){
cin>>str[i];
for(int j=0; j<N; j++){
if(str[i][j]=='T') type[i][j]=1;
else if(str[i][j]=='M') M={i, j};
else if(str[i][j]=='D') D={i, j};
else if(str[i][j]=='H') H={i, j};
}
}
BFS();
int s=1, e=N*N+1, mid;
while(s<e){
if(s==e-1){
if(can(e)){
s=e;
}else{
e=s;
}
}else{
mid=(s+e)/2;
if(can(mid)){
s=mid;
}else{
e=mid-1;
}
}
}
printf("%d", s);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
532 KB |
Output isn't correct |
6 |
Correct |
2 ms |
536 KB |
Output is correct |
7 |
Incorrect |
186 ms |
5660 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
9 |
Correct |
2 ms |
5660 KB |
Output is correct |
10 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
5660 KB |
Output isn't correct |
15 |
Correct |
2 ms |
5660 KB |
Output is correct |
16 |
Correct |
2 ms |
5660 KB |
Output is correct |
17 |
Correct |
2 ms |
5660 KB |
Output is correct |
18 |
Correct |
2 ms |
5660 KB |
Output is correct |
19 |
Correct |
2 ms |
5660 KB |
Output is correct |
20 |
Correct |
2 ms |
5660 KB |
Output is correct |
21 |
Correct |
2 ms |
5660 KB |
Output is correct |
22 |
Correct |
2 ms |
5660 KB |
Output is correct |
23 |
Correct |
2 ms |
5660 KB |
Output is correct |
24 |
Correct |
2 ms |
5660 KB |
Output is correct |
25 |
Correct |
2 ms |
5660 KB |
Output is correct |
26 |
Correct |
2 ms |
5660 KB |
Output is correct |
27 |
Correct |
2 ms |
5660 KB |
Output is correct |
28 |
Correct |
2 ms |
5660 KB |
Output is correct |
29 |
Correct |
2 ms |
5660 KB |
Output is correct |
30 |
Correct |
2 ms |
5660 KB |
Output is correct |
31 |
Correct |
2 ms |
5660 KB |
Output is correct |
32 |
Correct |
3 ms |
5660 KB |
Output is correct |
33 |
Correct |
9 ms |
5660 KB |
Output is correct |
34 |
Correct |
12 ms |
5660 KB |
Output is correct |
35 |
Incorrect |
30 ms |
5660 KB |
Output isn't correct |
36 |
Correct |
11 ms |
5660 KB |
Output is correct |
37 |
Correct |
15 ms |
5660 KB |
Output is correct |
38 |
Incorrect |
36 ms |
5660 KB |
Output isn't correct |
39 |
Correct |
13 ms |
5660 KB |
Output is correct |
40 |
Correct |
20 ms |
5660 KB |
Output is correct |
41 |
Incorrect |
48 ms |
5660 KB |
Output isn't correct |
42 |
Correct |
16 ms |
6088 KB |
Output is correct |
43 |
Correct |
21 ms |
6364 KB |
Output is correct |
44 |
Incorrect |
62 ms |
6584 KB |
Output isn't correct |
45 |
Correct |
20 ms |
7088 KB |
Output is correct |
46 |
Correct |
81 ms |
7488 KB |
Output is correct |
47 |
Incorrect |
69 ms |
7680 KB |
Output isn't correct |
48 |
Correct |
21 ms |
8360 KB |
Output is correct |
49 |
Correct |
36 ms |
8716 KB |
Output is correct |
50 |
Incorrect |
85 ms |
9072 KB |
Output isn't correct |
51 |
Correct |
25 ms |
9812 KB |
Output is correct |
52 |
Correct |
39 ms |
10228 KB |
Output is correct |
53 |
Incorrect |
101 ms |
10644 KB |
Output isn't correct |
54 |
Correct |
27 ms |
11320 KB |
Output is correct |
55 |
Correct |
50 ms |
11800 KB |
Output is correct |
56 |
Incorrect |
141 ms |
12408 KB |
Output isn't correct |
57 |
Correct |
32 ms |
13272 KB |
Output is correct |
58 |
Correct |
48 ms |
13696 KB |
Output is correct |
59 |
Incorrect |
143 ms |
14248 KB |
Output isn't correct |
60 |
Correct |
36 ms |
15184 KB |
Output is correct |
61 |
Correct |
58 ms |
15828 KB |
Output is correct |
62 |
Incorrect |
172 ms |
16584 KB |
Output isn't correct |
63 |
Incorrect |
165 ms |
17140 KB |
Output isn't correct |
64 |
Incorrect |
252 ms |
17712 KB |
Output isn't correct |
65 |
Incorrect |
160 ms |
18344 KB |
Output isn't correct |
66 |
Incorrect |
177 ms |
19096 KB |
Output isn't correct |
67 |
Incorrect |
134 ms |
19596 KB |
Output isn't correct |
68 |
Incorrect |
264 ms |
20296 KB |
Output isn't correct |
69 |
Incorrect |
228 ms |
20852 KB |
Output isn't correct |
70 |
Incorrect |
201 ms |
21480 KB |
Output isn't correct |
71 |
Incorrect |
239 ms |
22236 KB |
Output isn't correct |
72 |
Incorrect |
55 ms |
22712 KB |
Output isn't correct |
73 |
Incorrect |
185 ms |
23380 KB |
Output isn't correct |
74 |
Incorrect |
200 ms |
24136 KB |
Output isn't correct |
75 |
Incorrect |
173 ms |
24712 KB |
Output isn't correct |
76 |
Incorrect |
181 ms |
25280 KB |
Output isn't correct |
77 |
Incorrect |
206 ms |
25948 KB |
Output isn't correct |
78 |
Incorrect |
237 ms |
26680 KB |
Output isn't correct |
79 |
Incorrect |
93 ms |
27344 KB |
Output isn't correct |
80 |
Incorrect |
112 ms |
27900 KB |
Output isn't correct |
81 |
Incorrect |
85 ms |
28476 KB |
Output isn't correct |
82 |
Incorrect |
174 ms |
29100 KB |
Output isn't correct |
83 |
Incorrect |
89 ms |
29728 KB |
Output isn't correct |
84 |
Incorrect |
76 ms |
30428 KB |
Output isn't correct |
85 |
Incorrect |
76 ms |
31000 KB |
Output isn't correct |
86 |
Incorrect |
184 ms |
31612 KB |
Output isn't correct |
87 |
Incorrect |
79 ms |
32368 KB |
Output isn't correct |
88 |
Incorrect |
93 ms |
32868 KB |
Output isn't correct |
89 |
Incorrect |
80 ms |
33500 KB |
Output isn't correct |
90 |
Incorrect |
80 ms |
34124 KB |
Output isn't correct |
91 |
Incorrect |
82 ms |
34752 KB |
Output isn't correct |
92 |
Incorrect |
76 ms |
35500 KB |
Output isn't correct |