結果

問題 No.217 魔方陣を作ろう
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 02:57:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 171 ms / 5,000 ms
コード長 2,793 bytes
コンパイル時間 3,606 ms
コンパイル使用メモリ 73,896 KB
実行使用メモリ 56,512 KB
最終ジャッジ日時 2023-09-20 18:56:42
合計ジャッジ時間 7,492 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,124 KB
testcase_01 AC 125 ms
56,340 KB
testcase_02 AC 126 ms
55,980 KB
testcase_03 AC 127 ms
55,612 KB
testcase_04 AC 129 ms
55,752 KB
testcase_05 AC 133 ms
56,252 KB
testcase_06 AC 133 ms
55,692 KB
testcase_07 AC 134 ms
55,960 KB
testcase_08 AC 136 ms
55,724 KB
testcase_09 AC 152 ms
55,832 KB
testcase_10 AC 143 ms
56,072 KB
testcase_11 AC 154 ms
56,512 KB
testcase_12 AC 159 ms
55,924 KB
testcase_13 AC 167 ms
55,932 KB
testcase_14 AC 161 ms
55,924 KB
testcase_15 AC 159 ms
55,636 KB
testcase_16 AC 158 ms
55,656 KB
testcase_17 AC 171 ms
55,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	public static void print(final int N, int[][] matrix){
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				if(j != 0){
					System.out.print(" ");
				}
				System.out.print(matrix[i][j]);
			}
			System.out.println();
		}
	}
	
	public static int[][] SiamessMethod(final int N){
		int[][] matrix = new int[N][N];
		
		for(int current = 1, x = N / 2, y = 0; current <= N *N; current++, x = (x + 1) % N, y = (N + y - 1) % N){
			if(matrix[y][x] != 0){
				x = (N + x - 1) % N;
				y = (y + 2) % N;
			}
			
			matrix[y][x] = current;
		}
		
		return matrix;
	}
	
	public static int[][] DurerMethod(final int N){
		int[][] matrix = 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))){
					matrix[i][j] = (i * N) + j + 1;
				}else if(((i % 4 == 1) || (i % 4 == 2)) && ((j % 4 == 1) || (j % 4 == 2))){
					matrix[i][j] = (i * N) + j + 1;
				}
			}
		}
		
		for(int i = N - 1; i >= 0; i--){
			for(int j = N - 1; j >= 0; j--){
				if(((i % 4 == 0) || (i % 4 == 3)) && ((j % 4 == 1) || (j % 4 == 2))){
					matrix[i][j] = N * N - ((i * N) + j);
				}else if(((i % 4 == 1) || (i % 4 == 2)) && ((j % 4 == 0) || (j % 4 == 3))){
					matrix[i][j] = N * N - ((i * N) + j);
				}
			}
		}
		
		
		return matrix;
	}
	
	public static int[][] LUXMethod(final int N){
		final int n = (N - 2) / 4;
		final int small_N = n * 2 + 1;
		
		int[][] small_matrix = SiamessMethod(small_N);
		for(int i = 0; i < small_N; i++){
			for(int j = 0; j < small_N; j++){
				small_matrix[i][j] = (small_matrix[i][j] - 1) * 4;
			}
		}
		
		final int[][] L = {{4, 1}, {2, 3}};
		final int[][] U = {{1, 4}, {2, 3}};
		final int[][] X = {{1, 4}, {3, 2}};
		
		int[][] matrix = new int[N][N];
		for(int i = 0; i < small_N; i++){
			for(int j = 0; j < small_N; j++){
				final int base_y = i * 2;
				final int base_x = j * 2;
				
				for(int k = 0; k < 2; k++){
					for(int l = 0; l < 2; l++){
						final int y = base_y + k;
						final int x = base_x + l;
						
						if(i > n + 1){
							matrix[y][x] = small_matrix[i][j] + X[k][l];
						}else if(i == n + 1 && j != small_N / 2){
							matrix[y][x] = small_matrix[i][j] + U[k][l];
						}else if(i == n && j == small_N / 2){
							matrix[y][x] = small_matrix[i][j] + U[k][l];
						}else{
							matrix[y][x] = small_matrix[i][j] + L[k][l];
						}
					}
				}
			}
		}
		
		return matrix;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[][] matrix = null;
		if(N % 2 != 0){
			matrix = SiamessMethod(N);
		}else if(N % 4 == 0){
			matrix = DurerMethod(N);
		}else{
			matrix = LUXMethod(N);
		}
		
		print(N, matrix);
	}
}
0