結果

問題 No.1434 Make Maze
ユーザー ks2mks2m
提出日時 2021-03-19 23:25:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 3,213 bytes
コンパイル時間 6,215 ms
コンパイル使用メモリ 74,412 KB
実行使用メモリ 58,512 KB
最終ジャッジ日時 2023-08-16 09:27:31
合計ジャッジ時間 12,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,756 KB
testcase_01 AC 123 ms
55,700 KB
testcase_02 AC 185 ms
58,112 KB
testcase_03 AC 189 ms
58,052 KB
testcase_04 AC 124 ms
55,832 KB
testcase_05 AC 122 ms
55,952 KB
testcase_06 AC 122 ms
55,788 KB
testcase_07 AC 127 ms
55,832 KB
testcase_08 AC 123 ms
56,264 KB
testcase_09 AC 124 ms
55,880 KB
testcase_10 AC 123 ms
56,020 KB
testcase_11 AC 123 ms
55,868 KB
testcase_12 AC 128 ms
55,972 KB
testcase_13 AC 126 ms
56,048 KB
testcase_14 AC 131 ms
55,956 KB
testcase_15 AC 172 ms
56,160 KB
testcase_16 AC 134 ms
55,952 KB
testcase_17 AC 132 ms
55,872 KB
testcase_18 AC 154 ms
56,068 KB
testcase_19 AC 185 ms
55,944 KB
testcase_20 AC 122 ms
55,784 KB
testcase_21 AC 152 ms
56,252 KB
testcase_22 AC 121 ms
56,112 KB
testcase_23 AC 124 ms
55,920 KB
testcase_24 AC 178 ms
56,532 KB
testcase_25 AC 184 ms
58,512 KB
testcase_26 AC 141 ms
55,868 KB
testcase_27 AC 166 ms
56,240 KB
testcase_28 AC 127 ms
56,092 KB
testcase_29 AC 125 ms
53,848 KB
testcase_30 AC 153 ms
56,052 KB
testcase_31 AC 157 ms
56,108 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