結果

問題 No.5019 Hakai Project
ユーザー askr58askr58
提出日時 2023-11-18 03:06:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 3,000 ms
コード長 7,027 bytes
コンパイル時間 3,828 ms
コンパイル使用メモリ 259,092 KB
実行使用メモリ 6,676 KB
スコア 1,595,027,645
最終ジャッジ日時 2023-11-18 03:06:33
合計ジャッジ時間 9,984 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,676 KB
testcase_01 AC 8 ms
6,676 KB
testcase_02 AC 8 ms
6,676 KB
testcase_03 AC 7 ms
6,676 KB
testcase_04 AC 7 ms
6,676 KB
testcase_05 AC 7 ms
6,676 KB
testcase_06 AC 7 ms
6,676 KB
testcase_07 AC 8 ms
6,676 KB
testcase_08 AC 8 ms
6,676 KB
testcase_09 AC 7 ms
6,676 KB
testcase_10 AC 7 ms
6,676 KB
testcase_11 AC 7 ms
6,676 KB
testcase_12 AC 7 ms
6,676 KB
testcase_13 AC 6 ms
6,676 KB
testcase_14 AC 8 ms
6,676 KB
testcase_15 AC 7 ms
6,676 KB
testcase_16 AC 7 ms
6,676 KB
testcase_17 AC 7 ms
6,676 KB
testcase_18 AC 7 ms
6,676 KB
testcase_19 AC 8 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 AC 7 ms
6,676 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 7 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 7 ms
6,676 KB
testcase_26 AC 6 ms
6,676 KB
testcase_27 AC 7 ms
6,676 KB
testcase_28 AC 7 ms
6,676 KB
testcase_29 AC 7 ms
6,676 KB
testcase_30 AC 7 ms
6,676 KB
testcase_31 AC 7 ms
6,676 KB
testcase_32 AC 6 ms
6,676 KB
testcase_33 AC 8 ms
6,676 KB
testcase_34 AC 8 ms
6,676 KB
testcase_35 AC 8 ms
6,676 KB
testcase_36 AC 8 ms
6,676 KB
testcase_37 AC 7 ms
6,676 KB
testcase_38 AC 7 ms
6,676 KB
testcase_39 AC 8 ms
6,676 KB
testcase_40 AC 7 ms
6,676 KB
testcase_41 AC 8 ms
6,676 KB
testcase_42 AC 7 ms
6,676 KB
testcase_43 AC 8 ms
6,676 KB
testcase_44 AC 7 ms
6,676 KB
testcase_45 AC 7 ms
6,676 KB
testcase_46 AC 7 ms
6,676 KB
testcase_47 AC 7 ms
6,676 KB
testcase_48 AC 6 ms
6,676 KB
testcase_49 AC 8 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
ll linf=1000000000000000000;

template <typename T>
bool chmax(T& a,T b){
    if(a<b){
        a=b;
        return true;
    }else return false;
}
template <typename T>
bool chmin(T& a,T b){
    if(a>b){
        a=b;
        return true;
    }else return false;
}
vector<int> dx={1,0,-1,0};
vector<int> dy={0,1,0,-1};
struct bomb{
    ll c,l;
    int id;
    vector<pair<int,int>> range;
    bomb(){}
    bomb(ll c,ll l,int id):c(c),l(l),id(id){range.resize(l);}
};
struct bomb_instance{
    int x,y;
    int id;
    set<int> blown_shops;
    bomb_instance(){}
    bomb_instance(int x,int y,int id):x(x),y(y),id(id){}
};

bool operator==(bomb_instance& a,bomb_instance &b){
    return a.x==b.x&&a.y==b.y&&a.id==b.id;
}
int n,m;
vector<bomb> bombs;
vector<bomb_instance> locates;
vector<string> grid;
string ans;
int ans_count;
map<pair<int,int>,int> shop_num;
vector<pair<int,int>> shop_location;
set<pair<int,int>> shops;
vector<int> blown_num;

void read_input(){
    cin>>n>>m;
    ans_count=0;
    bombs.resize(m);
    grid.resize(n);
    for(int i=0;i<n;i++)cin>>grid[i];
    for(int i=0;i<m;i++){
        ll c,l;
        cin>>c>>l;
        bombs[i]=bomb(c,l,i);
        for(int j=0;j<l;j++){
            int a,b;
            cin>>a>>b;
            bombs[i].range[j]=make_pair(a,b);
        }
    }
    sort(bombs.begin(),bombs.end(),[](bomb a,bomb b)->bool{
        return a.c<b.c;
    });
    for(int i=0;i<n;i++)for(int j=0;j<n;j++){
        if(grid[i][j]=='@'){
            shop_num[{i,j}]=shop_location.size();
            shop_location.push_back({i,j});
            shops.insert({i,j});
        }
        
    }
    blown_num.resize(shop_location.size(),0);
}

void greedy(){
    vector<string> copy_grid=grid;

    auto good_bomb=[&](int x,int y)->void{
        bomb_instance res;
        int max_score=0;
        for(int i=0;i<m;i++){
            int nx=0,ny=0;
            for(auto [lx,ly]:bombs[i].range){
                if(x<n/2){
                    if(y<n/2){
                        if(lx>0||ly>0)continue;
                    }else{
                        if(lx>0||ly<0)continue;
                    }
                }else{
                    if(y<n/2){
                        if(lx<0||ly>0)continue;
                    }else{
                        if(lx<0||ly<0)continue;
                    }
                }
                if(abs(nx)+abs(ny)<abs(lx)+abs(ly)){
                    nx=lx;ny=ly;
                }
            }
            int score=0;
            for(auto [lx,ly]:bombs[i].range){
                int mx=x-nx+lx,my=y-ny+ly;
                if(mx<0||mx>=n||my<0||my>=n)continue;
                if(copy_grid[mx][my]!='.')score++;
            }
            if(score>max_score){
                max_score=score;
                res=bomb_instance(x-nx,y-ny,bombs[i].id);
            }
        }
        int index=0;
        for(int i=0;i<m;i++)if(bombs[i].id==res.id)index=i;

        for(auto [lx,ly]:bombs[index].range){
            if(res.x+lx<0||res.x+lx>=n||res.y+ly<0||res.y+ly>=n)continue;
            copy_grid[res.x+lx][res.y+ly]='.';
            if(shop_num.count({res.x+lx,res.y+ly})){
                res.blown_shops.insert(shop_num[{res.x+lx,res.y+ly}]);
            }
        }
        for(int x:res.blown_shops)blown_num[x]++;
       
        locates.push_back(res);
    };
    for(int i=0;i<n-1;i++){
        for(int j=0;j<=i;j++){
            if(j>=n/2||i-j>=n/2)continue;
            int x=j,y=i-j;
            if(copy_grid[x][y]!='.')good_bomb(x,y);
            if(copy_grid[n-1-x][y]!='.')good_bomb(n-1-x,y);
            if(copy_grid[x][n-1-y]!='.')good_bomb(x,n-1-y);
            if(copy_grid[n-1-x][n-1-y]!='.')good_bomb(n-1-x,n-1-y);

            //for(string s:copy_grid)cout<<s<<endl;
        }        
    }
    //cout<<endl;
    //for(string s:copy_grid)cout<<s<<endl;
}

void go_straight(int from,int to){
    int now_x=from/n,now_y=from%n,next_x=to/n,next_y=to%n;

    if(now_x>next_x){
        for(int i=0;i<now_x-next_x;i++){
            ans+="1 U\n";
            ans_count++;
        }
    }else if(now_x<next_x){
        for(int i=0;i<next_x-now_x;i++){
            ans+="1 D\n";
            ans_count++;
        }
    }
    if(now_y>next_y){
        for(int i=0;i<now_y-next_y;i++){
            ans+="1 L\n";
            ans_count++;
        }
    }else if(next_y>now_y){
        for(int i=0;i<next_y-now_y;i++){
            ans+="1 R\n";
            ans_count++;
        }
    }
}

int main()
{
    read_input();
    greedy();
    int min_index=0;
    for(int i=1;i<blown_num.size();i++){
        if(blown_num[i]<blown_num[min_index])min_index=i;
    }
    vector<bomb_instance> last_bomb;
    for(bomb_instance b:locates){
        if(b.blown_shops.count(min_index))last_bomb.push_back(b);
    }
    for(bomb_instance b:last_bomb){
        for(int i=0;i<locates.size();i++){
            if(locates[i]==b){
                if(i+1!=locates.size())swap(locates[i],locates[locates.size()-1]);
                locates.pop_back();
                break;
            }
        }
    }
    int now_x=0,now_y=0;
    int q=locates.size();
    for(int i=0;i<q;i++){
        int next_x=10000,next_y=100000;
        bomb_instance c;
        for(bomb_instance b:locates){
            if(abs(now_x-b.x)+abs(now_y-b.y)<abs(now_x-next_x)+abs(now_y-next_y)){
                next_x=b.x;
                next_y=b.y;
                c=b;
            }
        }

        int next_shop_x=10000,next_shop_y=10000;
        for(auto [x,y]:shops){
            if(abs(now_x-x)+abs(x-next_x)+abs(now_y-y)+abs(y-next_y)<abs(now_x-next_shop_x)+abs(next_shop_x-next_x)+abs(now_y-next_shop_y)+abs(next_shop_y-next_y)){
                next_shop_x=x;
                next_shop_y=y;
            }
        }
        go_straight(now_x*n+now_y,next_shop_x*n+next_shop_y);
        ans+="2 "+to_string(c.id+1)+"\n";
        ans_count++;
        go_straight(next_shop_x*n+next_shop_y,next_x*n+next_y);
        ans+="3 "+to_string(c.id+1)+"\n";
        ans_count++;

        now_x=next_x;now_y=next_y;
        for(int j=0;j<locates.size();j++){
            if(locates[j]==c){
                if(j+1!=locates.size())swap(locates[j],locates[locates.size()-1]);
                locates.pop_back();
                break;
            }
        }
        for(int p:c.blown_shops){
            if(shops.count(shop_location[p]))shops.erase(shop_location[p]);
        }
    }

    int next_shop_x=shop_location[min_index].first,next_shop_y=shop_location[min_index].second;
    go_straight(now_x*n+now_y,next_shop_x*n+next_shop_y);
    now_x=next_shop_x;now_y=next_shop_y;
    for(bomb_instance c:last_bomb){
        ans+="2 "+to_string(c.id+1)+"\n";
        ans_count++;
    }
    for(bomb_instance c:last_bomb){
        go_straight(now_x*n+now_y,c.x*n+c.y);
        ans+="3 "+to_string(c.id+1)+"\n";
        ans_count++;
        now_x=c.x;now_y=c.y;
    }

    cout<<ans_count<<endl;
    cout<<ans;

    



    
    return 0;
}
0