# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
906640 |
2024-01-14T15:43:00 Z |
tosivanmak |
Mecho (IOI09_mecho) |
C++17 |
|
155 ms |
10632 KB |
#include <bits/stdc++.h>
using namespace std;
#define ll int
ll dx[4]={0,0,1,-1};
ll dy[4]={1,-1,0,0};
vector<pair<short,short> >pos;
char arr[802][802];
struct Graph{
bool vis[802][802];
ll dist[802][802];
queue<pair<short,short> >q;
void init(ll n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=1e9;
}
}
}
void bfs(){
for(auto& u: pos){
vis[u.first][u.second]=1;
dist[u.first][u.second]=0;
q.push({u.first,u.second});
}
while(!q.empty()){
pair<ll,ll>cur=q.front();
q.pop();
for(int k=0;k<4;k++){
pair<ll,ll>u={cur.first+dx[k],cur.second+dy[k]};
ll i=cur.first,j=cur.second;
if(!vis[u.first][u.second]&&(arr[i+dx[k]][j+dy[k]]=='M'||arr[i+dx[k]][j+dy[k]]=='G'||arr[i+dx[k]][j+dy[k]]=='H')){
vis[u.first][u.second]=1;
dist[u.first][u.second]=dist[cur.first][cur.second]+1;
q.push(u);
}
}
}
}
}gp2;
struct Graph2{
bool vis[802][802];
queue<pair<short,short> >q;
ll bees[802][802];
ll dist[802][802];
void bfs(ll r, ll c, ll para, ll divi){
vis[r][c]=1;
dist[r][c]=0;
q.push({r,c});
while(!q.empty()){
pair<ll,ll>cur=q.front();
q.pop();
// cout<<bees[u.first][u.second]<<'\n';
// cout<<dist[cur.first][cur.second]+1<<" "<<(ll)(divi+1)*(ll)bees[u.first][u.second]-(ll)1<<'\n';
// cout<<lol<<'\n';
// if(lol<0){
// cout<<divi+1<<" "<<bees[u.first][u.second]<<'\n';
// }
ll i=cur.first,j=cur.second;
for(int k=0;k<4;k++){
pair<ll,ll>u={i+dx[k],j+dy[k]};
if((arr[i+dx[k]][j+dy[k]]=='M'||arr[i+dx[k]][j+dy[k]]=='G'||arr[i+dx[k]][j+dy[k]]=='D')&&!vis[i+dx[k]][j+dy[k]]){
long long lol=divi;
lol*=((long long)bees[u.first][u.second]-(long long)para);
lol--;
long long lol2=dist[cur.first][cur.second];
lol2++;
if(lol2<=lol){
vis[u.first][u.second]=1;
dist[u.first][u.second]=dist[cur.first][cur.second]+1;
q.push(u);
}
}
}
}
}
void clear(ll n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
// cout<<bees[i][j]<<" ";
vis[i][j]=0;
dist[i][j]=1e9;
}
// cout<<'\n';
}
}
bool ck(ll r, ll c, ll n, ll par, ll finr, ll finc, ll divi){
clear(n);
bfs(r,c,par,divi);
if(vis[finr][finc]){
return 1;
}
return 0;
}
}gp3;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n,s;
cin>>n>>s;
gp2.init(n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>arr[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(arr[i][j]=='H'){
pos.push_back({i,j});
}
}
}
gp2.bfs();
pos.clear();
ll startr,startc,endr,endc;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(arr[i][j]=='M'){
startr=i,startc=j;
}
if(arr[i][j]=='D'){
endr=i,endc=j;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
gp3.bees[i][j]=gp2.dist[i][j];
}
}
pos.clear();
ll l=0,r=gp2.dist[startr][startc]-1;
while(l<=r){
if(l==r){
break;
}
else if(l==r-1){
if(gp3.ck(startr,startc,n,r,endr,endc,s)){
l=r;
}
else{
r=l;
}
break;
}
else{
ll mid=(l+r)>>1;
if(gp3.ck(startr,startc,n,mid,endr,endc,s)){
l=mid;
}
else{
r=mid;
}
}
}
// cout<<'\n';
// cout<<l<<'\n';
if(!gp3.ck(startr,startc,n,l,endr,endc,s)){
cout<<"-1\n";
}
else{
cout<<l<<'\n';
}
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:164:15: warning: 'endc' may be used uninitialized in this function [-Wmaybe-uninitialized]
164 | if(!gp3.ck(startr,startc,n,l,endr,endc,s)){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:164:15: warning: 'startc' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:164:15: warning: 'startr' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:164:15: warning: 'endr' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
66 ms |
10520 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8576 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8792 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
6 ms |
9128 KB |
Output is correct |
34 |
Correct |
6 ms |
9052 KB |
Output is correct |
35 |
Correct |
18 ms |
9052 KB |
Output is correct |
36 |
Correct |
7 ms |
9048 KB |
Output is correct |
37 |
Correct |
9 ms |
9168 KB |
Output is correct |
38 |
Correct |
23 ms |
9052 KB |
Output is correct |
39 |
Correct |
9 ms |
9048 KB |
Output is correct |
40 |
Correct |
9 ms |
9052 KB |
Output is correct |
41 |
Correct |
28 ms |
9052 KB |
Output is correct |
42 |
Correct |
10 ms |
9052 KB |
Output is correct |
43 |
Correct |
10 ms |
9076 KB |
Output is correct |
44 |
Correct |
40 ms |
9252 KB |
Output is correct |
45 |
Correct |
11 ms |
9048 KB |
Output is correct |
46 |
Correct |
12 ms |
9112 KB |
Output is correct |
47 |
Correct |
45 ms |
9052 KB |
Output is correct |
48 |
Correct |
14 ms |
9308 KB |
Output is correct |
49 |
Correct |
14 ms |
9308 KB |
Output is correct |
50 |
Correct |
49 ms |
9308 KB |
Output is correct |
51 |
Correct |
15 ms |
9308 KB |
Output is correct |
52 |
Correct |
15 ms |
9308 KB |
Output is correct |
53 |
Correct |
64 ms |
9184 KB |
Output is correct |
54 |
Correct |
17 ms |
9308 KB |
Output is correct |
55 |
Correct |
17 ms |
9304 KB |
Output is correct |
56 |
Correct |
77 ms |
9500 KB |
Output is correct |
57 |
Correct |
20 ms |
9564 KB |
Output is correct |
58 |
Correct |
24 ms |
9560 KB |
Output is correct |
59 |
Correct |
96 ms |
10240 KB |
Output is correct |
60 |
Correct |
22 ms |
9724 KB |
Output is correct |
61 |
Correct |
24 ms |
9820 KB |
Output is correct |
62 |
Correct |
95 ms |
10324 KB |
Output is correct |
63 |
Correct |
86 ms |
9880 KB |
Output is correct |
64 |
Correct |
145 ms |
9812 KB |
Output is correct |
65 |
Correct |
155 ms |
9812 KB |
Output is correct |
66 |
Correct |
103 ms |
9892 KB |
Output is correct |
67 |
Correct |
103 ms |
10068 KB |
Output is correct |
68 |
Correct |
42 ms |
9820 KB |
Output is correct |
69 |
Correct |
34 ms |
9888 KB |
Output is correct |
70 |
Correct |
33 ms |
9808 KB |
Output is correct |
71 |
Correct |
33 ms |
9816 KB |
Output is correct |
72 |
Correct |
30 ms |
10016 KB |
Output is correct |
73 |
Correct |
28 ms |
10484 KB |
Output is correct |
74 |
Correct |
49 ms |
10584 KB |
Output is correct |
75 |
Correct |
48 ms |
10584 KB |
Output is correct |
76 |
Correct |
54 ms |
10588 KB |
Output is correct |
77 |
Correct |
52 ms |
10580 KB |
Output is correct |
78 |
Correct |
56 ms |
10576 KB |
Output is correct |
79 |
Correct |
45 ms |
10576 KB |
Output is correct |
80 |
Correct |
50 ms |
10584 KB |
Output is correct |
81 |
Correct |
55 ms |
10588 KB |
Output is correct |
82 |
Correct |
53 ms |
10588 KB |
Output is correct |
83 |
Correct |
62 ms |
10588 KB |
Output is correct |
84 |
Correct |
53 ms |
10576 KB |
Output is correct |
85 |
Correct |
56 ms |
10628 KB |
Output is correct |
86 |
Correct |
63 ms |
10576 KB |
Output is correct |
87 |
Correct |
57 ms |
10632 KB |
Output is correct |
88 |
Correct |
60 ms |
10580 KB |
Output is correct |
89 |
Correct |
70 ms |
10588 KB |
Output is correct |
90 |
Correct |
72 ms |
10592 KB |
Output is correct |
91 |
Correct |
67 ms |
10580 KB |
Output is correct |
92 |
Correct |
66 ms |
10588 KB |
Output is correct |