結果
問題 |
No.401 数字の渦巻き
|
ユーザー |
|
提出日時 | 2020-09-19 16:08:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 921 bytes |
コンパイル時間 | 2,454 ms |
コンパイル使用メモリ | 201,304 KB |
最終ジャッジ日時 | 2025-01-14 18:47:38 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <bits/stdc++.h> #define rep(i, ss, ee) for (int i = ss; i < ee; ++i) using namespace std; // 進行方向 // 0 1 2 3 = d%4 // 右下左上 int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; using vi = vector<int>; using vvi = vector<vi>; vvi dp; int N; void input() { cin >> N; } void output() { rep(i, 0, N) { rep(j, 0, N) { printf("%03d", dp[i][j]); j == N - 1 ? printf("\n") : printf(" "); } } } void solve() { dp.resize(N, vi(N, 0)); int d = 0; int p = 2; int y = 0; int x = 0; dp[y][x] = 1; while (p <= N * N) { while (true) { int ny = y + dy[d]; int nx = x + dx[d]; if (0 <= ny && ny < N && 0 <= nx && nx < N && dp[ny][nx] == 0) { dp[ny][nx] = p; p++; y = ny; x = nx; } else { break; } } //進行方向 d++; d %= 4; } } int main() { input(); solve(); output(); }