#include <bits/stdc++.h>
#define NMAX 100001
#define pb push_back
#define eb emplace_back
#define MOD 100003
#define nl '\n'
#define INF 2147483647
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int>
//#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
/*
*
*
================DEMONSTRATION===================
=====================END========================
*/
int n,s;
int mat[805][805];
int dist[805][805];
int bee[805][805];
int di[]={1,-1,0,0};
int dj[]={0,0,1,-1};
int i_s,j_s,i_e,j_e;
queue<pii>bq;
queue<tpl>q;
bool inmat(int i,int j)
{
return i>=1 && i<=n && j>=1 && j<=n;
}
void bear_bfs()
{
while(!q.empty())
{
auto [x,y,dis]=q.front();
q.pop();
// cout<<x<<" "<<y<<" "<<dist[x][y]<<nl;
if(x==i_e && y==j_e)
{
return;
}
for(int d=0;d<4;++d)
{
int inou = x + di[d];
int jnou = y + dj[d];
int ceva=0;
if(dis==1)
ceva=1;
if (inmat(inou, jnou)
&& (mat[inou][jnou] == 4 || mat[inou][jnou] == 3)
&& dist[x][y] + ceva < bee[inou][jnou]
&& dist[x][y] + ceva < dist[inou][jnou])
{
dist[inou][jnou] = dist[x][y] + ceva;
if(ceva)
q.push({inou, jnou,s});
else
q.push({inou,jnou,dis-1});
}
}
}
}
void bee_bfs()
{
while(!bq.empty())
{
auto [x,y]=bq.front();
bq.pop();
for(int d=0;d<4;++d)
{
int inou=x+di[d];
int jnou=y+dj[d];
if(inmat(inou,jnou) && bee[inou][jnou]>bee[x][y]+1 && mat[inou][jnou]==4)
{
bee[inou][jnou]=bee[x][y]+1;
bq.push({inou,jnou});
}
}
}
}
bool check(int sec)
{
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
dist[i][j]=INF;
}
}
while(!q.empty())
q.pop();
dist[i_s][j_s]=sec;
q.push({i_s,j_s,s});
bear_bfs();
return dist[i_e][j_e]!=INF;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>s;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
dist[i][j]=bee[i][j]=INF;
char x;
cin>>x;
if(x=='T')
mat[i][j]=1;
else if(x=='M')
{
i_s=i;
j_s=j;
mat[i][j]=2;
}
else if(x=='D')
{
i_e=i;
j_e=j;
mat[i][j]=3;
}
else if(x=='G')
{
mat[i][j]=4;
}
else
{
bee[i][j]=0;
bq.push({i,j});
mat[i][j]=5;
}
}
}
bee_bfs();
// for(int i=1;i<=n;++i)
// {
// for(int j=1;j<=n;++j)
// {
// cout<<bee[i][j]<<" ";
// }
// cout<<nl;
// }
int st=0;
int dr=1e9;
int ans=-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(check(mid))
{
ans=mid;
st=mid+1;
}
else
dr=mid-1;
}
cout<<ans;
return 0;
}
Compilation message
mecho.cpp:8: warning: "LLONG_MAX" redefined
8 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from mecho.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
mecho.cpp: In function 'void bear_bfs()':
mecho.cpp:40:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | auto [x,y,dis]=q.front();
| ^
mecho.cpp: In function 'void bee_bfs()':
mecho.cpp:74:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
74 | auto [x,y]=bq.front();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
70 ms |
8024 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
680 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
0 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
0 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
5 ms |
3676 KB |
Output is correct |
34 |
Correct |
5 ms |
3792 KB |
Output is correct |
35 |
Correct |
16 ms |
3804 KB |
Output is correct |
36 |
Correct |
6 ms |
4188 KB |
Output is correct |
37 |
Correct |
5 ms |
4188 KB |
Output is correct |
38 |
Correct |
21 ms |
4276 KB |
Output is correct |
39 |
Correct |
8 ms |
4700 KB |
Output is correct |
40 |
Correct |
7 ms |
4740 KB |
Output is correct |
41 |
Correct |
23 ms |
4752 KB |
Output is correct |
42 |
Correct |
8 ms |
5212 KB |
Output is correct |
43 |
Correct |
8 ms |
5212 KB |
Output is correct |
44 |
Correct |
37 ms |
5212 KB |
Output is correct |
45 |
Correct |
11 ms |
5464 KB |
Output is correct |
46 |
Correct |
10 ms |
5468 KB |
Output is correct |
47 |
Correct |
39 ms |
5464 KB |
Output is correct |
48 |
Correct |
12 ms |
5980 KB |
Output is correct |
49 |
Correct |
11 ms |
6088 KB |
Output is correct |
50 |
Correct |
46 ms |
6148 KB |
Output is correct |
51 |
Correct |
15 ms |
6492 KB |
Output is correct |
52 |
Correct |
13 ms |
6492 KB |
Output is correct |
53 |
Correct |
53 ms |
6640 KB |
Output is correct |
54 |
Correct |
16 ms |
7000 KB |
Output is correct |
55 |
Correct |
16 ms |
7004 KB |
Output is correct |
56 |
Correct |
60 ms |
7116 KB |
Output is correct |
57 |
Correct |
18 ms |
7580 KB |
Output is correct |
58 |
Correct |
22 ms |
7512 KB |
Output is correct |
59 |
Correct |
75 ms |
7572 KB |
Output is correct |
60 |
Correct |
21 ms |
8024 KB |
Output is correct |
61 |
Correct |
21 ms |
8028 KB |
Output is correct |
62 |
Correct |
81 ms |
8036 KB |
Output is correct |
63 |
Correct |
92 ms |
8036 KB |
Output is correct |
64 |
Correct |
140 ms |
8028 KB |
Output is correct |
65 |
Correct |
134 ms |
8024 KB |
Output is correct |
66 |
Correct |
99 ms |
8028 KB |
Output is correct |
67 |
Correct |
89 ms |
8036 KB |
Output is correct |
68 |
Correct |
52 ms |
8028 KB |
Output is correct |
69 |
Correct |
45 ms |
8028 KB |
Output is correct |
70 |
Correct |
34 ms |
8024 KB |
Output is correct |
71 |
Correct |
39 ms |
8028 KB |
Output is correct |
72 |
Incorrect |
37 ms |
8028 KB |
Output isn't correct |
73 |
Incorrect |
51 ms |
8284 KB |
Output isn't correct |
74 |
Correct |
46 ms |
8280 KB |
Output is correct |
75 |
Correct |
50 ms |
8284 KB |
Output is correct |
76 |
Correct |
46 ms |
8280 KB |
Output is correct |
77 |
Correct |
48 ms |
8280 KB |
Output is correct |
78 |
Correct |
53 ms |
8284 KB |
Output is correct |
79 |
Correct |
42 ms |
8280 KB |
Output is correct |
80 |
Correct |
46 ms |
8280 KB |
Output is correct |
81 |
Correct |
52 ms |
8280 KB |
Output is correct |
82 |
Correct |
44 ms |
8284 KB |
Output is correct |
83 |
Correct |
62 ms |
8240 KB |
Output is correct |
84 |
Correct |
53 ms |
8024 KB |
Output is correct |
85 |
Correct |
57 ms |
8028 KB |
Output is correct |
86 |
Correct |
60 ms |
8240 KB |
Output is correct |
87 |
Correct |
56 ms |
8028 KB |
Output is correct |
88 |
Correct |
59 ms |
8024 KB |
Output is correct |
89 |
Correct |
64 ms |
8028 KB |
Output is correct |
90 |
Correct |
61 ms |
8028 KB |
Output is correct |
91 |
Correct |
62 ms |
8024 KB |
Output is correct |
92 |
Correct |
63 ms |
8024 KB |
Output is correct |