結果

問題 No.3092 Tired Queen
ユーザー levyxx
提出日時 2025-04-06 17:01:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,015 bytes
コンパイル時間 4,036 ms
コンパイル使用メモリ 254,440 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2025-04-06 17:01:31
合計ジャッジ時間 8,001 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#include <atcoder/all>
using namespace atcoder;
using mint1 = modint1000000007;
using mint2 = modint998244353;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ld pi=3.141592653589793;
const int inf=2e9;
const ll linf=2e18;
const ld eps=1e-14;
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl

ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

struct edge{
    ll to;
    ll leng;
};

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

    ll n;
    cin >> n;
    if(n==1) {
        cout << 1 << endl;
        return 0;
    }else if(n==2) {
        cout << "1 3" << endl;
        cout << "4 2" << endl;
        return 0;
    }else if(n==3) {
        cout << "1 4 8" << endl;
        cout << "6 2 3" << endl;
        cout << "7 5 9" << endl;
        return 0;
    }
    vector<vector<ll>> a(n,vector<ll>(n));
    a[0][0]=1,a[0][1]=4,a[0][2]=8;
    a[1][0]=6,a[1][1]=2,a[1][2]=3;
    a[2][0]=7,a[2][1]=5,a[2][2]=9;
    ll x=2,y=2;
    ll now=9;
    for(ll i=3; i<n; i++) {
        if(i%2==0) {
            x++;
            now++;
            a[x][y]=now;
        }else {
            x++;
            y++;
            now++;
            a[x][y]=now;
        }
        while(now<(i+1)*(i+1)) {
            if(i%2==0) {
                if(now==i*i+1) {
                    swap(x,y);
                    now++;
                    a[x][y]=now;
                }else if(now==i*i+2) {
                    x++;
                    now++;
                    a[x][y]=now;
                }
                if(now%2==0) {
                    if(x>y) {
                        y++;
                        now++;
                        a[x][y]=now;
                    }else {
                        x++;
                        now++;
                        a[x][y]=now;
                    }
                }else {
                    swap(x,y);
                    now++;
                    a[x][y]=now;
                }
            }else {
                if(now==i*i+1) {
                    x--;
                    now++;
                    a[x][y]=now;
                }else {
                    if(now%2==0) {
                        if(y<n-1 && a[x][y+1]!=0) {
                            y--;
                            now++;
                            a[x][y]=now;
                        }else if(x<n-1 && a[x+1][y]!=0) {
                            x--;
                            now++;
                            a[x][y]=now;
                        }
                    }else {
                        swap(x,y);
                        now++;
                        a[x][y]=now;
                    }
                }
            }
        }

    }
    for(ll i=0; i<n; i++) {
        for(ll j=0; j<n; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

}
0