結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
Lepton_s
|
| 提出日時 | 2015-05-26 23:05:43 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,681 bytes |
| コンパイル時間 | 777 ms |
| コンパイル使用メモリ | 84,256 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-06 10:16:50 |
| 合計ジャッジ時間 | 1,386 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 |
ソースコード
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<25;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;
#define N 22
int a[N][N], b[N][N], c[N][N];
int d[3][4][4] = {
{{4, 1}, {2, 3}},
{{1, 4}, {2, 3}},
{{1, 4}, {3, 2}}};
void mk(int a[N][N], int n){
if(n&1){
int m = n/2;
int pa = 0, pb = m;
for(int i = 1; i <= n*n; i++){
a[pa][pb] = i;
pa = (pa-1+n)%n;
pb = (pb+1)%n;
if(a[pa][pb]){
pa = (pa+2)%n;
pb = (pb-1+n)%n;
}
}
return;
}
if(n%4 == 0){
for(int i = 1; i <= n*n; i++){
int x = (i-1)/n, y = (i-1)%n;
if(!((x%4==0||x%4==3)^(y%4==0||y%4==3))) a[x][y] = i;
else a[x][y] = n*n-i+1;
}
return;
}
if(n%4 == 2){
int m = n/2;
mk(b, m);
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
b[i][j] = 4*(b[i][j]-1);
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
c[i][j] = (i<=m/2?0:(i==m/2+1?1:2));
swap(c[m/2][m/2], c[m/2+1][m/2]);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int x = i/2, y = j/2;
int x2 = i%2, y2 = j%2;
a[i][j] = b[x][y] + d[c[x][y]][x2][y2];
}
}
return;
}
}
int main(){
int n;
cin>>n;
mk(a, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
Lepton_s