結果
問題 | No.401 数字の渦巻き |
ユーザー |
|
提出日時 | 2024-01-25 12:38:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,060 bytes |
コンパイル時間 | 854 ms |
コンパイル使用メモリ | 75,248 KB |
最終ジャッジ日時 | 2025-02-18 22:38:30 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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));A[0][0] = 1;int H = 0, W = 0;int cnt = 2;int d = 0;while (1) {int nh = H + dy[d];int nw = W + dx[d];if (0 <= nh && nh < N && 0 <= nw && nw < N && A[nh][nw] == 0) {H = nh;W = nw;A[H][W] = cnt;cnt++;} else {d++;d %= 4;bool ok = false;if (H + 1 < N && A[H + 1][W] == 0) {ok = true;}if (H - 1 >= 0 && A[H - 1][W] == 0) {ok = true;}if (W + 1 < N && A[H][W + 1] == 0) {ok = true;}if (W - 1 >= 0 && A[H][W - 1] == 0) {ok = true;}if (!ok) {break;}}}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(" ");}}}}