結果
問題 |
No.401 数字の渦巻き
|
ユーザー |
|
提出日時 | 2024-01-25 12:48:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 6 ms / 2,000 ms |
コード長 | 777 bytes |
コンパイル時間 | 635 ms |
コンパイル使用メモリ | 74,980 KB |
最終ジャッジ日時 | 2025-02-18 22:38:33 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include<iostream> #include<vector> using namespace std; const vector<int> dx = {1, 0, -1, 0}; const vector<int> dy = {0, -1, 0, 1}; int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N, 0)); int H = 0, W = 0; int d = 0; for (int i = 1; i <= N * N; i++) {//ループ回数は決まっている。 A[H][W] = i; if (i == N * N) break; while (1) { int nh = H + dy[d], nw = W + dx[d]; if (0 <= nh && nh < N && 0 <= nw && nw < N && A[nh][nw] == 0) { H = nh, W = nw; break; } d++; d %= 4; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%03d", A[i][j]); if (j == N - 1) { printf("\n"); } else { printf(" "); } } } }