結果
問題 |
No.401 数字の渦巻き
|
ユーザー |
|
提出日時 | 2019-02-02 23:00:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 868 bytes |
コンパイル時間 | 1,434 ms |
コンパイル使用メモリ | 167,276 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-29 17:40:04 |
合計ジャッジ時間 | 2,568 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <bits/stdc++.h> using namespace std; int n; int ans[31][31]; int x, y, d; int dx[4] = {0, 1, 0, -1}; int dy[4] = {-1,0, 1, 0}; void next(){ int xx = x + dx[d]; int yy = y + dy[d]; if(xx < 0 || n <= xx){ d = (d+1)%4; xx = x + dx[d]; yy = y + dy[d]; } else if(yy < 0 || n <= yy){ d = (d+1)%4; xx = x + dx[d]; yy = y + dy[d]; } else if(ans[yy][xx] != 0){ d = (d+1)%4; xx = x + dx[d]; yy = y + dy[d]; } x = xx; y = yy; } int main(){ cin >> n; int idx = 1; for(int i=0; i<n*n; i++){ ans[y][x] = idx; idx++; next(); } for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(j == n-1) printf("%03d\n", ans[i][j]); else printf("%03d ", ans[i][j]); } } return 0; }