結果

問題 No.217 魔方陣を作ろう
ユーザー Lepton_sLepton_s
提出日時 2015-05-26 23:05:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,681 bytes
コンパイル時間 1,864 ms
コンパイル使用メモリ 77,764 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 15:08:54
合計ジャッジ時間 1,758 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0