#include <bits/stdc++.h>
#define DIM 810
using namespace std;
char s[DIM];
int a[DIM][DIM],b[DIM][DIM],dist[DIM][DIM],f[DIM*DIM];
deque <pair<int,int> > c,aux,d;
int di[] = {-1,1,0,0};
int dj[] = {0,0,-1,1};
int n,nr,i,j,xstart,ystart,xfin,yfin;
inline int inmat (int i, int j){
if (i >= 1 && j >= 1 && i <= n && j <= n)
return 1;
return 0;
}
int verif (int val){
/// mai intai vad cat se extind albinele dupa val secunde
c.clear();
memset (f,0,sizeof f);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++){
b[i][j] = a[i][j];
if (a[i][j] == 1)
c.push_back(make_pair(i,j));
}
for (i=1;i<=val;i++){
aux.clear();
for (auto it : c){
int ic = it.first, jc = it.second;
for (int dir=0;dir<=3;dir++){
int iv = ic + di[dir];
int jv = jc + dj[dir];
if (inmat (iv,jv) && b[iv][jv] == 0){
aux.push_back(make_pair(iv,jv));
b[iv][jv] = 1;
}}}
c = aux;
f[i] = 1;
}
if (b[xstart][ystart])
return 0;
/// d - lista cu toate locurile in care pot sa ajung pana la secunda asta
memset (dist,0,sizeof dist);
d.clear();
d.push_back(make_pair(xstart,ystart));
dist[xstart][ystart] = 1;
while (!d.empty()){
int ic = d.front().first;
int jc = d.front().second;
d.pop_front();
if (dist[ic][jc] % nr == 0 && !f[dist[ic][jc] / nr + val]){
/// inseamna ca a mai trecut un minut si trb sa avansez cu albinele
f[dist[ic][jc] / nr + val] = 1;
aux.clear();
for (auto it : c){
int i = it.first, j = it.second;
for (int dir=0;dir<=3;dir++){
int iv = i + di[dir];
int jv = j + dj[dir];
if (inmat(iv,jv) && b[iv][jv] == 0){
aux.push_back(make_pair(iv,jv));
b[iv][jv] = 1;
}}}
c = aux;
}
for (int dir=0;dir<=3;dir++){
int iv = ic + di[dir];
int jv = jc + dj[dir];
if (inmat (iv,jv) && (b[iv][jv] == 0 || b[iv][jv] == 3) && !dist[iv][jv]){
if (iv == xfin && jv == yfin) /// am reusit sa ajung
return 1;
dist[iv][jv] = 1 + dist[ic][jc];
d.push_back(make_pair(iv,jv));
}}}
return 0;
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>nr;
for (i=1;i<=n;i++){
cin>>s+1;
for (j=1;j<=n;j++){
if (s[j] == 'T')
a[i][j] = 2;
if (s[j] == 'M'){
xstart = i, ystart = j;
//a[i][j] = 4;
}
if (s[j] == 'D'){
xfin = i, yfin = j;
a[i][j] = 3;
}
if (s[j] == 'H')
a[i][j] = 1;
}
}
/*for (i=1;i<=n;i++,cout<<endl)
for (j=1;j<=n;j++)
cout<<a[i][j]<<" ";
*/
int st = 0, dr = n*n, sol = -1;
while (st <= dr){
int mid = (st+dr)>>1;
if (verif (mid)){
sol = mid;
st = mid+1;
} else dr = mid-1;
}
cout<<sol;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:96:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin>>s+1;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5504 KB |
Output is correct |
2 |
Correct |
8 ms |
5504 KB |
Output is correct |
3 |
Correct |
7 ms |
5504 KB |
Output is correct |
4 |
Correct |
8 ms |
5504 KB |
Output is correct |
5 |
Correct |
8 ms |
5504 KB |
Output is correct |
6 |
Correct |
8 ms |
5504 KB |
Output is correct |
7 |
Correct |
380 ms |
9976 KB |
Output is correct |
8 |
Correct |
8 ms |
5504 KB |
Output is correct |
9 |
Correct |
8 ms |
5504 KB |
Output is correct |
10 |
Correct |
8 ms |
5504 KB |
Output is correct |
11 |
Correct |
7 ms |
5504 KB |
Output is correct |
12 |
Correct |
10 ms |
5888 KB |
Output is correct |
13 |
Correct |
9 ms |
5760 KB |
Output is correct |
14 |
Correct |
10 ms |
5888 KB |
Output is correct |
15 |
Correct |
9 ms |
5632 KB |
Output is correct |
16 |
Correct |
8 ms |
5632 KB |
Output is correct |
17 |
Correct |
9 ms |
5632 KB |
Output is correct |
18 |
Correct |
9 ms |
5632 KB |
Output is correct |
19 |
Correct |
9 ms |
5632 KB |
Output is correct |
20 |
Correct |
8 ms |
5632 KB |
Output is correct |
21 |
Correct |
10 ms |
5632 KB |
Output is correct |
22 |
Correct |
9 ms |
5760 KB |
Output is correct |
23 |
Correct |
10 ms |
5760 KB |
Output is correct |
24 |
Correct |
9 ms |
5760 KB |
Output is correct |
25 |
Correct |
13 ms |
5760 KB |
Output is correct |
26 |
Correct |
10 ms |
5888 KB |
Output is correct |
27 |
Correct |
10 ms |
5760 KB |
Output is correct |
28 |
Correct |
11 ms |
5888 KB |
Output is correct |
29 |
Correct |
10 ms |
5888 KB |
Output is correct |
30 |
Correct |
10 ms |
5888 KB |
Output is correct |
31 |
Correct |
11 ms |
5888 KB |
Output is correct |
32 |
Correct |
11 ms |
6016 KB |
Output is correct |
33 |
Correct |
74 ms |
7672 KB |
Output is correct |
34 |
Correct |
57 ms |
7672 KB |
Output is correct |
35 |
Correct |
63 ms |
7672 KB |
Output is correct |
36 |
Correct |
92 ms |
7928 KB |
Output is correct |
37 |
Correct |
77 ms |
7928 KB |
Output is correct |
38 |
Correct |
73 ms |
7928 KB |
Output is correct |
39 |
Correct |
111 ms |
8236 KB |
Output is correct |
40 |
Correct |
96 ms |
8312 KB |
Output is correct |
41 |
Correct |
97 ms |
8312 KB |
Output is correct |
42 |
Correct |
149 ms |
8568 KB |
Output is correct |
43 |
Correct |
117 ms |
8696 KB |
Output is correct |
44 |
Correct |
117 ms |
8572 KB |
Output is correct |
45 |
Correct |
183 ms |
8952 KB |
Output is correct |
46 |
Correct |
133 ms |
9068 KB |
Output is correct |
47 |
Correct |
135 ms |
9080 KB |
Output is correct |
48 |
Correct |
201 ms |
9208 KB |
Output is correct |
49 |
Correct |
169 ms |
9320 KB |
Output is correct |
50 |
Correct |
158 ms |
9208 KB |
Output is correct |
51 |
Correct |
248 ms |
9720 KB |
Output is correct |
52 |
Correct |
191 ms |
9592 KB |
Output is correct |
53 |
Correct |
186 ms |
9592 KB |
Output is correct |
54 |
Correct |
291 ms |
10108 KB |
Output is correct |
55 |
Correct |
237 ms |
9952 KB |
Output is correct |
56 |
Correct |
223 ms |
9976 KB |
Output is correct |
57 |
Correct |
338 ms |
10232 KB |
Output is correct |
58 |
Correct |
252 ms |
10360 KB |
Output is correct |
59 |
Correct |
261 ms |
10360 KB |
Output is correct |
60 |
Correct |
371 ms |
10488 KB |
Output is correct |
61 |
Correct |
281 ms |
10488 KB |
Output is correct |
62 |
Correct |
306 ms |
10744 KB |
Output is correct |
63 |
Correct |
358 ms |
10488 KB |
Output is correct |
64 |
Correct |
402 ms |
10588 KB |
Output is correct |
65 |
Correct |
361 ms |
10488 KB |
Output is correct |
66 |
Correct |
388 ms |
10616 KB |
Output is correct |
67 |
Correct |
367 ms |
10488 KB |
Output is correct |
68 |
Correct |
351 ms |
10744 KB |
Output is correct |
69 |
Correct |
323 ms |
10744 KB |
Output is correct |
70 |
Correct |
335 ms |
10744 KB |
Output is correct |
71 |
Correct |
356 ms |
10616 KB |
Output is correct |
72 |
Correct |
383 ms |
10744 KB |
Output is correct |
73 |
Correct |
309 ms |
11128 KB |
Output is correct |
74 |
Correct |
367 ms |
11256 KB |
Output is correct |
75 |
Correct |
391 ms |
11136 KB |
Output is correct |
76 |
Correct |
384 ms |
11256 KB |
Output is correct |
77 |
Correct |
385 ms |
11132 KB |
Output is correct |
78 |
Correct |
380 ms |
11128 KB |
Output is correct |
79 |
Correct |
387 ms |
11000 KB |
Output is correct |
80 |
Correct |
393 ms |
11080 KB |
Output is correct |
81 |
Correct |
385 ms |
11132 KB |
Output is correct |
82 |
Correct |
395 ms |
11004 KB |
Output is correct |
83 |
Correct |
393 ms |
11000 KB |
Output is correct |
84 |
Correct |
388 ms |
11032 KB |
Output is correct |
85 |
Correct |
404 ms |
11000 KB |
Output is correct |
86 |
Correct |
390 ms |
11128 KB |
Output is correct |
87 |
Correct |
392 ms |
10936 KB |
Output is correct |
88 |
Correct |
380 ms |
11000 KB |
Output is correct |
89 |
Correct |
378 ms |
10872 KB |
Output is correct |
90 |
Correct |
373 ms |
10876 KB |
Output is correct |
91 |
Correct |
380 ms |
10872 KB |
Output is correct |
92 |
Correct |
396 ms |
10872 KB |
Output is correct |