結果

問題 No.217 魔方陣を作ろう
ユーザー mizzsigmizzsig
提出日時 2016-10-15 14:20:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,197 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 68,796 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-14 13:53:43
合計ジャッジ時間 1,965 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
			
			// U
			if(((i == (n / 4) && (j == (n / 4))) || ((i == (n / 4 + 1)) && (j != (n / 4)))) || ((i == (n/4)) && (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;
			}
			// X
			else if(i > (n / 4 + 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;
			}
			// 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;
}
0