Submission #91077

#TimeUsernameProblemLanguageResultExecution timeMemory
91077Retro3014Mecho (IOI09_mecho)C++17
100 / 100
229 ms5516 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>


#define MAX_N 800

struct S{
    int x, y;
    bool operator ==(const S &a)const{
        return (x==a.x) && (y==a.y);
    }
};

using namespace std;
int N, K;
string str[MAX_N+1];
bool type[MAX_N+1][MAX_N+1];
S M, D;
bool visited[MAX_N+1][MAX_N+1];
int dist[MAX_N+1][MAX_N+1];

bool chk(int t, int p){
    S tmp={t, p};
    if(tmp.x<0 || tmp.x>=N || tmp.y<0 || tmp.y>=N)  return false;
    if(visited[t][p])   return false;
    if(type[t][p])   return false;
    return true;
}

int dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};
deque<S> dq;
void BFS(){
    for(int i=0; i<dq.size(); i++){
        visited[dq[i].x][dq[i].y]=true;
    }
    while(!dq.empty()){
        S now = dq.front();
        dq.pop_front();
        for(int k=0; k<4; k++){
            if(chk(now.x+dx[k], now.y+dy[k]) && !(now.x+dx[k]==D.x && now.y+dy[k]==D.y)){
                dist[now.x+dx[k]][now.y+dy[k]]=dist[now.x][now.y]+1;
                visited[now.x+dx[k]][now.y+dy[k]]=true;
                dq.push_back({now.x+dx[k], now.y+dy[k]});
            }
        }
    }
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            visited[i][j]=false;
        }
    }
}

bool can(int T){
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            visited[i][j]=false;
        }
    }
    int t=0;
    dq.clear();
    dq.push_back(M);
    visited[M.x][M.y]=true;
    while(!dq.empty()){
        int sz=(int)dq.size();
        while(sz--){
            S now = dq.front();
            dq.pop_front();
            if(dist[now.x][now.y]>T){
                if(now==D)  return true;
                for(int k=0; k<4; k++){
                    if(chk(now.x+dx[k], now.y+dy[k]) && dist[now.x+dx[k]][now.y+dy[k]]>T){
                        visited[now.x+dx[k]][now.y+dy[k]]=true;
                        dq.push_back({now.x+dx[k], now.y+dy[k]});
                    }
                }
            }
        }
        t++;
        if(t==K){
            t=0; T++;
        }
    }
    return false;
}

int main(){
    scanf("%d%d", &N, &K);
    for(int i=0; i<N; i++){
        cin>>str[i];
        for(int j=0; j<N; j++){
            if(str[i][j]=='T')  type[i][j]=1;
            else if(str[i][j]=='M')    M={i, j};
            else if(str[i][j]=='D')    D={i, j};
            else if(str[i][j]=='H'){
                dq.push_back({i, j});
            }
        }
    }
    dist[D.x][D.y]=MAX_N*MAX_N+1;
    BFS();
    int s=-1, e=N*N+10, mid;
    while(s<e){
        if(s==e-1){
            if(can(e)){
                s=e;
            }else{
                e=s;
            }
        }else{
            mid=(s+e)/2;
            if(can(mid)){
                s=mid;
            }else{
                e=mid-1;
            }
        }
    }
    printf("%d", s);
    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void BFS()':
mecho.cpp:36:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<dq.size(); i++){
                  ~^~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...