結果

問題 No.401 数字の渦巻き
ユーザー k
提出日時 2020-06-16 02:07:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 706 bytes
コンパイル時間 1,890 ms
コンパイル使用メモリ 197,136 KB
最終ジャッジ日時 2025-01-11 04:35:54
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

bool check(int x, int y, const vector<vector<int> > &bd) {
  if (x < 0 || x >= bd.size()) return false;
  if (y < 0 || y >= bd.size()) return false;
  return bd[y][x] == 0;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int> > bd(n, vector<int>(n));

  int v = 0, x = 0, y = 0;
  int cnt = 1;
  while (cnt <= n * n) {
    bd[y][x] = cnt;
    if (!check(x + dx[v], y + dy[v], bd)) {
      v = (v + 1) % 4;
    }
    y += dy[v];
    x += dx[v];
    ++cnt;
  }
  
  REP (i, n) REP (j, n) printf("%03d%c", bd[i][j], j == n-1 ? '\n' : ' ');

  return 0;
}
0