Submission #1297387

#TimeUsernameProblemLanguageResultExecution timeMemory
1297387carwynleCultivation (JOI17_cultivation)C++20
0 / 100
1 ms580 KiB
#include<bits/stdc++.h>
#define ii pair<int,int>
#define iii pair<int,ii>
#define st first
#define nd second
#define pb push_back
#define ln "\n"
using namespace std;
const int X=1e5+5;
const long long mod=1e9+7;
int R, C, N, dx[4]={0, -1, 0, 1}, dy[4]={-1, 0, 1, 0};
long long dem=0ll, ans=0ll;
set<ii>huong[4];
map<ii,bool>mp;

bool check(int aa, int bb){
    return (aa>=1 && aa<=R && bb>=1 && bb<=C);
}

void Inp(){
    cin>>R>>C;
    cin>>N;
    dem=1ll*N;

    for(int x, y, i=1; i<=N; i++){
        cin>>x>>y;
        mp[{x,y}]=1;
        for(int j=0; j<4; j++){
            int vx=x+dx[j], vy=y+dy[j];
            if(check(vx, vy))   huong[j].insert({x,y});
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);   cin.tie(0);
    //freopen("DISPERSAL.INP","r",stdin);    freopen("DISPERSAL.OUT","w",stdout);
    Inp();
   // for(int i=0; i<4; i++)  cout<<huong[i].size()<<" ";
    while(dem<R*C){
        //cout<<dem<<" ";
        //cout<<"d"<<dem<<" ";
        ans++;
        int id=0, ma=huong[0].size();
        for(int i=1; i<4; i++)
        if(huong[i].size()>ma){
            id=i;   ma=huong[i].size();
        }
        //if(ans<=5){
        //for(int i=0; i<4; i++)  cout<<huong[i].size()<<" ";    cout<<ln; }
        //cout<<id<<" ";
        //cout<<id<<" ";
        set<ii>newh = huong[id];
        huong[id].clear();
        for(ii v:newh){
            //if(mp[v]==1) continue;
            int x=v.st+dx[id], y=v.nd+dy[id];
            if(mp[{x,y}]==0)    dem++;
            mp[{x,y}]=1;
            for(int j=0; j<4; j++){
                int vx=x+dx[j], vy=y+dy[j];
                if(check(vx,vy) && huong[(j+2)%4].find({vx,vy})!=huong[(j+2)%4].end())  huong[(j+2)%4].erase({vx,vy});
            }
            for(int j=0; j<4; j++){
                int vx=x+dx[j], vy=y+dy[j];
               // cout<<vx<<" "<<vy<<" "<<mp[{vx,vy}]<<ln;
                if(check(vx, vy) && mp[{vx,vy}]==0){

                    huong[j].insert({x,y});
                }
            }
        }
    }
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...