結果

問題 No.217 魔方陣を作ろう
ユーザー kakira9618kakira9618
提出日時 2015-05-26 23:38:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,558 bytes
コンパイル時間 816 ms
コンパイル使用メモリ 93,420 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-06 10:30:10
合計ジャッジ時間 1,618 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
int main(int argc, char const *argv[]) {
	
	int N;
	cin >> N;

	std::vector<vector<int> > m(N, vector<int>(N, 0));
	std::vector<vector<int> > ok(N, vector<int>(N, 0));


	if (N % 2 == 1) {
		int px = N / 2;
		int py = 0;

		int count = 0;
		while(count != N * N) {
			if (m[py][px] != 0) {
				py = (py + 1) % N;
				px = (px + N - 1) % N;
				py = (py + 1) % N;
			} else {
				m[py][px] = count + 1;
				px = (px + 1) % N;
				py = (py + N - 1) % N;
				count++;
			}
		}
	} else if (N % 4 == 0) {
		

		for (int i = 0; i < N / 4; ++i) {
			for (int j = 0; j < N / 4; ++j) {
				ok[i * 4][j * 4] = 1;
				ok[i * 4 + 1][j * 4 + 1] = 1;
				ok[i * 4 + 2][j * 4 + 2] = 1;
				ok[i * 4 + 3][j * 4 + 3] = 1;
				ok[i * 4 + 3][j * 4] = 1;
				ok[i * 4 + 2][j * 4 + 1] = 1;
				ok[i * 4 + 1][j * 4 + 2] = 1;
				ok[i * 4][j * 4 + 3] = 1;
			}
		}


		int count = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				count++;
				if (ok[i][j]) {
					m[i][j] = count;
				}
			}
		}
		count = 0;
		for (int i = N - 1; i >= 0; i--) {
			for (int j = N - 1; j >= 0; j--) {
				count++;
				if (!ok[i][j]) {
					m[i][j] = count;
				}
			}
		}
		
	} else {
		int n = N / 2;
		std::vector<vector<char> > lux(n, vector<char>(n, 0));
		std::vector<vector<int> > lux_m(n, vector<int>(n, 0));
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i <= n / 2) {lux[i][j] = 'l';} else
				if (i == n / 2 + 1) {lux[i][j] = 'u';} else
				{lux[i][j] = 'x';}
			}
		}

		char temp = lux[n / 2][n / 2];
		lux[n / 2][n / 2] = lux[n / 2 + 1][n / 2];
		lux[n / 2 + 1][n / 2] = temp;
		

		int px = n / 2;
		int py = 0;
		int count = 0;
		while(count != n * n) {
			if (lux_m[py][px] != 0) {
				py = (py + 1) % n;
				px = (px + n - 1) % n;
				py = (py + 1) % n;
			} else {
				lux_m[py][px] = count + 1;
				px = (px + 1) % n;
				py = (py + n - 1) % n;
				count++;
			}
		}

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				lux_m[i][j]--;
				lux_m[i][j] <<= 2;
			}
		}

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				int px = 2 * j;
				int py = 2 * i;
				if (lux[i][j] == 'l') {
					m[py][px] = lux_m[i][j] + 4;
					m[py + 1][px] = lux_m[i][j] + 2;
					m[py][px + 1] = lux_m[i][j] + 1;
					m[py + 1][px + 1] = lux_m[i][j] + 3;
				} else if (lux[i][j] == 'u') {
					m[py][px] = lux_m[i][j] + 1;
					m[py + 1][px] = lux_m[i][j] + 2;
					m[py][px + 1] = lux_m[i][j] + 4;
					m[py + 1][px + 1] = lux_m[i][j] + 3;
				} else {

					m[py][px] = lux_m[i][j] + 1;
					m[py + 1][px] = lux_m[i][j] + 3;
					m[py][px + 1] = lux_m[i][j] + 4;
					m[py + 1][px + 1] = lux_m[i][j] + 2;
				}
			}
		}

	} 
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			cout << m[i][j] << " ";
		}
		cout << endl;
	}



	return 0;
}
0