結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
data9824
|
| 提出日時 | 2015-05-26 23:16:51 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 2,133 bytes |
| コンパイル時間 | 622 ms |
| コンパイル使用メモリ | 69,208 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-06 10:23:43 |
| 合計ジャッジ時間 | 1,464 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > odd(int n) {
vector<vector<int> > a(n, vector<int>(n));
int y = 0;
int x = n / 2;
for (int i = 1; i <= n * n; ++i) {
a[y][x] = i;
int nextY = (y + n - 1) % n;
int nextX = (x + 1) % n;
if (a[nextY][nextX] == 0) {
y = nextY;
x = nextX;
} else {
y = (y + 1) % n;
}
}
return a;
}
int main() {
int n;
cin >> n;
vector<vector<int> > a(n, vector<int>(n));
if (n % 2 == 1) {
a = odd(n);
} else if (n % 4 == 0) {
int i = 1;
for (int y = 0; y < n; ++y) {
for (int x = 0; x < n; ++x) {
if ((y + 1) % 4 < 2) {
if ((x + 1) % 4 < 2) {
a[y][x] = i;
}
} else {
if ((x + 1) % 4 >= 2) {
a[y][x] = i;
}
}
++i;
}
}
i = 1;
for (int y = n - 1; y >= 0; --y) {
for (int x = n - 1; x >= 0; --x) {
if ((y + 1) % 4 < 2) {
if ((x + 1) % 4 >= 2) {
a[y][x] = i;
}
} else {
if ((x + 1) % 4 < 2) {
a[y][x] = i;
}
}
++i;
}
}
} else if (n % 4 == 2) {
int smaller = (n / 4) * 2 + 1;
vector<vector<int> > base = odd(smaller);
for (int y = 0; y < smaller; ++y) {
for (int x = 0; x < smaller; ++x) {
base[y][x] = (base[y][x] - 1) * 4;
}
}
enum Type {
U, L, X
};
vector<vector<Type> > types(smaller, vector<Type>(smaller, L));
for (int x = 0; x < smaller; ++x) {
types[smaller / 2 + 1][x] = U;
}
for (int y = smaller / 2 + 2; y < smaller; ++y) {
for (int x = 0; x < smaller; ++x) {
types[y][x] = X;
}
}
types[smaller / 2][smaller / 2] = U;
types[smaller / 2 + 1][smaller / 2] = L;
int offsetL[2][2] = { 4, 1, 2, 3 };
int offsetU[2][2] = { 1, 4, 2, 3 };
int offsetX[2][2] = { 1, 4, 3, 2 };
for (int y = 0; y < n; ++y) {
for (int x = 0; x < n; ++x) {
Type type = types[y / 2][x / 2];
a[y][x] = base[y / 2][x / 2]
+ (type == L ? offsetL[y % 2][x % 2]
: type == U ? offsetU[y % 2][x % 2]
: offsetX[y % 2][x % 2]);
}
}
}
for (int y = 0; y < n; ++y) {
for (int x = 0; x < n; ++x) {
cout << a[y][x] << " ";
}
cout << endl;
}
return 0;
}
data9824