結果

問題 No.401 数字の渦巻き
コンテスト
ユーザー himueu
提出日時 2026-10-17 21:47:01
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
+ 957µs
コード長 987 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,458 ms
コンパイル使用メモリ 181,836 KB
実行使用メモリ 9,896 KB
最終ジャッジ日時 2026-10-17 21:47:10
合計ジャッジ時間 5,698 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <vector>

int main() {
    using namespace std;
    int n;
    cin >> n;
    
    auto to_3dig = [&](int n) -> string {
        string res = to_string(n);
        while (res.size() < 3) { res = "0" + res; }
        return res;
    };

    constexpr int dx[] = {0, 1, 0, -1};
    constexpr int dy[] = {1, 0, -1, 0};
    vector ans(n, vector<string>(n, ""));
    auto ok = [&](int x, int y, int d) -> bool {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n) { return false; }
        if (ans[nx][ny] != "") { return false; }
        return true;
    };
    int x = 0, y = 0, d = 0;
    for (int num = 1; num <= n * n; num++) {
        ans[x][y] = to_3dig(num);
        if (!ok(x, y, d)) { d = (d + 1) % 4; }
        x += dx[d];
        y += dy[d];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << ans[i][j] << " \n"[j == n - 1];
        }
    }
}
0