結果
問題 | No.217 魔方陣を作ろう |
ユーザー |
![]() |
提出日時 | 2016-12-06 19:03:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 3,896 bytes |
コンパイル時間 | 1,686 ms |
コンパイル使用メモリ | 181,808 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-28 03:13:54 |
合計ジャッジ時間 | 2,439 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>using namespace std;using pii = pair<int,int>;using ll = long long;#define rep(i, j) for(int i=0; i < (int)(j); i++)#define rrep(i, j) for(int i=(int)(j-1); i >=0; i--)#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)#define all(v) v.begin(),v.end()#define debug(x) cerr << #x << " : " << x << endltemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }template<class T> bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; }// vectortemplate<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }// pairtemplate<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }const int INF = 1 << 30;const ll INFL = 1LL << 60;class Solver {public:vector<vector<int>> odd_magic_square(int N) {assert(N % 2);vector<vector<int>> G(N, vector<int>(N));int nx = N / 2;int ny = 0;rep(i, N * N) {G[ny][nx] = i + 1;while(i != N * N - 1 and G[ny][nx]) {int nny = (ny + N - 1) % N;int nnx = (nx + 1) % N;if(G[nny][nnx]) ny = (ny + 1) % N;else nx = nnx, ny = nny;}}return G;}vector<vector<int>> four_times_magic_square(int N) {assert(N % 4 == 0);vector<vector<int>> G(N, vector<int>(N));vector<bool> used(N * N);rep(y, N) rep(x, N) {if(x % 4 == y % 4 or x % 4 == 3 - y % 4) {int n = y * N + x + 1;G[y][x] = n;used[n] = true;}}int n = 1;rrep(y, N) rrep(x, N) {if(not(x % 4 == y % 4 or x % 4 == 3 - y % 4)) {while(used[n]) n++;G[y][x] = n;used[n] = true;}}return G;}vector<vector<int>> four_times_plus_two_magic_square(int N) {assert(N % 4 == 2);int NN = N / 2;vector<vector<int>> G1 = odd_magic_square(NN);for(auto &l : G1)for(int &i : l)i = (i - 1) * 4;vector<vector<int>> G(N, vector<int>(N));const vector<vector<int>> L = {{4, 1},{2, 3}}, U = {{1, 4},{2, 3},}, X = {{1, 4},{3, 2}};int center = NN / 2;for(int y = 0; y < N; y += 2)for(int x = 0; x < N; x += 2) {vector<vector<int>> A;int yy = y / 2, xx = x / 2;if(yy <= center) A = L;else if(yy == center + 1) A = U;else A = X;if(yy == center and xx == center) A = U;if(yy == center + 1 and xx == center) A = L;rep(dy, 2) rep(dx, 2) {G[y + dy][x + dx] = G1[yy][xx] + A[dy][dx];}}return G;}bool solve() {int N; cin >> N;vector<vector<int>> ans;if(N % 2) {ans = odd_magic_square(N);} else if(N % 4 == 0) {ans = four_times_magic_square(N);} else if(N % 4 == 2) {ans = four_times_plus_two_magic_square(N);} else {assert(0); // 2}rep(y, N) {rep(x, N) cout << ans[y][x] << (x == N - 1 ? "\n" : " ");}return 0;}};int main() {cin.tie(0);ios::sync_with_stdio(false);Solver s;s.solve();return 0;}