結果

問題 No.401 数字の渦巻き
ユーザー @abcde@abcde
提出日時 2019-02-20 22:21:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,382 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 147,836 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-07 15:19:48
合計ジャッジ時間 2,567 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    
    // 1. 入力情報取得.
    int N;
    cin >> N;
    
    // 2. 数字の渦巻きを作る?
    // ex.
    // N = 4 の 場合.
    // 4 -> 3 -> 3 -> 2 -> 2 -> 1 -> 1
    // したがって, 以下のような数列をイメージすることになる.
    // N -> N - 1 -> N - 1 -> N - 2 -> N - 2 -> ... -> 2 -> 2 -> 1 -> 1
    vector<int> v;
    int count = N;
    v.push_back(N - 1);  // N だと上手く行かなかった.
    count--;
    while(count > 0) v.push_back(count), v.push_back(count), count--;
    // for(auto &p : v) cout << p << " " << endl;
    
    // 3. 二次元配列(r, c)成分は, 何番目?
    // dr, dc を使って, 右 -> 下 -> 左 -> 上 -> 右 -> ... を繰り返す方針で考える.
    int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
    int ans[N][N];
    int index = 1;
    ans[0][0] = index;
    // (前回)行, 列インデックス.
    int befR = 0, befC = 0;
    // 上下左右の向き判定フラグ.
    int flag = 0;
    for(auto &p : v){
        // 3-1. (前回)行, 列インデックス.
        int curR = befR, curC = befC;
        
        // 3-2. 二次元配列更新.
        for(int i = 0; i < p; i++){
            curR += dr[flag], curC += dc[flag];
            ans[curR][curC] = ++index;
            // cout << "curR:" << curR << " curC:" << curC << " index:" << index << endl;
            // ex.
            // N = 3 の 場合.
            // curR:0 curC:1 index:2
            // curR:0 curC:2 index:3
            // curR:1 curC:2 index:4
            // curR:2 curC:2 index:5
            // curR:2 curC:1 index:6
            // curR:2 curC:0 index:7
            // curR:1 curC:0 index:8
            // curR:1 curC:1 index:9
        }
        
        // 3-3. flag更新.
        (++flag) %= 4;
        // cout << "flag=" << flag << endl;
        
        // 3-4. 前回分更新.
        befR = curR, befC = curC;
    }
    
    // 4. 出力.
    // for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) cout << ans[i][j] << " ";
    // ex.
    // N = 3 の 場合.
    // -> 1 2 3 8 9 4 7 6 5 

    // string ans = "";
    // cout << ans << endl;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            cout << setfill('0') << setw(3) << ans[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
    
}
0