結果

問題 No.217 魔方陣を作ろう
ユーザー 158b158b
提出日時 2015-05-26 23:35:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,979 bytes
コンパイル時間 1,146 ms
コンパイル使用メモリ 67,660 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 15:23:52
合計ジャッジ時間 1,637 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <limits.h>
#include <vector>
#include <numeric>
using namespace std;


	
void func(int n, int field[20][20]){
	int nextx,nexty;
	int y,x;
	
	y = 0;
	x = n / 2;
	field[y][x] = 1;
	for(int i=2; i<=n*n; i++){
		//次へ
		nextx = (x + 1) % n;
		nexty = y - 1;
		if(nexty == -1){
			nexty = n - 1;
		}
		
		if(field[nexty][nextx] == 0){
			field[nexty][nextx] = i;
			y = nexty;
			x = nextx;
		}else{
			y = (y + 1) % n;
			field[y][x] = i;
		}
	}
}

int main(){
	int field[20][20] = {0};
	int field2[20][20] = {0};
	int n; //3-20
	int x,y;
	int nextx,nexty;
	bool first[4][4] = {{1,0,0,1},{0,1,1,0},{0,1,1,0},{1,0,0,1}};
	int last[20][20] = {0};	//0L 1U 2X
	int adjust[3][2][2] = 
		{{{4,1},{2,3}},
		 {{1,4},{2,3}},
		 {{1,4},{3,2}}};
	int i;
	
	cin >> n;
	if(n % 2 == 1){
		func(n, field);
	}else if(n % 4 == 0){
		i = 0;
		for(int iy=0; iy<n; iy++){
			for(int ix=0; ix<n; ix++){
				i ++;
				if(first[iy%4][ix%4]){
					field[iy][ix] = i;
				}
			}
		}
		
		i = 0;
		for(int iy=n-1; iy>=0; iy--){
			for(int ix=n-1; ix>=0; ix--){
				i ++;
				if(!first[iy%4][ix%4]){
					field[iy][ix] = i;
				}
			}
		}
	}else if(n % 4 == 2){	//4n+2
		func(n/2, field2);
		for(int iy=0; iy<n/2; iy++){
			for(int ix=0; ix<n/2; ix++){
				field2[iy][ix] = (field2[iy][ix] - 1) * 4;
			}
		}
		
		//last表作り(n/2)
		//0L 1U 2X
		n = n/2;
		for(int ix=0; ix<n; ix++){
			last[n/2+1][ix] = 1;
		}
		for(int iy=n/2+2; iy<n; iy++){
			for(int ix=0; ix<n; ix++){
				last[iy][ix] = 2;
			}
		}
		swap(last[n/2][n/2],last[n/2+1][n/2]);
		
		//作成
		n *= 2;
		int hy,hx;
		for(int iy=0; iy<n; iy++){
			for(int ix=0; ix<n; ix++){
				hy = iy / 2;
				hx = ix / 2;
				field[iy][ix] = field2[hy][hx] + adjust[last[hy][hx]][iy%2][ix%2];
			}
		}
	}
	
	//出力
	for(int iy=0; iy<n; iy++){
		for(int ix=0; ix<n; ix++){
			cout << field[iy][ix] << " ";
		}
		cout << endl;
	}
	cout << endl;
	return 0;
}
0