結果

問題 No.217 魔方陣を作ろう
ユーザー IL_mstaIL_msta
提出日時 2015-08-13 01:32:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,788 bytes
コンパイル時間 670 ms
コンパイル使用メモリ 88,840 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 11:21:01
合計ジャッジ時間 2,002 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 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,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 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,380 KB
testcase_17 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////

void ki(int* d,int N){
	int temp,sa,sb;
	sa = 0;
	sb = 0;
	rep(i,N*N){
		temp = (N+(N-i)%N+sa)%N * N + (i+N/2+(N+(-sb)%N)%N)%N;
		if( d[temp] == 0){
			d[temp] = i+1;
		}else{
			sa += 2;
			++sb ;
			temp = (N+(N-i)%N+sa)%N * N + (i+N/2+(N+(-sb)%N)%N)%N;
			d[temp] = i+1;
		}
	}
}

void view(int* d,int N){
	rep(i,N*N){
		cout << d[i];
		if( i % N == N-1){
			cout << '\n';
		}else{
			cout << ' ';
		}
	}
}
int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	const int dmax = 400;
	int d[dmax];
	int N;
	cin>>N;
	fill_n( d,N*N, 0);
	if( N%2 == 1){
		ki(d,N);
	}else if( N%4 == 0){
		rep(i,N)rep(j,N){
			if( (i%4)==(j%4) || 3-(i%4)==(j%4) ){
				d[i*N+j] = j+N*i+1;
			}
		}
		for(int i=0;i<N*N;++i){
			if( d[N*N-1-i] == 0){
				d[N*N-1-i] = i+1;
			}
		}
	}else{
		int temp;
		int dd[dmax];
		int n = N/2;
		int m = (N-2)/4;
		fill_n( dd,N*N, 0);
		ki(dd,n);
		rep(i,n)rep(j,n){
			d[i*N+j]		= dd[i*n+j];//A
			d[i*N+j+n]		= dd[i*n+j] +2*n*n;//B
			d[(i+n)*N +j]	= dd[i*n+j] +3*n*n;//C
			d[(i+n)*N +j+n]	= dd[i*n+j] +n*n;//D
		}
		//縦ラインはそろっている。
		//view(d,N);cout << endl;
		rep(i,n){//A<->C
			if(i==n/2){
				for(int j=1;j<=m;++j){
					temp			= d[(i+n)*N +j];
					d[(i+n)*N +j]	= d[i*N+j];
					d[i*N+j]		= temp;
				}
			}else{
				for(int j=0;j<m;++j){
					temp			= d[(i+n)*N+j];
					d[(i+n)*N+j]	= d[i*N+j];
					d[i*N+j]		= temp;
				}
			}
		}
		//view(d,N);cout << endl;
		//B<->D
		for(int j= m+2; j<2*m+1; ++j){
			rep(i,n){
				temp			= d[(i+n)*N+j+n];
				d[(i+n)*N +j+n]	= d[i*N+j+n];
				d[i*N+j+n]		= temp;
			}
		}
		
	}
	
	//表示
	rep(i,N*N){
		cout << d[i];
		if( i % N == N-1){
			cout << '\n';
		}else{
			cout << ' ';
		}
	}
	//check
	int sum = (1+N*N)*N*N/2/N;
	int ter;
	rep(i,N){
		ter = 0;
		rep(j,N){
			ter += d[i*N+j];
		}
		if( sum != ter){
			cout << i << "- " << ter << endl;
		}
	}
	rep(i,N){
		ter = 0;
		rep(j,N){
			ter += d[j*N + i];
		}
		if( sum != ter){
			cout << i << "| " << ter << endl;
		}
	}
	ter = 0;
	rep(i,N){
		ter += d[i*N+i];
	}
	if( sum != ter ){
		cout << "\" << ter << endl;
	}

	ter = 0;
	rep(i,N){
		ter += d[(N-1-i)*N+i];
	}
	if( sum != ter){
		cout << "/" << ter << endl;
	}
	return 0;
}
0