結果

問題 No.3723 Climb or Detour
コンテスト
ユーザー karinohito
提出日時 2026-09-19 13:42:40
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 86 ms / 2,000 ms
+ 945µs
コード長 1,635 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,129 ms
コンパイル使用メモリ 352,692 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-09-19 13:42:49
合計ジャッジ時間 8,353 ms
ジャッジサーバーID
(参考情報)
judge7_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 58
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N,K;
    cin>>N>>K;
    ll SY,SX,GY,GX;
    cin>>SY>>SX>>GY>>GX;
    SY--;SX--;GY--;GX--;
    bool ys=0,xs=0;
    if(SY>GY){
        SY=N-SY-1;
        GY=N-GY-1;
        ys=1;
    }
    if(SX>GX){
        SX=N-SX-1;
        GX=N-GX-1;
        xs=1;
    }
    ll d=(GX-SX)+(GY-SY);
    if(K<d||d>2*K){
        cout<<-1<<"\n";
        return 0;
    }
    ll e=K-d;
    vector<string> S(N,string(N,'.'));
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            ll r=abs(SY-i)+abs(SX-j);
            r=min(r,e);
            if(r%2==1)S[i][j]='#';
        }
    }
    if(S[GY][GX]=='#'){
        cout<<-1<<"\n";
        return 0;
    }
    vector<vector<int>> D(N,vector<int>(N,1e9));
    D[SY][SX]=0;
    priority_queue<array<ll,3>,vector<array<ll,3>>,greater<array<ll,3>>> Q;
    Q.push({0,SY,SX});
    vector<int> u={0,1,0,-1,0};
    while(!Q.empty()){
        auto [c,y,x]=Q.top();
        Q.pop();
        if(D[y][x]!=c)continue;
        for(int dd=0;dd<4;dd++){
            int ny=y+u[dd];
            int nx=x+u[dd+1];
            if(ny<0||nx<0||ny>=N||nx>=N)continue;
            ll nc=c+1+(S[ny][nx]!=S[y][x]);
            if(D[ny][nx]<=nc)continue;
            D[ny][nx]=nc;
            Q.push({nc,ny,nx});
        }
    }
    if(D[GY][GX]!=K){
    	cout<<-1<<"\n";
    	return 0;
    }
    assert(D[GY][GX]==K);
    if(ys)reverse(S.begin(),S.end());
    if(xs){
        for(int i=0;i<N;i++)reverse(S[i].begin(),S[i].end());
    }
    for(int i=0;i<N;i++)cout<<S[i]<<"\n";
}
0