#include<bits/stdc++.h>
using namespace std;
int dx[] = {0,1,0,-1} , dy[] = {1,0,-1,0} ,x ,y , st ,N , si , sj , fi , fj;
const int n=801;
char s[n][n];
vector<vector<int>>timebee(n , vector<int>(n,INT_MAX));
vector<vector<int>>timebear(n , vector<int>(n,INT_MAX));
vector<vector<int>>visbee(n , vector<int>(n,0));
queue<pair<pair<int,int>,int>>qbear,qbee;
int check(int m)
{
if(m>=timebee[si][sj])return 0;
while(!qbear.empty())qbear.pop();
for(int i=0 ; i<N ; i++)for(int j=0 ; j<N ; j++)timebear[i][j]=INT_MAX;
vector<vector<int>>visbear(N , vector<int>(N,0));
qbear.push({{si,sj},0});
timebear[si][sj] = m;
visbear[si][sj] = 1;
while(!qbear.empty())
{
int r = qbear.front().first.first , c = qbear.front().first.second , time = qbear.front().second;
qbear.pop();
for(int i=0 ; i<4 ; i++)
{
int nr = r+dx[i];
int nc = c+dy[i];
if(nr<0 or nr>=N or nc<0 or nc>=N or s[nr][nc]=='T' or visbear[nr][nc]==1)continue;
if((time+1)%st==0)timebear[nr][nc]=timebear[r][c]+1;
else timebear[nr][nc]=timebear[r][c];
if(timebee[nr][nc]<=timebear[nr][nc])continue;
visbear[nr][nc]=1;
qbear.push({{nr,nc},time+1});
}
}
if(timebear[fi][fj]!=INT_MAX)return 1;
else return 0;
}
int main()
{
cin >> N >> st;
for(int i=0 ; i<N ; i++)
{
for(int j=0 ; j<N ; j++)
{
cin >> s[i][j];
if(s[i][j]=='M')
{
si=i;
sj=j;
}
if(s[i][j]=='D')
{
fi=i;
fj=j;
}
if(s[i][j]=='H')
{
qbee.push({{i,j},0});
visbee[i][j] = 1;
timebee[i][j]=0;
}
}
}
while(!qbee.empty())
{
int r = qbee.front().first.first , c = qbee.front().first.second , time = qbee.front().second;
qbee.pop();
for(int i=0 ; i<4 ; i++)
{
int nr = r+dx[i];
int nc = c+dy[i];
if(nr<0 or nr>=N or nc<0 or nc>=N or s[nr][nc]=='D' or s[nr][nc]=='T' or visbee[nr][nc])continue;
visbee[nr][nc]=1;
timebee[nr][nc] = time+1;
qbee.push({{nr,nc},timebee[nr][nc]});
}
}
int l=0 ,r=N*N , m ,ans=-1;
while(l<=r)
{
m=l+(r-l)/2;
if(check(m))ans=m , l=m+1;
else r=m-1;
}
cout << ans ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8028 KB |
Output is correct |
2 |
Correct |
4 ms |
8028 KB |
Output is correct |
3 |
Correct |
4 ms |
8028 KB |
Output is correct |
4 |
Correct |
4 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
4 ms |
8028 KB |
Output is correct |
7 |
Correct |
94 ms |
11220 KB |
Output is correct |
8 |
Correct |
4 ms |
8028 KB |
Output is correct |
9 |
Correct |
4 ms |
8024 KB |
Output is correct |
10 |
Correct |
4 ms |
8028 KB |
Output is correct |
11 |
Correct |
4 ms |
8028 KB |
Output is correct |
12 |
Correct |
4 ms |
8028 KB |
Output is correct |
13 |
Correct |
4 ms |
8028 KB |
Output is correct |
14 |
Correct |
4 ms |
8028 KB |
Output is correct |
15 |
Correct |
4 ms |
8060 KB |
Output is correct |
16 |
Correct |
4 ms |
8028 KB |
Output is correct |
17 |
Correct |
4 ms |
8028 KB |
Output is correct |
18 |
Correct |
4 ms |
8028 KB |
Output is correct |
19 |
Correct |
4 ms |
8028 KB |
Output is correct |
20 |
Correct |
4 ms |
8028 KB |
Output is correct |
21 |
Correct |
5 ms |
8028 KB |
Output is correct |
22 |
Correct |
4 ms |
8088 KB |
Output is correct |
23 |
Correct |
4 ms |
8028 KB |
Output is correct |
24 |
Correct |
4 ms |
8028 KB |
Output is correct |
25 |
Correct |
4 ms |
8028 KB |
Output is correct |
26 |
Correct |
5 ms |
8028 KB |
Output is correct |
27 |
Correct |
4 ms |
8028 KB |
Output is correct |
28 |
Correct |
4 ms |
8028 KB |
Output is correct |
29 |
Correct |
4 ms |
8028 KB |
Output is correct |
30 |
Correct |
5 ms |
8028 KB |
Output is correct |
31 |
Correct |
4 ms |
8028 KB |
Output is correct |
32 |
Correct |
4 ms |
8028 KB |
Output is correct |
33 |
Correct |
12 ms |
8816 KB |
Output is correct |
34 |
Correct |
13 ms |
8832 KB |
Output is correct |
35 |
Correct |
25 ms |
8808 KB |
Output is correct |
36 |
Correct |
14 ms |
8792 KB |
Output is correct |
37 |
Correct |
19 ms |
9052 KB |
Output is correct |
38 |
Correct |
33 ms |
8824 KB |
Output is correct |
39 |
Correct |
25 ms |
9192 KB |
Output is correct |
40 |
Correct |
18 ms |
9200 KB |
Output is correct |
41 |
Correct |
44 ms |
9168 KB |
Output is correct |
42 |
Correct |
23 ms |
9440 KB |
Output is correct |
43 |
Correct |
22 ms |
9432 KB |
Output is correct |
44 |
Correct |
50 ms |
9412 KB |
Output is correct |
45 |
Correct |
36 ms |
9696 KB |
Output is correct |
46 |
Correct |
26 ms |
9676 KB |
Output is correct |
47 |
Correct |
58 ms |
9660 KB |
Output is correct |
48 |
Correct |
32 ms |
9976 KB |
Output is correct |
49 |
Correct |
29 ms |
9956 KB |
Output is correct |
50 |
Correct |
69 ms |
9928 KB |
Output is correct |
51 |
Correct |
36 ms |
10268 KB |
Output is correct |
52 |
Correct |
37 ms |
10328 KB |
Output is correct |
53 |
Correct |
90 ms |
10100 KB |
Output is correct |
54 |
Correct |
41 ms |
10580 KB |
Output is correct |
55 |
Correct |
40 ms |
10536 KB |
Output is correct |
56 |
Correct |
101 ms |
10556 KB |
Output is correct |
57 |
Correct |
46 ms |
10868 KB |
Output is correct |
58 |
Correct |
70 ms |
10868 KB |
Output is correct |
59 |
Correct |
111 ms |
10876 KB |
Output is correct |
60 |
Correct |
52 ms |
11248 KB |
Output is correct |
61 |
Correct |
67 ms |
11188 KB |
Output is correct |
62 |
Correct |
161 ms |
11216 KB |
Output is correct |
63 |
Correct |
127 ms |
11216 KB |
Output is correct |
64 |
Correct |
234 ms |
11204 KB |
Output is correct |
65 |
Correct |
160 ms |
11204 KB |
Output is correct |
66 |
Correct |
128 ms |
11240 KB |
Output is correct |
67 |
Correct |
125 ms |
11212 KB |
Output is correct |
68 |
Correct |
72 ms |
11196 KB |
Output is correct |
69 |
Correct |
78 ms |
11244 KB |
Output is correct |
70 |
Correct |
58 ms |
11224 KB |
Output is correct |
71 |
Correct |
69 ms |
11232 KB |
Output is correct |
72 |
Correct |
50 ms |
11188 KB |
Output is correct |
73 |
Correct |
62 ms |
11088 KB |
Output is correct |
74 |
Correct |
95 ms |
11364 KB |
Output is correct |
75 |
Correct |
88 ms |
11264 KB |
Output is correct |
76 |
Correct |
79 ms |
11236 KB |
Output is correct |
77 |
Correct |
75 ms |
11244 KB |
Output is correct |
78 |
Correct |
90 ms |
11548 KB |
Output is correct |
79 |
Correct |
67 ms |
11244 KB |
Output is correct |
80 |
Correct |
77 ms |
11228 KB |
Output is correct |
81 |
Correct |
87 ms |
11248 KB |
Output is correct |
82 |
Correct |
80 ms |
11240 KB |
Output is correct |
83 |
Correct |
86 ms |
11200 KB |
Output is correct |
84 |
Correct |
81 ms |
11240 KB |
Output is correct |
85 |
Correct |
89 ms |
11220 KB |
Output is correct |
86 |
Correct |
90 ms |
11220 KB |
Output is correct |
87 |
Correct |
91 ms |
11224 KB |
Output is correct |
88 |
Correct |
90 ms |
11224 KB |
Output is correct |
89 |
Correct |
90 ms |
11228 KB |
Output is correct |
90 |
Correct |
96 ms |
11224 KB |
Output is correct |
91 |
Correct |
90 ms |
11224 KB |
Output is correct |
92 |
Correct |
101 ms |
11208 KB |
Output is correct |