結果
問題 | No.157 2つの空洞 |
ユーザー | a3636tako |
提出日時 | 2016-08-25 16:32:17 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 58 ms / 2,000 ms |
コード長 | 3,239 bytes |
コンパイル時間 | 2,296 ms |
コンパイル使用メモリ | 78,168 KB |
実行使用メモリ | 37,172 KB |
最終ジャッジ日時 | 2024-11-08 02:22:20 |
合計ジャッジ時間 | 4,143 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
36,552 KB |
testcase_01 | AC | 53 ms
36,720 KB |
testcase_02 | AC | 52 ms
36,372 KB |
testcase_03 | AC | 52 ms
36,608 KB |
testcase_04 | AC | 53 ms
36,560 KB |
testcase_05 | AC | 53 ms
36,632 KB |
testcase_06 | AC | 55 ms
36,828 KB |
testcase_07 | AC | 53 ms
36,632 KB |
testcase_08 | AC | 52 ms
36,552 KB |
testcase_09 | AC | 52 ms
36,720 KB |
testcase_10 | AC | 52 ms
36,392 KB |
testcase_11 | AC | 54 ms
36,752 KB |
testcase_12 | AC | 53 ms
36,796 KB |
testcase_13 | AC | 53 ms
36,704 KB |
testcase_14 | AC | 56 ms
37,096 KB |
testcase_15 | AC | 54 ms
36,712 KB |
testcase_16 | AC | 55 ms
36,500 KB |
testcase_17 | AC | 58 ms
37,172 KB |
testcase_18 | AC | 55 ms
36,392 KB |
testcase_19 | AC | 52 ms
36,876 KB |
ソースコード
import java.io.*; import java.util.*; public class Main{ int W; int H; int[][] field; int[] DX = {1, 0, -1, 0}; int[] DY = {0, 1, 0, -1}; public void solve(){ W = nextInt(); H = nextInt(); field = new int[H][W]; for(int i = 0; i < H; i++){ String str = next(); for(int j = 0; j < W; j++){ if(str.charAt(j) == '#'){ field[i][j] = -1; }else{ field[i][j] = 0; } } } int cnt = 1; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(field[i][j] == 0){ paint(j, i, cnt); cnt++; } } } int ans = Integer.MAX_VALUE; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(field[i][j] == 1){ Queue<int[]> queue = new ArrayDeque<>(); queue.add(new int[]{j, i, 0}); boolean[][] flg = new boolean[H][W]; flg[i][j] = true; while(!queue.isEmpty()){ int[] e = queue.poll(); for(int k = 0; k < 4; k++){ int nx = e[0] + DX[k]; int ny = e[1] + DY[k]; if(0 <= nx && nx < W && 0 <= ny && ny < H && !flg[ny][nx]){ flg[ny][nx] = true; if(field[ny][nx] == 2){ ans = Math.min(ans, e[2]); }else if(field[ny][nx] == -1 && e[2] + 1 < ans){ queue.add(new int[]{nx, ny, e[2] + 1}); } } } } } } } out.println(ans); } public void paint(int x, int y, int c){ if(0 <= x && x < W && 0 <= y && y <= H && field[y][x] == 0){ field[y][x] = c; for(int i = 0; i < 4; i++){ paint(x + DX[i], y + DY[i], c); } } } private static PrintWriter out; public static void main(String[] args){ out = new PrintWriter(System.out); new Main().solve(); out.flush(); } public static int nextInt(){ int num = 0; String str = next(); boolean minus = false; int i = 0; if(str.charAt(0) == '-'){ minus = true; i++; } int len = str.length(); for(;i < len; i++){ char c = str.charAt(i); if(!('0' <= c && c <= '9')) throw new RuntimeException(); num = num * 10 + (c - '0'); } return minus ? -num : num; } public static long nextLong(){ long num = 0; String str = next(); boolean minus = false; int i = 0; if(str.charAt(0) == '-'){ minus = true; i++; } int len = str.length(); for(;i < len; i++){ char c = str.charAt(i); if(!('0' <= c && c <= '9')) throw new RuntimeException(); num = num * 10l + (c - '0'); } return minus ? -num : num; } public static String next(){ int c; while(!isAlNum(c = read())){} StringBuilder build = new StringBuilder(); build.append((char)c); while(isAlNum(c = read())){ build.append((char)c); } return build.toString(); } private static byte[] inputBuffer = new byte[1024]; private static int bufferLength = 0; private static int bufferIndex = 0; private static int read(){ if(bufferLength < 0) throw new RuntimeException(); if(bufferIndex >= bufferLength){ try{ bufferLength = System.in.read(inputBuffer); bufferIndex = 0; }catch(IOException e){ throw new RuntimeException(e); } if(bufferLength <= 0) return (bufferLength = -1); } return inputBuffer[bufferIndex++]; } private static boolean isAlNum(int c){ return '!' <= c && c <= '~'; } }