結果

問題 No.217 魔方陣を作ろう
ユーザー GrenacheGrenache
提出日時 2015-05-26 23:37:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 185 ms / 5,000 ms
コード長 2,618 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 74,000 KB
実行使用メモリ 57,600 KB
最終ジャッジ日時 2023-09-20 15:24:01
合計ジャッジ時間 6,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,896 KB
testcase_01 AC 132 ms
55,740 KB
testcase_02 AC 131 ms
55,640 KB
testcase_03 AC 131 ms
55,316 KB
testcase_04 AC 131 ms
55,608 KB
testcase_05 AC 135 ms
53,676 KB
testcase_06 AC 137 ms
57,600 KB
testcase_07 AC 139 ms
55,644 KB
testcase_08 AC 140 ms
56,300 KB
testcase_09 AC 160 ms
54,468 KB
testcase_10 AC 156 ms
55,656 KB
testcase_11 AC 168 ms
55,952 KB
testcase_12 AC 169 ms
55,964 KB
testcase_13 AC 175 ms
56,040 KB
testcase_14 AC 165 ms
55,880 KB
testcase_15 AC 183 ms
57,472 KB
testcase_16 AC 179 ms
55,472 KB
testcase_17 AC 185 ms
55,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		
		int[][] ret = new int[n][n];
		
		if (n % 2 == 1) {
			msOdd(ret);
		} else if (n % 4 == 0) {
			ms4n(ret);
		} else if (n % 4 == 2) {
			ms4np2(ret);
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.printf("%d ", ret[j][i]);
			}
		}
		
		sc.close();
	}

	private static void ms4np2(int[][] ret) {
		int n = ret[0].length;
		int nn = (n - 2) / 4;
		
		int[][] ret2 = new int[2 * nn + 1][2 * nn + 1];
		msOdd(ret2);
		
		for (int i = 0; i < ret2[0].length; i++) {
			for (int j = 0; j < ret2[0].length; j++) {
				ret2[i][j] = (ret2[i][j] - 1) * 4;
			}
		}
		
		int[] L = {4, 1, 2, 3};
		int[] U = {1, 4, 2, 3};
		int[] X = {1, 4, 3, 2};
		
		for (int i = 0; i < 2 * nn + 1; i++) {
			for (int j = 0; j < 2 * nn + 1; j++) {
				if (j < nn || (j == nn + 1 && i == nn) || (j == nn && i != nn)) {
					ret[i * 2][j * 2] = ret2[i][j] + L[0];
					ret[i * 2 + 1][j * 2] = ret2[i][j] + L[1];
					ret[i * 2][j * 2 + 1] = ret2[i][j] + L[2];
					ret[i * 2 + 1][j * 2 + 1] = ret2[i][j] + L[3];
				} else if ((j == nn && i == nn) || (j == nn + 1 && i != nn)) {
					ret[i * 2][j * 2] = ret2[i][j] + U[0];
					ret[i * 2 + 1][j * 2] = ret2[i][j] + U[1];
					ret[i * 2][j * 2 + 1] = ret2[i][j] + U[2];
					ret[i * 2 + 1][j * 2 + 1] = ret2[i][j] + U[3];
				} else if (j > nn) {
					ret[i * 2][j * 2] = ret2[i][j] + X[0];
					ret[i * 2 + 1][j * 2] = ret2[i][j] + X[1];
					ret[i * 2][j * 2 + 1] = ret2[i][j] + X[2];
					ret[i * 2 + 1][j * 2 + 1] = ret2[i][j] + X[3];
				}
			}
		}
	}

	private static void ms4n(int[][] ret) {
		int n = ret[0].length;

		for (int i = 1; i <= n * n; i++) {
			int x = (i - 1) % n;
			int y = (i - 1) / n;
			if ((x % 4 == 0 || x % 4 == 3) && (y % 4 == 0 || y % 4 == 3)) {
				ret[x][y] = i;
			} else if ((x % 4 == 1 || x % 4 == 2) && (y % 4 == 1 || y % 4 == 2)) {
				ret[x][y] = i;
			}
		}		

		for (int i = 1; i <= n * n; i++) {
			int x = n - 1 - (i - 1) % n;
			int y = n - 1 - (i - 1) / n;
			if (ret[x][y] == 0) {
				ret[x][y] = i;
			}
		}		
	}

	private static void msOdd(int[][] ret) {
		int n = ret[0].length;
		
		int x = n / 2;
		int y = 0;
		
		for (int i = 1; i <= n * n; i++) {
			ret[x][y] = i;
			int tmpx = x + 1;
			int tmpy = y - 1;
			if (tmpx >= n) {
				tmpx = 0;
			}
			if (tmpy < 0) {
				tmpy = n - 1;
			}
			
			if (ret[tmpx][tmpy] == 0) {
				x = tmpx;
				y = tmpy;
				ret[x][y] = i;
			} else {
				y++;
				if (y >= n) {
					y = 0;
				}
			}
		}
		
	}
}
0