#include<bits/stdc++.h>
using namespace std;
#define in insert
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define Endl "\n"
#define ENDL "\n"
#define fi first
#define se second
#define be begin()
#define en end()
#define pb push_back
#define mpa make_pair
typedef long long ll;
typedef long double ld;
const int MOD=1e9+7;
const int MAX=2e5+1;
ll n,s;
string arr[802][802];
bool vist[802][802];
ll mechoDist[802][802];
ll BeesDist[802][802];
pair<ll,ll> mecho;
pair<ll,ll> home;
ll dx[]={1,-1,0,0};
ll dy[]={0,0,1,-1};
bool valid (ll x,ll y){
return x>=1&&x<=n&&y>=1&&y<=n&&!vist[x][y]&&arr[x][y]!="T";
}
bool check =false;
void bfs (ll val){
vist[mecho.fi][mecho.se]=true;
queue<pair<ll,ll>>q;
q.push(mecho);
while (!q.empty()){
auto p =q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
if(x==home.fi&&y==home.se)check=true;
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)&&arr[nx][ny]=="G"&&val+mechoDist[nx][ny]<BeesDist[nx][ny]){
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
}
int main(){
fastio
//freopen("11.in","r",stdin);
//freopen("cl.out","w",stdout);
cin>>n>>s;
for (int i=1;i<=n;i++)for (int j=1;j<=n;j++){
BeesDist[i][j]=1e9;
mechoDist[i][j]=1e9;
}
for (ll i=1;i<=n;i++){
string s;
cin>>s;
for (ll j=1;j<=n;j++){
arr[i][j]=s[j-1];
if(arr[i][j]=="M")mecho=mpa(i,j);
else if (arr[i][j]=="D")home = mpa(i,j);
}
}
queue <pair<ll,ll>> q;
q.push(mecho);
mechoDist[mecho.fi][mecho.se]=0;
vist[mecho.fi][mecho.se]=true;
while(!q.empty()){
auto p = q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)&&arr[nx][ny]!="H"){
mechoDist[nx][ny]=mechoDist[x][y]+1;
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
for (ll i=1;i<=n;i++)for(ll j=1;j<=n;j++){
mechoDist[i][j]=mechoDist[i][j]/s;
vist[i][j]=false;
}
for (ll i=1;i<=n;i++)for (ll j=1;j<=n;j++){
if(arr[i][j]=="H"){
q.push(mpa(i,j));
BeesDist[i][j]=0;
vist[i][j]=true;
}
}
while(!q.empty()){
auto p = q.front();
ll x = p.fi;
ll y = p.se;
q.pop();
for (ll i=0;i<4;i++){
ll nx = x+dx[i];
ll ny = y+dy[i];
if(valid(nx,ny)&&arr[nx][ny]!="D"&&arr[nx][ny]!="H"){
BeesDist[nx][ny]=BeesDist[x][y]+1;
vist[nx][ny]=true;
q.push(mpa(nx,ny));
}
}
}
/*cout<<endl<<endl;
for(ll i=1;i<=n;i++){for (ll j=1;j<=n;j++){
if(arr[i][j]=="G")
cout<<mechoDist[i][j];
else cout<<arr[i][j];
}
cout<<endl;
}
cout<<endl<<endl;
for(ll i=1;i<=n;i++){for (ll j=1;j<=n;j++){
cout<<BeesDist[i][j];
}
cout<<endl;
}*/
ll l=0;
ll r = 1e9;
ll ans =-1;
while (l<=r){
ll mid = (l+r)/2;
for (ll i=1;i<=n;i++)for(ll j=1;j<=n;j++){
vist[i][j]=false;
}
check=false;
bfs(mid);
if(check){
l=mid+1;
ans = mid;
} else {
r=mid-1;
}
}
cout<<ans;
return 0;
}
unsigned long long power(unsigned long long x,
ll y, ll p)
{
unsigned long long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
unsigned long long modInverse(unsigned long long n,
ll p)
{
return power(n, p - 2, p);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
20504 KB |
Output isn't correct |
2 |
Incorrect |
13 ms |
20436 KB |
Output isn't correct |
3 |
Incorrect |
10 ms |
20436 KB |
Output isn't correct |
4 |
Incorrect |
11 ms |
20436 KB |
Output isn't correct |
5 |
Incorrect |
13 ms |
20564 KB |
Output isn't correct |
6 |
Incorrect |
11 ms |
20484 KB |
Output isn't correct |
7 |
Incorrect |
347 ms |
31380 KB |
Output isn't correct |
8 |
Correct |
14 ms |
20564 KB |
Output is correct |
9 |
Incorrect |
13 ms |
20564 KB |
Output isn't correct |
10 |
Incorrect |
11 ms |
20528 KB |
Output isn't correct |
11 |
Incorrect |
11 ms |
20564 KB |
Output isn't correct |
12 |
Incorrect |
15 ms |
20972 KB |
Output isn't correct |
13 |
Incorrect |
11 ms |
20944 KB |
Output isn't correct |
14 |
Correct |
11 ms |
21076 KB |
Output is correct |
15 |
Incorrect |
12 ms |
20564 KB |
Output isn't correct |
16 |
Incorrect |
12 ms |
20628 KB |
Output isn't correct |
17 |
Incorrect |
12 ms |
20692 KB |
Output isn't correct |
18 |
Incorrect |
13 ms |
20692 KB |
Output isn't correct |
19 |
Incorrect |
10 ms |
20704 KB |
Output isn't correct |
20 |
Incorrect |
10 ms |
20692 KB |
Output isn't correct |
21 |
Incorrect |
11 ms |
20800 KB |
Output isn't correct |
22 |
Incorrect |
13 ms |
20796 KB |
Output isn't correct |
23 |
Incorrect |
14 ms |
20960 KB |
Output isn't correct |
24 |
Incorrect |
20 ms |
20948 KB |
Output isn't correct |
25 |
Incorrect |
12 ms |
20948 KB |
Output isn't correct |
26 |
Incorrect |
14 ms |
21056 KB |
Output isn't correct |
27 |
Incorrect |
11 ms |
21060 KB |
Output isn't correct |
28 |
Incorrect |
12 ms |
21076 KB |
Output isn't correct |
29 |
Incorrect |
11 ms |
21076 KB |
Output isn't correct |
30 |
Incorrect |
13 ms |
21096 KB |
Output isn't correct |
31 |
Incorrect |
13 ms |
21204 KB |
Output isn't correct |
32 |
Incorrect |
12 ms |
21204 KB |
Output isn't correct |
33 |
Incorrect |
47 ms |
25248 KB |
Output isn't correct |
34 |
Incorrect |
68 ms |
25044 KB |
Output isn't correct |
35 |
Incorrect |
89 ms |
25140 KB |
Output isn't correct |
36 |
Incorrect |
56 ms |
25812 KB |
Output isn't correct |
37 |
Incorrect |
81 ms |
25820 KB |
Output isn't correct |
38 |
Incorrect |
85 ms |
25716 KB |
Output isn't correct |
39 |
Incorrect |
70 ms |
26484 KB |
Output isn't correct |
40 |
Incorrect |
113 ms |
26480 KB |
Output isn't correct |
41 |
Incorrect |
158 ms |
26392 KB |
Output isn't correct |
42 |
Incorrect |
107 ms |
27156 KB |
Output isn't correct |
43 |
Incorrect |
128 ms |
27144 KB |
Output isn't correct |
44 |
Incorrect |
155 ms |
27152 KB |
Output isn't correct |
45 |
Incorrect |
110 ms |
27816 KB |
Output isn't correct |
46 |
Incorrect |
170 ms |
27816 KB |
Output isn't correct |
47 |
Incorrect |
213 ms |
27828 KB |
Output isn't correct |
48 |
Incorrect |
114 ms |
28364 KB |
Output isn't correct |
49 |
Incorrect |
175 ms |
28500 KB |
Output isn't correct |
50 |
Incorrect |
251 ms |
28500 KB |
Output isn't correct |
51 |
Incorrect |
153 ms |
29136 KB |
Output isn't correct |
52 |
Incorrect |
216 ms |
29160 KB |
Output isn't correct |
53 |
Incorrect |
277 ms |
29248 KB |
Output isn't correct |
54 |
Incorrect |
162 ms |
29824 KB |
Output isn't correct |
55 |
Incorrect |
276 ms |
29892 KB |
Output isn't correct |
56 |
Incorrect |
323 ms |
29900 KB |
Output isn't correct |
57 |
Incorrect |
181 ms |
30488 KB |
Output isn't correct |
58 |
Incorrect |
325 ms |
30500 KB |
Output isn't correct |
59 |
Incorrect |
366 ms |
30500 KB |
Output isn't correct |
60 |
Incorrect |
212 ms |
31152 KB |
Output isn't correct |
61 |
Incorrect |
342 ms |
31156 KB |
Output isn't correct |
62 |
Incorrect |
436 ms |
31172 KB |
Output isn't correct |
63 |
Incorrect |
344 ms |
31052 KB |
Output isn't correct |
64 |
Incorrect |
549 ms |
31180 KB |
Output isn't correct |
65 |
Incorrect |
492 ms |
31156 KB |
Output isn't correct |
66 |
Incorrect |
500 ms |
31156 KB |
Output isn't correct |
67 |
Correct |
363 ms |
31164 KB |
Output is correct |
68 |
Incorrect |
197 ms |
31160 KB |
Output isn't correct |
69 |
Incorrect |
254 ms |
31216 KB |
Output isn't correct |
70 |
Incorrect |
181 ms |
31180 KB |
Output isn't correct |
71 |
Incorrect |
179 ms |
31308 KB |
Output isn't correct |
72 |
Incorrect |
231 ms |
31292 KB |
Output isn't correct |
73 |
Incorrect |
191 ms |
31696 KB |
Output isn't correct |
74 |
Incorrect |
357 ms |
31708 KB |
Output isn't correct |
75 |
Incorrect |
282 ms |
31708 KB |
Output isn't correct |
76 |
Incorrect |
250 ms |
31708 KB |
Output isn't correct |
77 |
Incorrect |
307 ms |
31708 KB |
Output isn't correct |
78 |
Correct |
278 ms |
31552 KB |
Output is correct |
79 |
Incorrect |
330 ms |
31744 KB |
Output isn't correct |
80 |
Incorrect |
293 ms |
31640 KB |
Output isn't correct |
81 |
Incorrect |
230 ms |
31652 KB |
Output isn't correct |
82 |
Incorrect |
272 ms |
31656 KB |
Output isn't correct |
83 |
Incorrect |
328 ms |
31576 KB |
Output isn't correct |
84 |
Incorrect |
386 ms |
31552 KB |
Output isn't correct |
85 |
Incorrect |
305 ms |
31564 KB |
Output isn't correct |
86 |
Incorrect |
276 ms |
31544 KB |
Output isn't correct |
87 |
Incorrect |
376 ms |
31596 KB |
Output isn't correct |
88 |
Incorrect |
292 ms |
31472 KB |
Output isn't correct |
89 |
Incorrect |
451 ms |
31480 KB |
Output isn't correct |
90 |
Incorrect |
308 ms |
31484 KB |
Output isn't correct |
91 |
Incorrect |
339 ms |
31484 KB |
Output isn't correct |
92 |
Incorrect |
324 ms |
31484 KB |
Output isn't correct |