結果

問題 No.3765 321-avoiding Grid
コンテスト
ユーザー GOTKAKO
提出日時 2026-10-16 21:30:24
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 10 ms / 1,000 ms
+ 546µs
コード長 2,243 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,568 ms
コンパイル使用メモリ 221,108 KB
実行使用メモリ 9,796 KB
最終ジャッジ日時 2026-10-16 21:31:02
合計ジャッジ時間 4,395 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    int N,a,b,c; cin >> N >> a >> b >> c;
    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    if(N <= 3){
        vector<vector<int>> A(N,vector<int>(N));
        auto dfs = [&](auto dfs,int pos) -> void {
            if(pos == N*N){
                int one = 0,two = 0,three = 0;
                for(int i=0; i<N; i++) for(int k=0; k<N; k++){
                    if(A.at(i).at(k) == 1) one++;
                    else if(A.at(i).at(k) == 3) three++;
                    else{
                        two++;
                        int now = 0;
                        for(auto [dx,dy] : dxy){
                            int ni = i+dx,nk = k+dy;
                            if(ni < 0 || nk < 0 || ni >= N || nk >= N) continue;
                            now |= 1<<A.at(ni).at(nk);
                        }
                        now &= 10;
                        if(now == 10) return;
                    }
                }
                if(one != a || two != b || three != c) return;
                for(int i=0; i<N; i++,cout<<"\n") for(int k=0; k<N; k++) cout << A.at(i).at(k);
                exit(0);
            }
            for(int i=1; i<=3; i++) A.at(pos/N).at(pos%N) = i,dfs(dfs,pos+1);
        };
        dfs(dfs,0);
    }
    else{
        vector<vector<int>> A(N,vector<int>(N));
        if(a >= N){
            for(int i=0; i<N; i++) for(int k=0; k<N; k++){
                if(b) b--,A.at(i).at(k) = 2;
                else if(a) a--,A.at(i).at(k) = 1;
                else A.at(i).at(k) = 3;
            }
        }
        else if(c >= N){
            for(int i=0; i<N; i++) for(int k=0; k<N; k++){
                if(b) b--,A.at(i).at(k) = 2;
                else if(c) c--,A.at(i).at(k) = 3;
                else A.at(i).at(k) = 1;
            }
        }
        else{
            for(int i=0; i<N; i++) for(int k=0; k<N; k++){
                if(a) a--,A.at(i).at(k) = 1;
                else if(b) b--,A.at(i).at(k) = 2;
                else A.at(i).at(k) = 3;
            }
        }
        for(int i=0; i<N; i++,cout<<"\n") for(int k=0; k<N; k++) cout << A.at(i).at(k);
    }
}
0