結果

問題 No.3092 Tired Queen
コンテスト
ユーザー Today03
提出日時 2025-04-06 17:16:22
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,824 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,966 ms
コンパイル使用メモリ 344,840 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2026-07-08 12:56:52
合計ジャッジ時間 5,394 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define LB(v, x) (int)(lower_bound(ALL(v), x) - (v).begin())
#define UQ(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define IO ios::sync_with_stdio(false), cin.tie(nullptr);
#define chmax(a, b) (a) = (a) < (b) ? (b) : (a)
#define chmin(a, b) (a) = (a) < (b) ? (a) : (b)
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

const vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
// 0下 1右 2上 3左 4右下 5右上 6左下 7左上

int main() {
    ll N;
    cin >> N;

    vector<vector<ll>> ans(N, vector<ll>(N));
    int x = 0, y = 0;
    int type = 0;
    if (N % 2 == 0) type = 1;
    int idx = 1;
    for (int t = N; t >= 1; t--) {
        if (t % 2 == 1) {
            vector<int> jun;
            int gx, gy;
            // 0下 1右 2上 3左 4右下 5右上 6左下 7左上
            // 右下 上 左上 右
            if (type == 0) jun = {4, 2, 7, 1}, gx = x + t - 1, gy = y;
            // 左下 上 右上 左
            else
                jun = {6, 2, 5, 3}, gx = x - t + 1, gy = y;

            int d = 0, d2 = t - 1;
            while (true) {
                ans[y][x] = idx++;
                if (x == gx && y == gy) break;
                if (d % 2 == 0)
                    x += dx[jun[d]] * d2, y += dy[jun[d]] * d2, d2--;
                else
                    x += dx[jun[d]], y += dy[jun[d]];
                d++, d %= 4;
            }

            if (type == 0)
                x--, y++;
            else
                x++, y++;
        } else {
            vector<int> jun;
            int px = x, py = y;
            // 0下 1右 2上 3左 4右下 5右上 6左下 7左上
            // 左上 右 右下 上
            int shote;
            if (type == 0) jun = {7, 1, 4, 2}, shote = 0;
            // 左下 上 右上 左
            else
                jun = {6, 2, 5, 3}, shote = 1;

            ans[y][x] = idx++;
            x += dx[shote] * (t - 1);
            y += dy[shote] * (t - 1);

            int d = 0, d2 = t - 1;
            while (true) {
                if (x < 0 || x >= N || y < 0 || y >= N || ans[y][x] != 0) break;
                ans[y][x] = idx++;
                if (d % 2 == 0)
                    x += dx[jun[d]] * d2, y += dy[jun[d]] * d2, d2--;
                else
                    x += dx[jun[d]], y += dy[jun[d]];
                d++, d %= 4;
            }

            if (type == 0)
                x = px - 1, y = py + 1;
            else
                x = px + 1, y = py + 1;

            type = 1 - type;
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%5d ", ans[i][j]);
        }
        cout << endl;
    }
}
0