結果

問題 No.217 魔方陣を作ろう
ユーザー fujisufujisu
提出日時 2015-05-26 23:28:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 4,584 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 77,348 KB
実行使用メモリ 53,416 KB
最終ジャッジ日時 2023-09-20 15:22:39
合計ジャッジ時間 5,353 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
52,760 KB
testcase_01 AC 77 ms
52,916 KB
testcase_02 AC 78 ms
52,508 KB
testcase_03 AC 80 ms
52,924 KB
testcase_04 AC 79 ms
53,112 KB
testcase_05 AC 81 ms
53,044 KB
testcase_06 AC 91 ms
53,096 KB
testcase_07 AC 82 ms
53,112 KB
testcase_08 AC 84 ms
53,000 KB
testcase_09 AC 85 ms
53,416 KB
testcase_10 AC 86 ms
52,936 KB
testcase_11 AC 87 ms
52,748 KB
testcase_12 AC 90 ms
53,172 KB
testcase_13 AC 101 ms
53,156 KB
testcase_14 AC 93 ms
50,760 KB
testcase_15 AC 95 ms
52,324 KB
testcase_16 AC 108 ms
53,024 KB
testcase_17 AC 99 ms
52,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.InputMismatchException;

public class Main {
	int[][] magicSquareOdd(int n) {
		int[][] res = new int[n][n];
		int x = n / 2;
		int y = 0;
		int id = 1;
		while (true) {
			res[y][x] = id++;
			int nx = x + 1;
			nx %= n;
			int ny = y - 1;
			ny += n;
			ny %= n;
			if (res[ny][nx] != 0) {
				y++;
				y %= n;
			} else {
				x = nx;
				y = ny;
			}
			if (n * n < id) {
				return res;
			}
		}
	}

	int[][] magicSquare4N(int n) {
		int[][] res = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if ((i % 4 == 0 || i % 4 == 3) && (j % 4 == 0 || j % 4 == 3)) {
					res[i][j] = i * n + j + 1;
				} else if ((i % 4 == 1 || i % 4 == 2) && (j % 4 == 1 || j % 4 == 2)) {
					res[i][j] = i * n + j + 1;
				}
			}
		}
		int id = 1;
		for (int i = n - 1; 0 <= i; i--) {
			for (int j = n - 1; 0 <= j; j--) {
				if (res[i][j] == 0) {
					res[i][j] = id;
				}
				id++;
			}
		}
		return res;
	}

	int[][] magicSquare4NPuls2(int n) {
		int[][] res = new int[n][n];

		int nn = n / 2;
		int[][] tmp = magicSquareOdd(nn);
		for (int i = 0; i < nn; i++) {
			for (int j = 0; j < nn; j++) {
				tmp[i][j]--;
				tmp[i][j] *= 4;
			}
		}

		int[][][][] add = new int[nn][nn][][];
		int[][] l = { { 4, 1 }, { 2, 3 } };
		int[][] u = { { 1, 4 }, { 2, 3 } };
		int[][] x = { { 1, 4 }, { 3, 2 } };
		for (int i = 0; i < nn; i++) {
			if (i <= nn / 2) {
				for (int j = 0; j < nn; j++) {
					add[i][j] = l;
				}
			} else if (i > nn / 2 + 1) {
				for (int j = 0; j < nn; j++) {
					add[i][j] = x;
				}
			} else {
				for (int j = 0; j < nn; j++) {
					add[i][j] = u;
				}
			}
		}
		int xx = nn / 2;
		int yy = nn / 2;
		add[yy][xx] = u;
		add[yy + 1][xx] = l;
		
		/*
		for (int i = 0; i < nn; i++) {
			for (int j = 0; j < nn; j++) {
				if (add[i][j] == l) {
					System.out.print("L ");
				}
				if (add[i][j] == u) {
					System.out.print("u ");
				}
				if (add[i][j] == x) {
					System.out.print("x ");
				}
			}
			System.out.println();
		}
		*/

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				res[i][j] = tmp[i / 2][j / 2] + add[i / 2][j / 2][i % 2][j % 2];
			}
		}

		return res;
	}

	void run() {
		MyScanner sc = new MyScanner();

		int n = sc.nextInt();
		int[][] res;

		if (n % 2 == 1) {
			res = magicSquareOdd(n);
		} else if (n % 4 == 0) {
			res = magicSquare4N(n);
		} else {
			res = magicSquare4NPuls2(n);
		}

		String tab = "";
		for (int i = 0; i < n; i++) {
			tab = "";
			for (int j = 0; j < n; j++) {
				System.out.print(tab + res[i][j]);
				tab = " ";
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0