結果

問題 No.217 魔方陣を作ろう
ユーザー ぴろずぴろず
提出日時 2015-05-26 23:00:02
言語 Java19
(openjdk 21)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 2,429 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 74,088 KB
実行使用メモリ 56,036 KB
最終ジャッジ日時 2023-09-20 15:06:09
合計ジャッジ時間 5,811 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,812 KB
testcase_01 AC 125 ms
55,448 KB
testcase_02 AC 127 ms
55,644 KB
testcase_03 AC 127 ms
55,480 KB
testcase_04 AC 129 ms
55,292 KB
testcase_05 AC 129 ms
55,708 KB
testcase_06 AC 128 ms
55,800 KB
testcase_07 AC 128 ms
55,372 KB
testcase_08 AC 128 ms
55,536 KB
testcase_09 AC 127 ms
55,472 KB
testcase_10 AC 126 ms
55,744 KB
testcase_11 AC 128 ms
55,548 KB
testcase_12 AC 127 ms
55,528 KB
testcase_13 AC 128 ms
55,688 KB
testcase_14 AC 127 ms
55,692 KB
testcase_15 AC 126 ms
56,036 KB
testcase_16 AC 126 ms
55,604 KB
testcase_17 AC 131 ms
55,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no217;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] ans;
		if (n % 2 == 1) {
			ans = odd(n);
		}else if((n / 2) % 2 == 0) {
			ans = even1(n);
		}else{
			ans = even2(n);
		}
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if (j > 0) {
					sb.append(' ');
				}
				sb.append(ans[i][j]);
			}
			sb.append('\n');
		}

		System.out.print(sb.toString());
	}

	public static int[][] odd(int n) {
		int[][] m = new int[n][n];
		int i = n - 1;
		int j = n / 2;
		for(int t=0;t<n*n;t++) {
			m[i][j] = t + 1;
			int ni = (i + 1) % n;
			int nj = (j + 1) % n;
			if (m[ni][nj] == 0) {
				i = ni;
				j = nj;
			}else{
				i = (i + n - 1) % n;
			}
		}
		return m;
	}

	// http://d.hatena.ne.jp/ziom/20090620/p1
	public static int[][] even1(int n) {
		int[][] m = new int[n][n];
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				m[i][j] = i * n + j + 1;
			}
		}
		for(int i=0;i<n/2;i++) {
			for(int j=0;j<n;j++) {
				if (((i + 1) / 2 + (j + 1) / 2) % 2 == 1) {
					int temp = m[i][j];
					m[i][j] = m[n-1-i][n-1-j];
					m[n-1-i][n-1-j] = temp;
				}
			}
		}
		return m;
	}

	// http://d.hatena.ne.jp/ziom/20090621/p1
	public static int[][] even2(int n) {
		int[][] m = new int[n][n];
		m[0][0] = 1;
		m[0][n-1] = 2;
		m[n-1][0] = n * n - 1;
		m[n-1][n-1] = n * n;
		m[n-1][1] = 3;
		m[n-1][2] = 4;
		for(int i=3;i<n-2;i++) {
			if (i / 2 % 2 == 1) {
				m[n-1][i] = i + 2;
			}else{
				m[0][i] = i + 2;
			}
		}
		m[0][n-2] = n + 3;
		for(int i=1;i<n-1;i++) {
			int num = n - 1 + i;
			if (num >= n + 3) {
				num++;
			}
			if (i / 2 % 2 == 0) {
				m[i][0] = num;
			}else{
				m[i][n-1] = num;
			}
		}
		for(int i=1;i<n-1;i++) {
			if (m[i][0] == 0) {
				m[i][0] = n * n + 1 - m[i][n-1];
			}
			if (m[i][n-1] == 0) {
				m[i][n-1] = n * n + 1 - m[i][0];
			}
			if (m[0][i] == 0) {
				m[0][i] = n * n + 1 - m[n-1][i];
			}
			if (m[n-1][i] == 0) {
				m[n-1][i] = n * n + 1 - m[0][i];
			}
		}
		for(int i=0;i<n-2;i++) {
			for(int j=0;j<n-2;j++) {
				m[i+1][j+1] = i * (n-2) + j + n * 2 - 1;
			}
		}
		for(int i=0;i<(n-2)/2;i++) {
			for(int j=0;j<n-2;j++) {
				if (((i + 1) / 2 + (j + 1) / 2) % 2 == 1) {
					int temp = m[i+1][j+1];
					m[i+1][j+1] = m[n-2-i][n-2-j];
					m[n-2-i][n-2-j] = temp;
				}
			}
		}
		return m;
	}

}
0