#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]) 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]) && !(now.x+dx[k]==D.x && now.y+dy[k]==D.y)){
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=0, 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 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
556 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
556 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
556 KB |
Output isn't correct |
5 |
Correct |
1 ms |
556 KB |
Output is correct |
6 |
Incorrect |
2 ms |
556 KB |
Output isn't correct |
7 |
Incorrect |
222 ms |
5012 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
13 |
Correct |
2 ms |
5012 KB |
Output is correct |
14 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
15 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
22 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
5012 KB |
Output isn't correct |
28 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
29 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
31 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
32 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
33 |
Incorrect |
20 ms |
5012 KB |
Output isn't correct |
34 |
Incorrect |
30 ms |
5012 KB |
Output isn't correct |
35 |
Incorrect |
28 ms |
5012 KB |
Output isn't correct |
36 |
Incorrect |
26 ms |
5012 KB |
Output isn't correct |
37 |
Incorrect |
39 ms |
5012 KB |
Output isn't correct |
38 |
Incorrect |
37 ms |
5012 KB |
Output isn't correct |
39 |
Incorrect |
33 ms |
5012 KB |
Output isn't correct |
40 |
Incorrect |
50 ms |
5012 KB |
Output isn't correct |
41 |
Incorrect |
50 ms |
5012 KB |
Output isn't correct |
42 |
Incorrect |
41 ms |
5012 KB |
Output isn't correct |
43 |
Incorrect |
62 ms |
5012 KB |
Output isn't correct |
44 |
Incorrect |
58 ms |
5012 KB |
Output isn't correct |
45 |
Incorrect |
49 ms |
5012 KB |
Output isn't correct |
46 |
Incorrect |
75 ms |
5012 KB |
Output isn't correct |
47 |
Incorrect |
72 ms |
5012 KB |
Output isn't correct |
48 |
Incorrect |
58 ms |
5012 KB |
Output isn't correct |
49 |
Incorrect |
89 ms |
5012 KB |
Output isn't correct |
50 |
Incorrect |
81 ms |
5012 KB |
Output isn't correct |
51 |
Incorrect |
71 ms |
5012 KB |
Output isn't correct |
52 |
Incorrect |
113 ms |
5012 KB |
Output isn't correct |
53 |
Incorrect |
111 ms |
5012 KB |
Output isn't correct |
54 |
Incorrect |
81 ms |
5012 KB |
Output isn't correct |
55 |
Incorrect |
131 ms |
5012 KB |
Output isn't correct |
56 |
Incorrect |
121 ms |
5012 KB |
Output isn't correct |
57 |
Incorrect |
90 ms |
5012 KB |
Output isn't correct |
58 |
Incorrect |
143 ms |
5012 KB |
Output isn't correct |
59 |
Incorrect |
146 ms |
5012 KB |
Output isn't correct |
60 |
Incorrect |
106 ms |
5100 KB |
Output isn't correct |
61 |
Incorrect |
178 ms |
5160 KB |
Output isn't correct |
62 |
Incorrect |
167 ms |
5168 KB |
Output isn't correct |
63 |
Correct |
204 ms |
5168 KB |
Output is correct |
64 |
Incorrect |
224 ms |
5168 KB |
Output isn't correct |
65 |
Incorrect |
200 ms |
5168 KB |
Output isn't correct |
66 |
Incorrect |
207 ms |
5244 KB |
Output isn't correct |
67 |
Incorrect |
152 ms |
5244 KB |
Output isn't correct |
68 |
Correct |
236 ms |
5244 KB |
Output is correct |
69 |
Incorrect |
243 ms |
5244 KB |
Output isn't correct |
70 |
Incorrect |
238 ms |
5244 KB |
Output isn't correct |
71 |
Incorrect |
229 ms |
5300 KB |
Output isn't correct |
72 |
Incorrect |
243 ms |
5300 KB |
Output isn't correct |
73 |
Incorrect |
190 ms |
5300 KB |
Output isn't correct |
74 |
Incorrect |
197 ms |
5300 KB |
Output isn't correct |
75 |
Incorrect |
224 ms |
5300 KB |
Output isn't correct |
76 |
Incorrect |
200 ms |
5300 KB |
Output isn't correct |
77 |
Incorrect |
201 ms |
5300 KB |
Output isn't correct |
78 |
Incorrect |
201 ms |
5300 KB |
Output isn't correct |
79 |
Incorrect |
212 ms |
5300 KB |
Output isn't correct |
80 |
Incorrect |
190 ms |
5300 KB |
Output isn't correct |
81 |
Incorrect |
187 ms |
5300 KB |
Output isn't correct |
82 |
Incorrect |
199 ms |
5300 KB |
Output isn't correct |
83 |
Correct |
201 ms |
5300 KB |
Output is correct |
84 |
Incorrect |
213 ms |
5300 KB |
Output isn't correct |
85 |
Incorrect |
206 ms |
5300 KB |
Output isn't correct |
86 |
Incorrect |
247 ms |
5300 KB |
Output isn't correct |
87 |
Incorrect |
210 ms |
5300 KB |
Output isn't correct |
88 |
Correct |
201 ms |
5300 KB |
Output is correct |
89 |
Incorrect |
200 ms |
5304 KB |
Output isn't correct |
90 |
Incorrect |
206 ms |
5304 KB |
Output isn't correct |
91 |
Incorrect |
228 ms |
5304 KB |
Output isn't correct |
92 |
Incorrect |
205 ms |
5304 KB |
Output isn't correct |