結果

問題 No.217 魔方陣を作ろう
ユーザー FF256grhyFF256grhy
提出日時 2015-05-27 23:21:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,757 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 26,540 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 16:55:13
合計ジャッジ時間 1,529 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <stdio.h>

int value(int, int, int);

int main(void) {
	int i, j, n;
	scanf("%d", &n);
	for(i = 0; i < n; i++) {
	for(j = 0; j < n; j++) {
		printf("%3d ", value(n, i, j) + 1 );
	} printf("\n");
	}
	
	/* 検算用
	for(i = 0; i < n; i++) { int sum = 0;
	for(j = 0; j < n; j++) {
		sum += value(n, i, j) + 1;
	}	printf("yoko: %d\n", sum);
	}
	
	for(j = 0; j < n; j++) { int sum = 0;
	for(i = 0; i < n; i++) {
		sum += value(n, i, j) + 1;
	}	printf("tate: %d\n", sum);
	}
	
	int sum_a = 0, sum_b = 0;
	for(i = 0; i < n; i++) {
		sum_a += value(n, i, i) + 1;
		sum_b += value(n, i, n - 1 - i) + 1;
	}
	printf("naname: %d, %d\n", sum_a, sum_b);
	*/
	
	return 0;
}

int four[4][4] = {
	{ 0, 14, 13,  3}, 
	{11,  5,  6,  8}, 
	{ 7,  9, 10,  4}, 
	{12,  2,  1, 15}  
};

int pattern[7][2][2] = {
	{ {0, 1}, {2, 3} }, // 0
	{ {3, 2}, {1, 0} }, // 1
	{ {0, 3}, {2, 1} }, // 2
	{ {3, 0}, {1, 2} }, // 3
	{ {3, 1}, {0, 2} }, // 4
	{ {0, 2}, {3, 1} }, // 5
	{ {3, 0}, {2, 1} }  // 6
};

int value(int size, int i, int j) {
	if(size == 4) { return four[i][j]; }
	if(size % 2 == 1) {
		/* sizeが奇数の魔方陣は、簡単な生成法を偶然知っていたので、それを使う
			            04
			     .___03____09___.                .______________.
			     |02    08    14|                |-- 15 -- 21 --|
			   01|   07    13   |19              |19 -- 20 -- 01|
			00   |06    12    18|   24   ---->   |-- 24 -- 00 --|
			   05|   11    17   |23              |23 -- 04 -- 05|
			     |10    16    22|                |-- 03 -- 09 --|
			     '"""15""""21"""'                '""""""""""""""'
			            20
		*/
		
		int x, y, r; // i:↓, j:→, x:右上, y:右下    例) (x, y) = (3, 2) が↑の 13 に相当
		r = ((i + j) % 2) * size;
		x = ( (size / 2) + (j - i + r) / 2 ) % size; // この部分超わかりにくい感じする
		y = (          0 + (j + i + r) / 2 ) % size;
		return x + size * y;
	} else {
		/* N*Nの魔方陣をもとに、2N*2Nの魔方陣を作る方法をさっき見つけたので、それを使う
			size / 2:
			even:          odd:
			0 1 0 1 0 1    0 1 0 1 0 1 2
			1 0 1 0 1 0    1 0 1 0 1 0 6
			0 1 0 1 0 1    0 1 0 1 0 1 2
			1 0 1 0 1 0    1 0 1 0 1 0 3
			0 1 0 1 0 1    0 1 0 1 0 1 2
			1 0 1 0 1 0    1 0 1 0 1 0 3
			               4 4 4 5 4 5 0
		*/
		
		int k;
		if( (size / 2) % 2 == 1 && ( (size - 2 <= j) ^ (size - 2 <= i) ) ) {
			if(size - 2 <= j) {
				if( (i / 2) % 2 == 0 ) { k = 2; } else if(i / 2 != 1) { k = 3; } else { k = 6; }
			} else {
				if( (j / 2) % 2 == 0 ) { k = 4; } else if(j / 2 != 1) { k = 5; } else { k = 4; }
			}
		} else {
			if( (i / 2 + j / 2) % 2 == 0 ) { k = 0; } else { k = 1; }
		}
		return value(size / 2, i / 2, j / 2) * 4 + pattern[k][ i % 2 ][ j % 2 ];
	}
}
0