結果

問題 No.3765 321-avoiding Grid
コンテスト
ユーザー t98slider
提出日時 2026-10-16 22:29:02
言語 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  
実行時間 7 ms / 1,000 ms
+ 45µs
コード長 2,559 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,515 ms
コンパイル使用メモリ 353,164 KB
実行使用メモリ 9,864 KB
最終ジャッジ日時 2026-10-16 22:29:39
合計ジャッジ時間 4,867 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<pair<char, int>> a(3);
    for(int i = 0; i < 3; i++){
        cin >> a[i].second;
        a[i].first = '1' + i;
    }
    vector<string> A(n, string(n, '?'));
    vector<pair<int,int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    auto dfs = [&](auto dfs, int y, int x, int cur) -> bool {
        if(cur == -1) return true;
        for(auto [dy, dx] : dir){
            int ny = y + dy, nx = x + dx;
            if(ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
            if(A[ny][nx] == cur + '1' && dfs(dfs, ny, nx, cur - 1)) return true;
        }
        return false;
    };
    vector<pair<int,int>> ord;
    for(int y = 0; y < n; y++){
        for(int x = 0; x < n; x++){
            if((y + x) % 2 == 0) ord.emplace_back(y, x);
        }
    }
    for(int y = 0; y < n; y++){
        for(int x = 0; x < n; x++){
            if((y + x) % 2 == 1) ord.emplace_back(y, x);
        }
    }
    do{
        auto b = a;
        for(int y = 0; y < n; y++){
            for(int x = 0; x < n; x++){
                for(int i = 0; i < 3; i++){
                    if(b[i].second >= 1){
                        b[i].second--;
                        A[y][x] = b[i].first;
                        break;
                    }
                }
            }
        }
        bool flg = true;
        for(int y = 0; y < n && flg; y++){
            for(int x = 0; x < n; x++){
                if(A[y][x] == '3' && dfs(dfs, y, x, 1)){
                    flg = false;
                    break;
                }
            }
        }
        if(flg){
            for(auto &&str : A) cout << str << '\n';
            return 0;
        }
    }while(next_permutation(a.begin(), a.end()));
    do{
        auto b = a;
        for(auto [y, x] : ord){
            for(int i = 0; i < 3; i++){
                if(b[i].second >= 1){
                    b[i].second--;
                    A[y][x] = b[i].first;
                    break;
                }
            }
        }
        bool flg = true;
        for(int y = 0; y < n && flg; y++){
            for(int x = 0; x < n; x++){
                if(A[y][x] == '3' && dfs(dfs, y, x, 1)){
                    flg = false;
                    break;
                }
            }
        }
        if(flg){
            for(auto &&str : A) cout << str << '\n';
            return 0;
        }
    }while(next_permutation(a.begin(), a.end()));
}
0