// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#define here cerr<<"---------------------------\n"
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
a+=b;
a+=mod;
if(a>=mod) a%=mod;
return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 805
ll n,s;
ll a[maxn][maxn];
ll dm[maxn][maxn];
ll dh[maxn][maxn];
ll conv(char c){
if(c=='T') return 0;
if(c=='G') return 1;
if(c=='M') return 2;
if(c=='D') return 3;
if(c=='H') return 4;
}
vector<ll> dx = {0,0,-1,1};
vector<ll> dy = {1,-1,0,0};
vector<pll> hive;
ll sx,sy,ex,ey;
bool valid(ll x,ll y){return x>=1&&x<=n&&y>=1&&y<=n;}
bool moe(ll t){
if(t*s>=dh[sx][sy]) return 0;
for(ll i = 1;i<=n;i++) for(ll j = 1;j<=n;j++) dm[i][j] = llinf;
dm[sx][sy] = t*s;
queue<pll> q;
q.push({sx,sy});
while(!q.empty()){
pll p = q.front();
q.pop();
ll x = p.fi,y = p.sc;
if(a[x][y]==3) return 1;
for(ll i = 0;i<4;i++){
ll x1 = x + dx[i];
ll y1 = y + dy[i];
if(!valid(x1,y1)||dm[x1][y1]!=llinf||dh[x1][y1]<=dm[x][y]+1||a[x][y]==0) continue;
dm[x1][y1] = dm[x][y]+1;
q.push({x1,y1});
}
}
return 0;
}
ll bs(){
ll l = 0,r = 2*n*n,mid,rez = -1;
while(l<=r){
mid = (l+r)/2;
if(moe(mid)){
rez = mid;
l = mid+1;
}else r = mid-1;
}
return rez;
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> s;
for(ll i = 1;i<=n;i++){
string s1; cin >> s1;
for(ll j = 1;j<=n;j++){
a[i][j] = conv(s1[j-1]);
if(a[i][j]==2){
sx = i;
sy = j;
a[i][j] = 1;
}
if(a[i][j]==3){
ex = i;
ey = j;
}
if(a[i][j]==4){
hive.pb({i,j});
}
}
}
for(ll i = 1;i<=n;i++) for(ll j = 1;j<=n;j++) dh[i][j] = llinf;
queue<pll> q;
for(pll p : hive){
dh[p.fi][p.sc] = 0;
q.push(p);
}
while(!q.empty()){
pll p = q.front();
q.pop();
ll x = p.fi,y = p.sc;
for(ll i = 0;i<4;i++){
ll x1 = x + dx[i];
ll y1 = y + dy[i];
if(!valid(x1,y1)||a[x1][y1]!=1||dh[x1][y1]!=llinf) continue;
dh[x1][y1] = dh[x][y]+s;
q.push({x1,y1});
}
}
dh[ex][ey] = llinf;
cout<<bs()<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:33:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'long long int conv(char)':
mecho.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
64 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
90 ms |
15696 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
1 ms |
1356 KB |
Output is correct |
32 |
Correct |
1 ms |
1356 KB |
Output is correct |
33 |
Correct |
5 ms |
6860 KB |
Output is correct |
34 |
Correct |
8 ms |
6860 KB |
Output is correct |
35 |
Correct |
29 ms |
6968 KB |
Output is correct |
36 |
Correct |
6 ms |
7904 KB |
Output is correct |
37 |
Correct |
7 ms |
7884 KB |
Output is correct |
38 |
Correct |
34 ms |
7884 KB |
Output is correct |
39 |
Correct |
10 ms |
8780 KB |
Output is correct |
40 |
Correct |
9 ms |
8780 KB |
Output is correct |
41 |
Correct |
47 ms |
8780 KB |
Output is correct |
42 |
Correct |
9 ms |
9804 KB |
Output is correct |
43 |
Correct |
10 ms |
9804 KB |
Output is correct |
44 |
Correct |
64 ms |
9804 KB |
Output is correct |
45 |
Correct |
11 ms |
10700 KB |
Output is correct |
46 |
Correct |
13 ms |
10640 KB |
Output is correct |
47 |
Correct |
101 ms |
10748 KB |
Output is correct |
48 |
Correct |
12 ms |
11596 KB |
Output is correct |
49 |
Correct |
14 ms |
11632 KB |
Output is correct |
50 |
Correct |
87 ms |
11716 KB |
Output is correct |
51 |
Correct |
14 ms |
12620 KB |
Output is correct |
52 |
Correct |
15 ms |
12620 KB |
Output is correct |
53 |
Correct |
128 ms |
12640 KB |
Output is correct |
54 |
Correct |
16 ms |
13588 KB |
Output is correct |
55 |
Correct |
22 ms |
13536 KB |
Output is correct |
56 |
Correct |
161 ms |
13552 KB |
Output is correct |
57 |
Correct |
23 ms |
14416 KB |
Output is correct |
58 |
Correct |
20 ms |
14412 KB |
Output is correct |
59 |
Correct |
169 ms |
14444 KB |
Output is correct |
60 |
Correct |
19 ms |
15436 KB |
Output is correct |
61 |
Correct |
26 ms |
15396 KB |
Output is correct |
62 |
Correct |
173 ms |
15476 KB |
Output is correct |
63 |
Correct |
170 ms |
15436 KB |
Output is correct |
64 |
Correct |
305 ms |
15484 KB |
Output is correct |
65 |
Correct |
225 ms |
15480 KB |
Output is correct |
66 |
Correct |
184 ms |
15476 KB |
Output is correct |
67 |
Correct |
172 ms |
15428 KB |
Output is correct |
68 |
Correct |
74 ms |
15532 KB |
Output is correct |
69 |
Correct |
75 ms |
15532 KB |
Output is correct |
70 |
Correct |
52 ms |
15496 KB |
Output is correct |
71 |
Correct |
72 ms |
15540 KB |
Output is correct |
72 |
Correct |
43 ms |
15484 KB |
Output is correct |
73 |
Correct |
46 ms |
15984 KB |
Output is correct |
74 |
Correct |
67 ms |
15976 KB |
Output is correct |
75 |
Correct |
80 ms |
16020 KB |
Output is correct |
76 |
Correct |
87 ms |
16040 KB |
Output is correct |
77 |
Correct |
92 ms |
16032 KB |
Output is correct |
78 |
Correct |
99 ms |
15940 KB |
Output is correct |
79 |
Correct |
52 ms |
15960 KB |
Output is correct |
80 |
Correct |
62 ms |
15996 KB |
Output is correct |
81 |
Correct |
70 ms |
16016 KB |
Output is correct |
82 |
Correct |
69 ms |
15896 KB |
Output is correct |
83 |
Correct |
109 ms |
15852 KB |
Output is correct |
84 |
Correct |
77 ms |
15812 KB |
Output is correct |
85 |
Correct |
87 ms |
15848 KB |
Output is correct |
86 |
Correct |
95 ms |
15880 KB |
Output is correct |
87 |
Correct |
93 ms |
15872 KB |
Output is correct |
88 |
Correct |
98 ms |
15788 KB |
Output is correct |
89 |
Correct |
93 ms |
15684 KB |
Output is correct |
90 |
Correct |
91 ms |
15892 KB |
Output is correct |
91 |
Correct |
114 ms |
15820 KB |
Output is correct |
92 |
Correct |
106 ms |
15756 KB |
Output is correct |