結果

問題 No.401 数字の渦巻き
ユーザー uenoku
提出日時 2016-09-20 21:33:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 696 bytes
コンパイル時間 436 ms
コンパイル使用メモリ 54,208 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 10:13:24
合計ジャッジ時間 1,605 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>

using namespace std;
int ma[31][31];
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, -1, 0, 1};
int n;
void dfs(int v, int depth, int x, int y)
{
    ma[x][y] = depth;
    if (depth == n * n)
        return;
    int nx = vex[v] + x, ny = vey[v] + y;
    while (ma[nx][ny] || 0 > nx || nx >= n || ny < 0 || ny >= n) {
        v = (v + 1) % 4;
        nx = vex[v] + x, ny = vey[v] + y;
    }
    nx = vex[v] + x, ny = vey[v] + y;
    dfs(v, depth + 1, nx, ny);
}
int main()
{
    cin >> n;
    dfs(0, 1, 0, 0);
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
            printf("%03d ", ma[i][j]);
        }
        printf("\n");
    }
}
0