結果

問題 No.217 魔方陣を作ろう
ユーザー threepipes_sthreepipes_s
提出日時 2015-05-26 23:31:34
言語 Java19
(openjdk 21)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 4,975 bytes
コンパイル時間 3,608 ms
コンパイル使用メモリ 84,580 KB
実行使用メモリ 50,792 KB
最終ジャッジ日時 2023-09-20 15:23:24
合計ジャッジ時間 6,031 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
50,372 KB
testcase_01 AC 68 ms
50,552 KB
testcase_02 AC 67 ms
50,492 KB
testcase_03 AC 68 ms
50,636 KB
testcase_04 AC 72 ms
50,472 KB
testcase_05 AC 69 ms
50,312 KB
testcase_06 AC 69 ms
50,208 KB
testcase_07 AC 68 ms
50,400 KB
testcase_08 AC 70 ms
50,608 KB
testcase_09 AC 71 ms
50,792 KB
testcase_10 AC 69 ms
50,308 KB
testcase_11 AC 69 ms
50,212 KB
testcase_12 AC 69 ms
50,188 KB
testcase_13 AC 70 ms
50,140 KB
testcase_14 AC 70 ms
50,640 KB
testcase_15 AC 71 ms
50,316 KB
testcase_16 AC 70 ms
50,684 KB
testcase_17 AC 72 ms
50,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws NumberFormatException,
	IOException {
		ContestScanner in = new ContestScanner();
		int n = in.nextInt();
		int[][] res = new int[n][n];
		if(n%2 == 1){
			odd(res, n);
		}else if(n%4 == 0){
			even4(res, n);
		}else{
			lux(res, n);
		}
		
		System.out.println(out(res, n));
	}
	
//	public static int[][] lux ={
//		{1, 1, 1, 1, 1},
//		{1, 1, 1, 1, 1},
//		{1, 1, 2, 1, 1},
//		{2, 2, 1, 2, 2},
//		{3, 3, 3, 3, 3},
//	};
	public static int[][] l = {
		{4, 1},
		{2, 3}
	};
	public static int[][] u = {
		{1, 4},
		{2, 3}
	};
	public static int[][] x = {
		{1, 4},
		{3, 2}
	};
	public static void lux(int[][] board, int n){
		int half = n/2;
		int[][] b = new int[half][half];
		odd(b, half);
		for(int i=0; i<half; i++){
			for(int j=0; j<half; j++){
				b[i][j] = (b[i][j]-1)*4;
			}
		}
		
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				board[i][j] = b[i/2][j/2]; 
			}
		}
		int[][] lux = new int[half][half];
		createLux(lux, half);
//		System.out.println(out(lux, half));
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				int val = lux[i/2][j/2];
				int add = 0;
				if(val == 0){
					add = l[i%2][j%2];
				}else if(val == 1){
					add = u[i%2][j%2];
					
				}else{
					add = x[i%2][j%2];
					
				}
				board[i][j] += add;
			}
		}
	}
	
	public static void createLux(int[][] board, int n){
		// 0:L, 1:U, 2:X
		for(int i=n/2+2; i<n; i++){
			for(int j=0; j<n; j++){
				board[i][j] = 2;
			}
		}
		for(int i=0; i<n; i++){
//			board[n-1][i] = 2;
			board[n/2+1][i] = 1;
		}
		board[n/2][n/2] = 1;
		board[n/2+1][n/2] = 0;
	}
	
	public static int[][] map4 = {
		{1, 0, 0, 1},
		{0, 1, 1, 0},
		{0, 1, 1, 0},
		{1, 0, 0, 1},		
	};
	public static void even4(int[][] board, int n){
		int idx = 0;
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				idx++;
				if(map4[i%4][j%4] == 1){
					board[i][j] = idx;
				}
			}
		}
		idx = 0;
		for(int i=n-1; i>=0; i--){
			for(int j=n-1; j>=0; j--){
				idx++;
				if(map4[i%4][j%4] == 0){
					board[i][j] = idx;
				}
			}
		}
	}
	
	public static String out(int[][] res, int n){
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				sb.append(res[i][j]+" ");
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
	public static void odd(int[][] board, int n){
		board[0][n/2] = 1;
		int y = n-1;
		int x = n/2+1;
		int val = 2;
		while(board[y][x] == 0){
			board[y][x] = val;
			val++;
			y--;
			x++;
			if(y < 0) y += n;
			if(x >=n) x -= n;
			if(board[y][x] != 0){
				y++; y++;
				x--;
				if(y >= n) y -= n;
				if(x < 0) x += n;
			}
		}
	}
}

class Node{
	int id;
	ArrayList<Node> edge = new ArrayList<Node>();
	public Node(int id) {
		this.id = id;
	}
	public void createEdge(Node node) {
		edge.add(node);
	}
}

class MyComp implements Comparator<int[]> {
	final int idx;
	public MyComp(int idx){
		this.idx = idx;
	}
	public int compare(int[] a, int[] b) {
		return a[idx] - b[idx];
	}
}

class Reverse implements Comparator<Integer> {
	public int compare(Integer arg0, Integer arg1) {
		return arg1 - arg0;
	}
}


class ContestWriter {
	private PrintWriter out;

	public ContestWriter(String filename) throws IOException {
		out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
	}

	public ContestWriter() throws IOException {
		out = new PrintWriter(System.out);
	}

	public void println(String str) {
		out.println(str);
	}
	
	public void println(Object obj) {
		out.println(obj);
	}

	public void print(String str) {
		out.print(str);
	}
	
	public void print(Object obj) {
		out.print(obj);
	}

	public void close() {
		out.close();
	}
}

class ContestScanner {
	private BufferedReader reader;
	private String[] line;
	private int idx;

	public ContestScanner() throws FileNotFoundException {
		reader = new BufferedReader(new InputStreamReader(System.in));
	}

	public ContestScanner(String filename) throws FileNotFoundException {
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(
				filename)));
	}

	public String nextToken() throws IOException {
		if (line == null || line.length <= idx) {
			line = reader.readLine().trim().split(" ");
			idx = 0;
		}
		return line[idx++];
	}
	
	public String readLine() throws IOException{
		return reader.readLine();
	}

	public long nextLong() throws IOException, NumberFormatException {
		return Long.parseLong(nextToken());
	}

	public int nextInt() throws NumberFormatException, IOException {
		return (int) nextLong();
	}

	public double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}
}
0