結果

問題 No.217 魔方陣を作ろう
ユーザー yuppe19 😺yuppe19 😺
提出日時 2015-05-27 12:59:07
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,173 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 28,896 KB
最終ジャッジ日時 2024-11-14 19:03:51
合計ジャッジ時間 824 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:8:1: error: ‘vector’ does not name a type
    8 | vector<vector<int>> solve1(int n) {
      | ^~~~~~
main.cpp:24:1: error: ‘vector’ does not name a type
   24 | vector<vector<int>> solve2(int n) {
      | ^~~~~~
main.cpp:39:1: error: ‘vector’ does not name a type
   39 | vector<vector<int>> solve3(int n) {
      | ^~~~~~
main.cpp:73:1: error: ‘vector’ does not name a type
   73 | vector<vector<int>> solve4(int n) {
      | ^~~~~~
main.cpp: In function ‘int main()’:
main.cpp:104:3: error: ‘vector’ was not declared in this scope
  104 |   vector<vector<int>> res;
      |   ^~~~~~
main.cpp:3:1: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
    2 | #include <algorithm>
  +++ |+#include <vector>
    3 | using namespace std;
main.cpp:104:17: error: expected primary-expression before ‘int’
  104 |   vector<vector<int>> res;
      |                 ^~~
main.cpp:106:5: error: ‘res’ was not declared in this scope
  106 |     res = solve1(n);
      |     ^~~
main.cpp:106:11: error: ‘solve1’ was not declared in this scope
  106 |     res = solve1(n);
      |           ^~~~~~
main.cpp:108:5: error: ‘res’ was not declared in this scope
  108 |     res = solve2(n);
      |     ^~~
main.cpp:108:11: error: ‘solve2’ was not declared in this scope
  108 |     res = solve2(n);
      |           ^~~~~~
main.cpp:111:5: error: ‘res’ was not declared in this scope
  111 |     res = solve4(n);
      |     ^~~
main.cpp:111:11: error: ‘solve4’ was not declared in this scope
  111 |     res = solve4(n);
      |           ^~~~~~
main.cpp:115:22: error: ‘res’ was not declared in this scope
  115 |       printf("%d%c", res[i][j], j==n-1?'\n':' ');
      |                      ^~~
main.cpp:103:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  103 |   int n; scanf("%d", &n);
      |      

ソースコード

diff #

#include <cstdio>
#include <algorithm>
using namespace std;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};

vector<vector<int>> solve1(int n) {
  vector<vector<int>> res(n, vector<int>(n));
  int i = 0, j = n / 2;
  for(int k : range(n*n)) {
    res[i][j] = k + 1;
    int ni = (i - 1 + n) % n;
    int nj = (j + 1) % n;
    if(res[ni][nj] > 0) {
      ni = (i + 1) % n;
      nj = j;
    }
    i = ni, j = nj;
  }
  return res;
}

vector<vector<int>> solve2(int n) {
  vector<vector<int>> res(n, vector<int>(n));
  for(int k : range(n*n)) {
    int i = k / n;
    int j = k % n;
    if((i + j) % 4 == 3 || (i - j) % 4 == 0) {
      res[i][j] = k + 1;
    } else {
      res[n-1-i][n-1-j] = k + 1;
    }
  }
  return res;
}

// (ΦωΦ)<関孝和の方法
vector<vector<int>> solve3(int n) {
  int n2 = n - 2;
  vector<vector<int>> res(n, vector<int>(n));
  vector<vector<int>> inner = solve2(n2);
  for(int i : range(n2)) {
    for(int j : range(n2)) {
      res[i+1][j+1] = inner[i][j] + 2*n - 2;
    }
  }
  for(int k : range(n2)) {
    res[n-1][k+2] = k + 1;
    res[n-1-k][0] = n2 + k + 1;
  }
  res[n-1][1] = 2 * n2 + 1;
  res[1][0] = 2 * n2 + 2;
  for(int k : range(n)) {
    int target = (k==0||k==n-1) ? res[n-1][n-1-k] : res[n-1][k];
    res[0][k] = n*n+1 - target;
  }
  for(int k : range(1, n)) {
    int target = (k==n-1) ? res[0][0] : res[k][0];
    res[k][n-1] = n*n+1 - target;
  }
  swap(res[0][1], res[n-1][1]);
  for(int j : range(2, n-1)) {
    if((j-2)%4 < 2) { swap(res[0][j], res[n-1][j]); }
  }
  for(int i : range(1, n-1)) {
    if(i%4 < 2) { swap(res[i][0], res[i][n-1]); }
  }
  return res;
}

// (ΦωΦ)<LUX
vector<vector<int>> solve4(int n) {
  int half = n / 2;
  int quo = half / 2;
  vector<vector<int>> small = solve1(half);
  vector<vector<int>> L = {{4, 1}, {2, 3}},
                      U = {{1, 4}, {2, 3}},
                      X = {{1, 4}, {3, 2}};
  vector<vector<vector<vector<int>>>> add(half, vector<vector<vector<int>>>(half, vector<vector<int>>(2, vector<int>(2))));
  for(int i : range(half)) {
    vector<vector<int>> target;
    if(i < quo+1) { target = L; }
    else if(i == quo+1) { target = U; }
    else { target = X; }
    for(int j : range(half)) { add[i][j] = target; }
  }
  swap(add[quo][quo], add[quo+1][quo]);
  vector<vector<int>> res(n, vector<int>(n));
  for(int i : range(half)) {
    for(int j : range(half)) {
      int x = (small[i][j] - 1) * 4;
      res[2*i]  [2*j]   = x + add[i][j][0][0];
      res[2*i]  [2*j+1] = x + add[i][j][0][1];
      res[2*i+1][2*j]   = x + add[i][j][1][0];
      res[2*i+1][2*j+1] = x + add[i][j][1][1];
    }
  }
  return res;
}

int main(void) {
  int n; scanf("%d", &n);
  vector<vector<int>> res;
  if(n & 1) {
    res = solve1(n);
  } else if(n % 4 == 0) {
    res = solve2(n);
  } else {
//    res = solve3(n);
    res = solve4(n);
  }
  for(int i : range(n)) {
    for(int j : range(n)) {
      printf("%d%c", res[i][j], j==n-1?'\n':' ');
    }
  }
  return 0;
}
0