Submission #554786

# Submission time Handle Problem Language Result Execution time Memory
554786 2022-04-29T11:52:38 Z roctes7 Mecho (IOI09_mecho) C++17
60 / 100
450 ms 27032 KB
#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;

int n,s;
string arr[802][802];
bool vist[802][802];
int mechoDist[802][802];
int BeesDist[802][802];
bool finished[802][802];
pair<int,int> mecho;
pair<int,int> home;

int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

bool valid (int x,int y){
return x>=1&&x<=n&&y>=1&&y<=n&&!vist[x][y]&&arr[x][y]!="T";
}


bool check =false;
void bfs (int i,int j ,int val){
vist[i][j]=true;
queue<pair<int,int>>q;
q.push(mpa(i,j));

while (!q.empty()){
    auto p =q.front();
    int x = p.fi;
    int y = p.se;
    q.pop();
//cout<<x<<" "<<y<<" "<<val<<endl;

if(val+mechoDist[x][y]-BeesDist[x][y]==0&&finished[x][y])continue;
else if(val+mechoDist[x][y]-BeesDist[x][y]>0)continue;
if(x==home.fi&&y==home.se)check=true;


for (int w=0;w<4;w++){
        int nx = x+dx[w];
        int ny = y+dy[w];

        if(valid(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++){
    string s;
    cin>>s;
    for (int 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<int,int>> q;
q.push(mecho);
vist[mecho.fi][mecho.se]=true;

while(!q.empty()){
    auto p = q.front();
    int x = p.fi;
    int y = p.se;

    q.pop();

    for (int i=0;i<4;i++){
        int nx = x+dx[i];
        int ny = y+dy[i];

        if(valid(nx,ny)){
            mechoDist[nx][ny]=mechoDist[x][y]+1;
            vist[nx][ny]=true;
            q.push(mpa(nx,ny));
        }
    }

}
for (int i=1;i<=n;i++)for(int j=1;j<=n;j++){

    if (mechoDist[i][j]%s==0)finished[i][j]=true;

    mechoDist[i][j]=mechoDist[i][j]/s+(mechoDist[i][j]%s==0?0:1);


    vist[i][j]=false;
}



for (int i=1;i<=n;i++)for (int j=1;j<=n;j++){
    if(arr[i][j]=="H"){
            q.push(mpa(i,j));
            vist[i][j]=true;
    }
}



while(!q.empty()){
    auto p = q.front();
    int x = p.fi;
    int y = p.se;
    q.pop();

    for (int i=0;i<4;i++){
        int nx = x+dx[i];
        int ny = y+dy[i];

        if(valid(nx,ny)){
            BeesDist[nx][ny]=BeesDist[x][y]+1;
            vist[nx][ny]=true;
            q.push(mpa(nx,ny));
        }
    }

}


/*cout<<endl<<endl;
for(int i=1;i<=n;i++){for (int 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(int i=1;i<=n;i++){for (int j=1;j<=n;j++){
    cout<<BeesDist[i][j];
}
cout<<endl;
}*/




int l=0;
int r = 1e9;
int ans =-1;
while (l<=r){
    int mid = (l+r)/2;
    for (int i=1;i<=n;i++)for(int j=1;j<=n;j++){
 vist[i][j]=false;
}
check=false;
bfs(mecho.fi,mecho.se,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);
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20508 KB Output is correct
2 Incorrect 11 ms 20440 KB Output isn't correct
3 Correct 9 ms 20408 KB Output is correct
4 Incorrect 10 ms 20460 KB Output isn't correct
5 Correct 10 ms 20476 KB Output is correct
6 Incorrect 10 ms 20436 KB Output isn't correct
7 Incorrect 317 ms 26944 KB Output isn't correct
8 Correct 12 ms 20564 KB Output is correct
9 Correct 10 ms 20564 KB Output is correct
10 Correct 11 ms 20492 KB Output is correct
11 Correct 10 ms 20480 KB Output is correct
12 Incorrect 11 ms 20936 KB Output isn't correct
13 Correct 10 ms 20820 KB Output is correct
14 Correct 12 ms 20864 KB Output is correct
15 Correct 12 ms 20632 KB Output is correct
16 Correct 11 ms 20556 KB Output is correct
17 Correct 13 ms 20560 KB Output is correct
18 Correct 14 ms 20564 KB Output is correct
19 Correct 11 ms 20720 KB Output is correct
20 Correct 11 ms 20692 KB Output is correct
21 Correct 11 ms 20792 KB Output is correct
22 Correct 11 ms 20752 KB Output is correct
23 Correct 10 ms 20820 KB Output is correct
24 Correct 10 ms 20820 KB Output is correct
25 Correct 11 ms 20888 KB Output is correct
26 Correct 10 ms 20848 KB Output is correct
27 Correct 11 ms 20948 KB Output is correct
28 Correct 11 ms 20948 KB Output is correct
29 Correct 11 ms 20948 KB Output is correct
30 Correct 13 ms 20984 KB Output is correct
31 Correct 11 ms 21076 KB Output is correct
32 Correct 11 ms 20948 KB Output is correct
33 Correct 24 ms 23124 KB Output is correct
34 Correct 26 ms 23176 KB Output is correct
35 Incorrect 55 ms 23216 KB Output isn't correct
36 Correct 26 ms 23608 KB Output is correct
37 Correct 27 ms 23596 KB Output is correct
38 Incorrect 87 ms 23616 KB Output isn't correct
39 Correct 30 ms 24004 KB Output is correct
40 Correct 39 ms 23908 KB Output is correct
41 Incorrect 120 ms 23956 KB Output isn't correct
42 Correct 32 ms 24372 KB Output is correct
43 Correct 34 ms 24396 KB Output is correct
44 Incorrect 159 ms 24404 KB Output isn't correct
45 Correct 42 ms 24728 KB Output is correct
46 Correct 44 ms 24764 KB Output is correct
47 Incorrect 189 ms 24720 KB Output isn't correct
48 Correct 45 ms 25212 KB Output is correct
49 Correct 51 ms 25184 KB Output is correct
50 Incorrect 237 ms 25208 KB Output isn't correct
51 Correct 63 ms 25548 KB Output is correct
52 Correct 63 ms 25520 KB Output is correct
53 Incorrect 277 ms 25592 KB Output isn't correct
54 Correct 65 ms 25972 KB Output is correct
55 Correct 70 ms 25968 KB Output is correct
56 Incorrect 317 ms 25988 KB Output isn't correct
57 Correct 70 ms 26324 KB Output is correct
58 Correct 65 ms 26372 KB Output is correct
59 Incorrect 362 ms 26316 KB Output isn't correct
60 Correct 73 ms 26752 KB Output is correct
61 Correct 78 ms 26696 KB Output is correct
62 Incorrect 412 ms 26768 KB Output isn't correct
63 Correct 280 ms 26692 KB Output is correct
64 Correct 450 ms 26764 KB Output is correct
65 Correct 424 ms 26756 KB Output is correct
66 Correct 326 ms 26832 KB Output is correct
67 Correct 277 ms 26756 KB Output is correct
68 Correct 171 ms 26828 KB Output is correct
69 Correct 181 ms 26704 KB Output is correct
70 Correct 125 ms 26688 KB Output is correct
71 Correct 131 ms 26660 KB Output is correct
72 Correct 129 ms 26740 KB Output is correct
73 Correct 156 ms 26944 KB Output is correct
74 Correct 173 ms 27016 KB Output is correct
75 Correct 195 ms 26996 KB Output is correct
76 Correct 218 ms 26956 KB Output is correct
77 Correct 188 ms 26956 KB Output is correct
78 Correct 199 ms 26888 KB Output is correct
79 Correct 172 ms 26896 KB Output is correct
80 Correct 168 ms 26956 KB Output is correct
81 Correct 201 ms 26956 KB Output is correct
82 Correct 183 ms 26956 KB Output is correct
83 Correct 276 ms 27008 KB Output is correct
84 Correct 238 ms 26944 KB Output is correct
85 Correct 249 ms 26916 KB Output is correct
86 Correct 257 ms 27032 KB Output is correct
87 Correct 287 ms 26964 KB Output is correct
88 Correct 264 ms 26920 KB Output is correct
89 Correct 279 ms 26836 KB Output is correct
90 Correct 282 ms 26812 KB Output is correct
91 Correct 281 ms 26916 KB Output is correct
92 Correct 283 ms 27024 KB Output is correct