# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
946715 | narsuk | Mecho (IOI09_mecho) | C++17 | 1096 ms | 11200 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//
#define ll long long int
#define endl "\n";
const int maxn = 601;
// array<int,n>
// pq
int n,st;
pair<int,int>mv[4]={ {0,1},{0,-1} , {1,0}, {-1,0} };
char g[maxn][maxn];
char g1[maxn][maxn];
char g2[maxn][maxn];
bool val(int x , int y){
if(x<0 || x>=n || y<0 || y>=n || g[x][y]=='T')return 0;
return 1;
}
int xc,yc;
void bbfs(int x){
int vis[n][n];
queue<pair<pair<int,int>,int>>q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(g1[i][j]=='H'){
q.push({{i,j},0});
}
vis[i][j]=0;
}
}
while(!q.empty()){
pair<int,int>cur;
cur=q.front().first;
int dt = q.front().second;
q.pop();
if(dt>x){
continue;
}
if(vis[cur.first][cur.second])continue;
vis[cur.first][cur.second]=1;
if(g1[cur.first][cur.second]=='D' || g1[cur.first][cur.second]=='T'){
continue;
}
g1[cur.first][cur.second]='H';
// cout<<cur.first<<" "<<cur.second<<endl;
for(auto ele : mv){
int xx = cur.first+ele.first;
int yy = cur.second+ele.second;
if(val(xx,yy)){
q.push({{xx,yy},dt+1});
}
}
}
}
int mbfs(int x){
int vis[n][n];
queue<pair<pair<int,int>,int>>q;
int cou = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
// cout<<g1[i][j];
if(g1[i][j]=='M'){
q.push({{i,j},0});
cou++;
}
vis[i][j]=0;
}
// cout<<endl;
}
// cout<<endl;
if(cou==0){
return 0;
}
while(!q.empty()){
pair<int,int>cur;
cur=q.front().first;
int dt = q.front().second;
q.pop();
if(dt>x){
continue;
}
if(g1[cur.first][cur.second]=='D'){
return 1;
}
if(g1[cur.first][cur.second]!='G' && g1[cur.first][cur.second]!='M'){
continue;
}
if(vis[cur.first][cur.second])continue;
vis[cur.first][cur.second]=1;
g1[cur.first][cur.second]='M';
for(auto ele : mv){
int xx = cur.first+ele.first;
int yy = cur.second+ele.second;
if(val(xx,yy)){
q.push({{xx,yy},dt+1});
}
}
}
return 2;
}
bool check(int x){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
g1[i][j]=g[i][j];
}
}
bbfs(x);
// cout<<"de"<<endl;
while(1){
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// cout<<g1[i][j];
// }
// cout<<endl;
// }
// cout<<endl;
// int cans1 = mbfs(st);
// cout<<cans1<<endl;
// if(cans1==1) return 1;
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// g1[i][j]=g2[i][j];
// }
// }
int cans = mbfs(st);
// cout<<cans<<endl;
if(cans==0)return 0;
if(cans==1) return 1;
bbfs(1);
}
}
void solu(){
cin>>n>>st;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>g[i][j];
g1[i][j]=g[i][j];
if(g[i][j]=='M'){
xc=i;
yc=j;
}
}
}
// cout<<endl;
int l=0;
int r = 1e6;
int ans=0;
while(l<r){
int mid = l + (r-l+1)/2;
// cout<<mid<<endl;
if(check(mid)){
ans=max(mid,ans);
l=mid;
}else{
r=mid-1;
}
}
// cout<<check(1)<<endl;
cout<<ans<<endl;
}
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// int cass;
// cin>>cass;
// for(int i=0;i<cass;i++)
solu();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |