結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
codershifth
|
| 提出日時 | 2016-01-26 00:11:38 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,808 bytes |
| コンパイル時間 | 1,420 ms |
| コンパイル使用メモリ | 172,352 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 16:15:29 |
| 合計ジャッジ時間 | 2,444 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 WA * 4 |
ソースコード
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()
using namespace std;
class MakeMagicSquare
{
public:
typedef vector<vector<int>> Mat;
void report(const Mat &mat) {
int n = mat.size();
REP(i,n)
{
REP(j,n)
{
cout<<mat[i][j];
if (j < n-1)
cout<<" ";
}
cout<<endl;
}
}
Mat makeOddMS(int n) {
Mat mat(n,vector<int>(n,0));
int i,j;
i = 0;
j = n/2;
for (int k = 1; k <= n*n; ++k)
{
mat[i][j] = k;
int ii = i, jj = j;
i = (i-1+n)%n;
j = (j+1)%n;
if ( mat[i][j] > 0)
{
i = (ii+1)%n;
j = jj;
}
}
return mat;
}
Mat make4xMS(int n) {
Mat mat(n,vector<int>(n,0));
auto tmp(mat);
// 白黒ボードとして tmp を塗る
// 4x4 ブロックに区切り対角線部のみぬる
int m = n/2;
REP(dx,2)
REP(dy,2)
{
int x = m*dx;
int y = m*dy;
REP(i,m)
{
tmp[x+i][y+i] = 1;
tmp[x+i][(m+y)-1-i] = 1;
}
}
//REP(i,n){REP(j,n)cerr<<tmp[i][j];cerr<<endl;}
int x,y;
x = y = 0;
// 左上から右へブロックの対角線部のみ数字をおく
for (int k = 1; k <= n*n; ++k)
{
if ( tmp[y][x] )
mat[y][x] = k;
++x;
if ( x == n )
{
++y;
x = 0;
}
}
x = y = n-1;
for (int k = 1; k <= n*n; ++k)
{
if ( !tmp[y][x] )
mat[y][x] = k;
--x;
if ( x < 0 )
{
--y;
x = n-1;
}
}
return mat;
}
Mat LUX(int n) {
int m = n/2;
auto small = makeOddMS(m);
REP(i,m)
REP(j,m)
small[i][j] = 4*(small[i][j]-1);
vector<vector<char>> lux(m,vector<char>(m,'L'));
REP(i,m)
lux[m/2+1][i] = 'U';
FOR(i,m/2+2,m)
REP(j,m)
lux[i][j] = 'X';
swap(lux[m/2][m/2], lux[m/2+1][m/2]);
Mat mat(n,vector<int>(n,0));
const Mat L = {{4,1},
{2,3}};
const Mat U = {{1,4},
{2,3}};
const Mat X = {{1,4},
{3,2}};
REP(i,n)
REP(j,n)
{
int iy = i/2;
int ix = j/2;
int dy = i%2;
int dx = j%2;
switch ( lux[iy][ix] )
{
case 'L':
mat[i][j] = L[dy][dx] + small[iy][ix];
break;
case 'U':
mat[i][j] = U[dy][dx] + small[iy][ix];
break;
case 'X':
mat[i][j] = X[dy][dx] + small[iy][ix];
break;
}
}
return mat;
}
void solve(void)
{
int n;
cin>>n;
int N = n*(n*n+1)/2;
if (n % 2 == 1)
{
report(makeOddMS(n));
}
else if (n % 4 == 0)
{
report(make4xMS(n));
}
else if (n % 4 == 2)
{
report(LUX(n));
}
}
};
#if 1
int main(int argc, char *argv[])
{
ios::sync_with_stdio(false);
auto obj = new MakeMagicSquare();
obj->solve();
delete obj;
return 0;
}
#endif
codershifth