結果

問題 No.1434 Make Maze
ユーザー ks2mks2m
提出日時 2021-03-19 23:25:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 3,213 bytes
コンパイル時間 4,380 ms
コンパイル使用メモリ 77,528 KB
実行使用メモリ 43,676 KB
最終ジャッジ日時 2024-05-03 18:12:53
合計ジャッジ時間 11,411 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
41,372 KB
testcase_01 AC 119 ms
41,316 KB
testcase_02 AC 188 ms
43,676 KB
testcase_03 AC 187 ms
43,468 KB
testcase_04 AC 106 ms
40,172 KB
testcase_05 AC 119 ms
41,056 KB
testcase_06 AC 118 ms
41,044 KB
testcase_07 AC 105 ms
39,908 KB
testcase_08 AC 110 ms
40,428 KB
testcase_09 AC 119 ms
41,448 KB
testcase_10 AC 107 ms
39,904 KB
testcase_11 AC 118 ms
40,916 KB
testcase_12 AC 121 ms
41,312 KB
testcase_13 AC 120 ms
41,192 KB
testcase_14 AC 123 ms
41,028 KB
testcase_15 AC 152 ms
41,164 KB
testcase_16 AC 129 ms
41,292 KB
testcase_17 AC 122 ms
41,064 KB
testcase_18 AC 136 ms
41,512 KB
testcase_19 AC 159 ms
42,188 KB
testcase_20 AC 116 ms
40,940 KB
testcase_21 AC 143 ms
41,852 KB
testcase_22 AC 105 ms
40,124 KB
testcase_23 AC 103 ms
40,096 KB
testcase_24 AC 177 ms
42,256 KB
testcase_25 AC 169 ms
42,088 KB
testcase_26 AC 132 ms
41,532 KB
testcase_27 AC 166 ms
41,916 KB
testcase_28 AC 124 ms
41,256 KB
testcase_29 AC 110 ms
40,000 KB
testcase_30 AC 144 ms
41,596 KB
testcase_31 AC 132 ms
40,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int x = sc.nextInt();
		sc.close();

		char[][] s = null;
		if (h % 4 == 3 && w % 4 == 3) {
			int max = h * (w / 2 + 1) + (w / 2) - 1;
			if (h % 4 == 3 && w % 4 == 3) {
				max -= Math.min(h, w) - 1;
			}
			if (x <= max) {
				s = solve(h, w, x);
			} else {
				if (h <= w) {
					int w2 = w - 2;
					max = h * (w2 / 2 + 1) + (w2 / 2) - 1;
					char[][] s1 = solve(h, w - 2, max);
					char[][] s2 = solve(h, 3, x - max + h - 1);
					if (s2 == null) {
						System.out.println(-1);
						return;
					}
					s = new char[h][w];
					for (int i = 0; i < h; i++) {
						for (int j = 0; j < w2; j++) {
							s[i][j] = s1[i][j];
						}
						for (int j = 0; j < 3; j++) {
							s[i][w2 - 1 + j] = s2[i][j];
						}
					}
				} else {
					int h2 = h - 2;
					max = w * (h2 / 2 + 1) + (h2 / 2) - 1;
					char[][] s1 = solve(h - 2, w, max);
					char[][] s2 = solve(3, w, x - max + w - 1);
					if (s2 == null) {
						System.out.println(-1);
						return;
					}
					s = new char[h][w];
					for (int j = 0; j < w; j++) {
						for (int i = 0; i < h2; i++) {
							s[i][j] = s1[i][j];
						}
						for (int i = 0; i < 3; i++) {
							s[h2 - 1 + i][j] = s2[i][j];
						}
					}
				}
			}
		} else {
			s = solve(h, w, x);
		}
		if (s == null) {
			System.out.println(-1);
			return;
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < h; i++) {
			pw.println(s[i]);
		}
		pw.flush();
	}

	static char[][] solve(int h, int w, int x) {
		int min = h + w - 2;
		int max = h * (w / 2 + 1) + (w / 2) - 1;
		if (h % 4 == 3 && w % 4 == 3) {
			max -= Math.min(h, w) - 1;
		}
		int y = max - x;
		if (x < min || max < x || y % 4 != 0) {
			return null;
		}

		y /= 4;
		char[][] s = new char[h][w];
		for (int i = 0; i < h; i++) {
			Arrays.fill(s[i], '.');
		}
		for (int i = 1; i < h; i += 2) {
			for (int j = 1; j < w; j += 2) {
				s[i][j] = '#';
			}
		}

		boolean tate = true;
		if (w % 4 == 3) {
			if (h % 4 == 1 || h > w) {
				tate = false;
			}
		}
		if (tate) {
			tate(s, h, w, y);
		} else {
			yoko(s, h, w, y);
		}
		return s;
	}

	static void tate(char[][] s, int h, int w, int y) {
		for (int j = 1; j < w; j += 4) {
			s[0][j] = '#';
		}
		for (int i = 2; i < h - 1; i += 2) {
			for (int j = 1; j < w; j += 2) {
				s[i][j] = '#';
			}
		}
		for (int j = 3; j < w; j += 4) {
			s[h - 1][j] = '#';
		}

		int i = h - 1;
		int j = 1;
		for (int k = 0; k < y; k++) {
			s[i][j] = '#';
			s[i - 2][j] = '.';
			i -= 2;
			if (i < 2) {
				i = h - 1;
				j += 4;
			}
		}
	}

	static void yoko(char[][] s, int h, int w, int y) {
		for (int i = 1; i < h; i += 4) {
			s[i][0] = '#';
		}
		for (int j = 2; j < w - 1; j += 2) {
			for (int i = 1; i < h; i += 2) {
				s[i][j] = '#';
			}
		}
		for (int i = 3; i < h; i += 4) {
			s[i][w - 1] = '#';
		}

		int j = w - 1;
		int i = 1;
		for (int k = 0; k < y; k++) {
			s[i][j] = '#';
			s[i][j - 2] = '.';
			j -= 2;
			if (j < 2) {
				j = w - 1;
				i += 4;
			}
		}
	}
}
0