結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
mizzsig
|
| 提出日時 | 2016-10-15 13:50:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,158 bytes |
| コンパイル時間 | 756 ms |
| コンパイル使用メモリ | 66,376 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-22 11:29:33 |
| 合計ジャッジ時間 | 1,680 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 3 |
ソースコード
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void Hindu(vector<vector<int>> &a, int n){
int x = n / 2;
int y = 0;
int num = 1;
int end = n * n;
while(num <= end){
a[y][x] = num++;
x++;
y--;
if(x == n){
x = 0;
}
if(y < 0){
y = n - 1;
}
if(a[y][x] != 0){
x = (x+n-1) % n;
y = (y+2) % n;
}
}
}
void Diagonal2(vector<vector<int>> &a, int n, int x, int y, int dx){
while((x >= 0) && (x < n) && (y >= 0) && (y < n)){
a[y][x] = y * n + x + 1;
x += dx;
y++;
}
}
void Diagonal(vector<vector<int>> &a, int n){
for(int i = 0; i < n; i += 4){
Diagonal2(a, n, 0, i, 1);
Diagonal2(a, n, i, 0, 1);
}
for(int i = 3; i < n; i += 4){
Diagonal2(a, n, n-1, i-3, -1);
Diagonal2(a, n, i, 0, -1);
}
for(int x = 0; x < n; x++){
for(int y = 0; y < n; y++){
if(a[y][x] == 0){
a[y][x] = (n * n) - (y * n + x);
}
}
}
}
void LUX(vector<vector<int>> &a, int n){
vector<vector<int>> b(n / 2, vector<int>(n / 2));
Hindu(b, n / 2);
for(int i = 0; i < (n / 2); i++){
for(int j = 0; j < (n / 2); j++){
b[i][j]--;
b[i][j] *= 4;
// X
if(i == (n/2-1)){
a[i * 2][j * 2] = b[i][j] + 1;
a[i * 2 + 1][j * 2] = b[i][j] + 3;
a[i * 2][j * 2 + 1] = b[i][j] + 4;
a[i * 2 + 1][j * 2 + 1] = b[i][j] + 2;
}
// U
else if((i == (n / 4) && (j == (n / 4))) || ((i == (n / 4 + 1)) && (j != (n / 4)))){
a[i * 2][j * 2] = b[i][j] + 1;
a[i * 2 + 1][j * 2] = b[i][j] + 2;
a[i * 2][j * 2 + 1] = b[i][j] + 4;
a[i * 2 + 1][j * 2 + 1] = b[i][j] + 3;
}
// L
else{
a[i * 2][j * 2] = b[i][j] + 4;
a[i * 2 + 1][j * 2] = b[i][j] + 2;
a[i * 2][j * 2 + 1] = b[i][j] + 1;
a[i * 2 + 1][j * 2 + 1] = b[i][j] + 3;
}
}
}
}
int main() {
int n;
cin >> n;
vector<vector<int>> a(n, vector<int>(n));
for(int x = 0; x < n; x++){
for(int y = 0; y < n; y++){
a[y][x] = 0;
}
}
if(n % 2 == 1){
Hindu(a, n);
}else if(n % 4 == 0){
Diagonal(a, n);
}else{
LUX(a, n);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
mizzsig