#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,s;
vector<vector<int>> dist;
vector<vector<int>> grid;
pair<int,int> start;
pair<int,int> finish;
int debug=0;
int adebug = 0;
int INF=1000000007;
int gval(pair<int,int> cell) {
return grid[cell.first][cell.second];
}
bool bounds(pair<int,int> cell) {
return 0<=cell.first && cell.first<n && 0<=cell.second && cell.second<n;
}
vector<pair<int,int>> getnei(pair<int,int> st) {
if (!(gval(st)==0)) {
return {};
}
vector<pair<int,int>> V={{st.first-1,st.second},{st.first+1,st.second},{st.first,st.second+1},{st.first,st.second-1}};
vector<pair<int,int>> ans;
for (auto nei:V) {
if (bounds(nei) && ((gval(nei)==0) || gval(nei)==2)) {
ans.push_back(nei);
}
}
return ans;
}
bool BSTA(int time) {
//returns whether it can be done after waiting this time
vector<vector<int>> distance(n,vector<int>(n,INF));
vector<vector<int>> proc(n,vector<int>(n,0));
deque<pair<int,int>> visited={start};
distance[start.first][start.second] = 0;
if (time>=dist[start.first][start.second]) {
return 0;
}
while (visited.size()) {
pair<int,int> fi=visited[0];
visited.pop_front();
if (proc[fi.first][fi.second]) {
continue;
}
proc[fi.first][fi.second] = 1;
for (auto j:getnei(fi)) {
int bdist=time+(distance[fi.first][fi.second]+1)/s;
//it's the total number of steps the honey has to walk to reach this cell
if(debug) cout << fi.first << " " << fi.second << " " << j.first << " " << j.second << " "<<bdist << " " <<dist[fi.first][fi.second] << endl;
if (bdist>=(dist[j.first][j.second])) {
continue;
}
distance[j.first][j.second] = min(distance[j.first][j.second],distance[fi.first][fi.second]+1);
visited.push_back(j);
}
}
if(debug || adebug) cout << "BSTA TRYING METHOD CALCULATION TO FIND THE ANSWER" << " " << time << endl;
int b=distance[finish.first][finish.second] < 3*n*n;
if (debug || adebug) cout <<b << " " << distance[finish.first][finish.second] << endl;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if(debug) cout << distance[i][j] <<" ";
}
if(debug) cout << endl;
}
return distance[finish.first][finish.second] < 3*n*n;
}
vector<pair<int,int>> adjlistbee(pair<int,int> st) {
if (!(gval(st)==0 || gval(st)==3)) {
return {};
}
vector<pair<int,int>> V={{st.first-1,st.second},{st.first+1,st.second},{st.first,st.second+1},{st.first,st.second-1}};
vector<pair<int,int>> ans;
for (auto nei:V) {
if (bounds(nei) && gval(nei)==0) {
ans.push_back(nei);
}
}
return ans;
}
signed main() {
if (debug || adebug) freopen("input.txt","r",stdin);
if (debug || adebug) freopen("output.txt","w",stdout);
cin >> n >> s;
dist=vector<vector<int>>(n,vector<int>(n,INF));
grid=vector<vector<int>>(n,vector<int>(n,-1));
vector<vector<int>> proc(n,vector<int>(n,0));
vector<pair<int,int>> hives;
deque<pair<int,int>> visited;
for (int i=0; i<n; i++) {
string st;
cin >> st;
//if(debug) cout << st << endl;
for (int j=0; j<n; j++) {
//if(debug) cout << i <<" " << j << " " << st[j] << endl;
int ans;
if (st[j]=='G') {
ans=0;
}
else if (st[j]=='M') {
ans=0;
start={i,j};
}
else if (st[j]=='D') {
ans=2;
finish={i,j};
}
else if (st[j]=='T') {
ans=1;
}
else {
//assert(st[j]=='H');
ans=3;
hives.push_back({i,j});
dist[i][j] = 0;
visited.push_back({i,j});
}
grid[i][j] = ans;
//if(debug) cout << i << " " << j << endl;
}
}
//a cell is available to bees if the cell is 0 or 3, 3 denotes starting hives
//a cell is available to mecho if the dist condition holds and the cell isn't a tree
while (visited.size()) {
pair<int,int> fi=visited[0];
visited.pop_front();
if (proc[fi.first][fi.second]) {
continue;
}
proc[fi.first][fi.second] = 1;
for (auto j:adjlistbee(fi)) {
dist[j.first][j.second] = min(dist[j.first][j.second],dist[fi.first][fi.second]+1);
visited.push_back(j);
}
}
if(debug) cout << "DISTS"<< endl;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
//cout << dist[i][j] <<" ";
}
if(debug) cout << endl;
}
int l=0;
int r=n*n*1;
while (l<r) {
int m=(l+r)/2;
if (!BSTA(m)) {
l=l;
r=m;
}
else {
l=m+1;
r=r;
}
}
if (l==0 && !BSTA(l)) {
cout << -1 << endl;
return 0;
}
while (l>0 && !BSTA(l)) {
l--;
}
while (BSTA(l+1)) {
l++;
}
if (l==0 && !BSTA(l)) {
if(debug) cout << -1 << endl;
return 0;
}
cout << l << endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:93:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | if (debug || adebug) freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:94:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | if (debug || adebug) freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
628 ms |
25908 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
532 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
23 ms |
5404 KB |
Output is correct |
34 |
Correct |
27 ms |
5512 KB |
Output is correct |
35 |
Correct |
124 ms |
5384 KB |
Output is correct |
36 |
Correct |
30 ms |
6932 KB |
Output is correct |
37 |
Correct |
33 ms |
6916 KB |
Output is correct |
38 |
Correct |
177 ms |
6912 KB |
Output is correct |
39 |
Correct |
38 ms |
8604 KB |
Output is correct |
40 |
Correct |
41 ms |
8528 KB |
Output is correct |
41 |
Correct |
227 ms |
8540 KB |
Output is correct |
42 |
Correct |
46 ms |
10508 KB |
Output is correct |
43 |
Correct |
52 ms |
10620 KB |
Output is correct |
44 |
Correct |
282 ms |
10552 KB |
Output is correct |
45 |
Correct |
59 ms |
12640 KB |
Output is correct |
46 |
Correct |
61 ms |
12656 KB |
Output is correct |
47 |
Correct |
358 ms |
12856 KB |
Output is correct |
48 |
Correct |
71 ms |
14980 KB |
Output is correct |
49 |
Correct |
75 ms |
14960 KB |
Output is correct |
50 |
Correct |
423 ms |
14956 KB |
Output is correct |
51 |
Correct |
81 ms |
17540 KB |
Output is correct |
52 |
Correct |
90 ms |
17416 KB |
Output is correct |
53 |
Correct |
502 ms |
17524 KB |
Output is correct |
54 |
Correct |
93 ms |
20208 KB |
Output is correct |
55 |
Correct |
103 ms |
20296 KB |
Output is correct |
56 |
Correct |
621 ms |
20284 KB |
Output is correct |
57 |
Correct |
109 ms |
23148 KB |
Output is correct |
58 |
Correct |
127 ms |
23144 KB |
Output is correct |
59 |
Correct |
698 ms |
23044 KB |
Output is correct |
60 |
Correct |
130 ms |
26288 KB |
Output is correct |
61 |
Correct |
136 ms |
26156 KB |
Output is correct |
62 |
Correct |
891 ms |
26168 KB |
Output is correct |
63 |
Correct |
461 ms |
26112 KB |
Output is correct |
64 |
Correct |
641 ms |
26208 KB |
Output is correct |
65 |
Correct |
640 ms |
26496 KB |
Output is correct |
66 |
Correct |
495 ms |
26144 KB |
Output is correct |
67 |
Correct |
436 ms |
26204 KB |
Output is correct |
68 |
Correct |
249 ms |
26300 KB |
Output is correct |
69 |
Correct |
197 ms |
26192 KB |
Output is correct |
70 |
Correct |
184 ms |
26172 KB |
Output is correct |
71 |
Correct |
184 ms |
26276 KB |
Output is correct |
72 |
Correct |
143 ms |
26272 KB |
Output is correct |
73 |
Correct |
227 ms |
26288 KB |
Output is correct |
74 |
Correct |
376 ms |
26380 KB |
Output is correct |
75 |
Correct |
421 ms |
26328 KB |
Output is correct |
76 |
Correct |
405 ms |
26552 KB |
Output is correct |
77 |
Correct |
432 ms |
26320 KB |
Output is correct |
78 |
Correct |
537 ms |
26488 KB |
Output is correct |
79 |
Correct |
332 ms |
26300 KB |
Output is correct |
80 |
Correct |
372 ms |
26220 KB |
Output is correct |
81 |
Correct |
476 ms |
26184 KB |
Output is correct |
82 |
Correct |
383 ms |
26224 KB |
Output is correct |
83 |
Correct |
548 ms |
26420 KB |
Output is correct |
84 |
Correct |
487 ms |
26284 KB |
Output is correct |
85 |
Correct |
453 ms |
26272 KB |
Output is correct |
86 |
Correct |
483 ms |
26412 KB |
Output is correct |
87 |
Correct |
508 ms |
26292 KB |
Output is correct |
88 |
Correct |
582 ms |
26320 KB |
Output is correct |
89 |
Correct |
543 ms |
26232 KB |
Output is correct |
90 |
Correct |
584 ms |
26260 KB |
Output is correct |
91 |
Correct |
588 ms |
26276 KB |
Output is correct |
92 |
Correct |
544 ms |
26472 KB |
Output is correct |