結果
問題 | No.2412 YOU Grow Bigger! |
ユーザー | tenten |
提出日時 | 2023-08-17 09:28:59 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 766 ms / 6,000 ms |
コード長 | 5,319 bytes |
コンパイル時間 | 2,750 ms |
コンパイル使用メモリ | 84,784 KB |
実行使用メモリ | 59,768 KB |
最終ジャッジ日時 | 2024-05-04 16:20:36 |
合計ジャッジ時間 | 10,983 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
50,240 KB |
testcase_01 | AC | 48 ms
50,092 KB |
testcase_02 | AC | 48 ms
50,312 KB |
testcase_03 | AC | 721 ms
59,444 KB |
testcase_04 | AC | 740 ms
59,384 KB |
testcase_05 | AC | 525 ms
59,372 KB |
testcase_06 | AC | 583 ms
58,780 KB |
testcase_07 | AC | 738 ms
59,264 KB |
testcase_08 | AC | 766 ms
59,476 KB |
testcase_09 | AC | 52 ms
50,348 KB |
testcase_10 | AC | 158 ms
57,944 KB |
testcase_11 | AC | 48 ms
50,000 KB |
testcase_12 | AC | 46 ms
50,284 KB |
testcase_13 | AC | 167 ms
58,736 KB |
testcase_14 | AC | 78 ms
52,016 KB |
testcase_15 | AC | 47 ms
50,324 KB |
testcase_16 | AC | 47 ms
50,304 KB |
testcase_17 | AC | 50 ms
50,364 KB |
testcase_18 | AC | 241 ms
59,268 KB |
testcase_19 | AC | 45 ms
50,340 KB |
testcase_20 | AC | 46 ms
50,420 KB |
testcase_21 | AC | 147 ms
57,320 KB |
testcase_22 | AC | 179 ms
58,720 KB |
testcase_23 | AC | 300 ms
59,768 KB |
testcase_24 | AC | 399 ms
59,528 KB |
testcase_25 | AC | 267 ms
59,444 KB |
testcase_26 | AC | 131 ms
56,584 KB |
testcase_27 | AC | 89 ms
53,232 KB |
testcase_28 | AC | 212 ms
58,676 KB |
testcase_29 | AC | 48 ms
50,116 KB |
testcase_30 | AC | 46 ms
50,400 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); char[][] field = new char[h][]; for (int i = 0; i < h; i++) { field[i] = sc.next().toCharArray(); } ArrayDeque<Integer> deq = new ArrayDeque<>(); boolean[][] visited = new boolean[h][w]; deq.add(w + 1); while (deq.size() > 0) { int x = deq.poll(); int r = x / w; int c = x % w; if (visited[r][c]) { continue; } visited[r][c] = true; boolean hasDef = false; for (int i = -1; i <= 1 && !hasDef; i++) { for (int j = -1; j <= 1 && !hasDef; j++) { hasDef = (field[r + i][c + j] == '#'); } } if (hasDef) { continue; } if (r > 1) { deq.add(x - w); if (c > 1) { deq.add(x - w - 1); } if (c < w - 2) { deq.add(x - w + 1); } } if (r < h - 2) { deq.add(x + w); if (c > 1) { deq.add(x + w - 1); } if (c < w - 2) { deq.add(x + w + 1); } } if (c > 1) { deq.add(x - 1); } if (c < w - 2) { deq.add(x + 1); } } if (!visited[h - 2][w - 2]) { System.out.println(0); return; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i <= 2 && j <= 2) { continue; } if (i >= h - 3 && j >= w - 3) { continue; } if (field[i][j] == '#') { continue; } field[i][j] = '#'; deq.add(w + 1); for (boolean[] arr : visited) { Arrays.fill(arr, false); } while (deq.size() > 0) { int x = deq.poll(); int r = x / w; int c = x % w; if (visited[r][c]) { continue; } visited[r][c] = true; boolean hasDef = false; for (int a = -1; a <= 1 && !hasDef; a++) { for (int b = -1; b <= 1 && !hasDef; b++) { hasDef = (field[r + a][c + b] == '#'); } } if (hasDef) { continue; } if (r > 1) { deq.add(x - w); if (c > 1) { deq.add(x - w - 1); } if (c < w - 2) { deq.add(x - w + 1); } } if (r < h - 2) { deq.add(x + w); if (c > 1) { deq.add(x + w - 1); } if (c < w - 2) { deq.add(x + w + 1); } } if (c > 1) { deq.add(x - 1); } if (c < w - 2) { deq.add(x + 1); } } if (!visited[h - 2][w - 2]) { System.out.println(1); return; } field[i][j] = '.'; } } System.out.println(2); } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }