#include <bits/stdc++.h>
using namespace std;
struct node{
int i;
int j;
node(){}
node(int I,int J){
i=I;
j=J;
}
};
queue<node> Q;
char forest[805][805];
int bees[805][805];
int dist[805][805];
int energy[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,s;
void bfsBees(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
bees[i][j]=INT_MAX;
if(forest[i][j]=='H'){
Q.push(node(i,j));
bees[i][j]=0;
}
}
}
while(!Q.empty()){
node u=Q.front();
Q.pop();
for(int mov=0;mov<4;mov++){
int i=u.i+dx[mov];
int j=u.j+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T' || forest[i][j]=='D') continue;
if(bees[i][j]==INT_MAX){
bees[i][j]=bees[u.i][u.j]+1;
Q.push(node(i,j));
}
}
}
}
node mecho;
node home;
bool bfsMecho(int init){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=INT_MAX;
}
}
dist[mecho.i][mecho.j]=init;
energy[mecho.i][mecho.j]=0;
Q.push(mecho);
while(!Q.empty()){
node u=Q.front();
Q.pop();
if(bees[u.i][u.j]==dist[u.i][u.j] && energy[u.i][u.j]==0) continue;
for(int mov=0;mov<4;mov++){
int i=u.i+dx[mov];
int j=u.j+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T') continue;
if(dist[i][j]==INT_MAX){
int change=(energy[u.i][u.j]>0)? 0 : 1;
dist[i][j]=dist[u.i][u.j]+change;
if(bees[i][j]<dist[i][j]) continue;
energy[i][j]=(energy[u.i][u.j]>0)? energy[u.i][u.j]-1 : s-1;
Q.push(node(i,j));
}
}
}
return dist[home.i][home.j]!=INT_MAX;
}
int binarySearch(){
int k=-1;
int L=0,R=3*n;
while(L<=R){
int mid=(L+R)/2;
if(bfsMecho(mid)){
k=mid;
L=mid+1;
}
else{
R=mid-1;
}
}
return k;
}
int main()
{
cin>>n>>s;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>forest[i][j];
if(forest[i][j]=='M'){
mecho=node(i,j);
}
else if(forest[i][j]=='D'){
home=node(i,j);
}
}
}
bfsBees();
int ans=binarySearch();
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
149 ms |
8300 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
876 KB |
Output is correct |
15 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
748 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
748 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
876 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
876 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
876 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
876 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
1004 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
1004 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
1004 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
1004 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
1132 KB |
Output isn't correct |
33 |
Incorrect |
57 ms |
3436 KB |
Output isn't correct |
34 |
Incorrect |
26 ms |
4076 KB |
Output isn't correct |
35 |
Correct |
31 ms |
4088 KB |
Output is correct |
36 |
Incorrect |
26 ms |
3948 KB |
Output isn't correct |
37 |
Incorrect |
34 ms |
4588 KB |
Output isn't correct |
38 |
Correct |
41 ms |
4588 KB |
Output is correct |
39 |
Incorrect |
38 ms |
4460 KB |
Output isn't correct |
40 |
Incorrect |
48 ms |
5100 KB |
Output isn't correct |
41 |
Correct |
50 ms |
5100 KB |
Output is correct |
42 |
Incorrect |
41 ms |
4972 KB |
Output isn't correct |
43 |
Incorrect |
59 ms |
5740 KB |
Output isn't correct |
44 |
Correct |
64 ms |
5868 KB |
Output is correct |
45 |
Incorrect |
48 ms |
5356 KB |
Output isn't correct |
46 |
Incorrect |
64 ms |
6252 KB |
Output isn't correct |
47 |
Correct |
85 ms |
6252 KB |
Output is correct |
48 |
Incorrect |
57 ms |
5868 KB |
Output isn't correct |
49 |
Incorrect |
75 ms |
6764 KB |
Output isn't correct |
50 |
Correct |
104 ms |
7020 KB |
Output is correct |
51 |
Incorrect |
68 ms |
6380 KB |
Output isn't correct |
52 |
Incorrect |
88 ms |
7532 KB |
Output isn't correct |
53 |
Correct |
125 ms |
7532 KB |
Output is correct |
54 |
Incorrect |
80 ms |
6892 KB |
Output isn't correct |
55 |
Incorrect |
109 ms |
7916 KB |
Output isn't correct |
56 |
Correct |
140 ms |
8044 KB |
Output is correct |
57 |
Incorrect |
91 ms |
7404 KB |
Output isn't correct |
58 |
Incorrect |
123 ms |
8556 KB |
Output isn't correct |
59 |
Correct |
163 ms |
8556 KB |
Output is correct |
60 |
Incorrect |
105 ms |
7916 KB |
Output isn't correct |
61 |
Incorrect |
142 ms |
9196 KB |
Output isn't correct |
62 |
Correct |
187 ms |
9324 KB |
Output is correct |
63 |
Correct |
149 ms |
9288 KB |
Output is correct |
64 |
Correct |
221 ms |
9196 KB |
Output is correct |
65 |
Correct |
240 ms |
9196 KB |
Output is correct |
66 |
Correct |
170 ms |
9196 KB |
Output is correct |
67 |
Correct |
163 ms |
9344 KB |
Output is correct |
68 |
Correct |
87 ms |
9196 KB |
Output is correct |
69 |
Correct |
84 ms |
9196 KB |
Output is correct |
70 |
Correct |
86 ms |
9196 KB |
Output is correct |
71 |
Correct |
81 ms |
9324 KB |
Output is correct |
72 |
Correct |
69 ms |
7916 KB |
Output is correct |
73 |
Correct |
72 ms |
9452 KB |
Output is correct |
74 |
Correct |
110 ms |
9452 KB |
Output is correct |
75 |
Correct |
118 ms |
9452 KB |
Output is correct |
76 |
Correct |
111 ms |
9452 KB |
Output is correct |
77 |
Correct |
105 ms |
9452 KB |
Output is correct |
78 |
Correct |
122 ms |
8172 KB |
Output is correct |
79 |
Correct |
93 ms |
7788 KB |
Output is correct |
80 |
Correct |
101 ms |
7788 KB |
Output is correct |
81 |
Correct |
117 ms |
7916 KB |
Output is correct |
82 |
Correct |
102 ms |
7916 KB |
Output is correct |
83 |
Correct |
127 ms |
8044 KB |
Output is correct |
84 |
Correct |
122 ms |
8044 KB |
Output is correct |
85 |
Correct |
115 ms |
8088 KB |
Output is correct |
86 |
Correct |
125 ms |
8172 KB |
Output is correct |
87 |
Correct |
129 ms |
8172 KB |
Output is correct |
88 |
Correct |
130 ms |
8428 KB |
Output is correct |
89 |
Correct |
129 ms |
8044 KB |
Output is correct |
90 |
Correct |
132 ms |
8044 KB |
Output is correct |
91 |
Correct |
141 ms |
8172 KB |
Output is correct |
92 |
Correct |
131 ms |
8172 KB |
Output is correct |