結果

問題 No.157 2つの空洞
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-20 17:12:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,700 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 80,056 KB
実行使用メモリ 57,676 KB
最終ジャッジ日時 2023-10-17 08:06:53
合計ジャッジ時間 6,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,636 KB
testcase_01 AC 128 ms
57,372 KB
testcase_02 AC 130 ms
57,448 KB
testcase_03 AC 130 ms
57,676 KB
testcase_04 AC 131 ms
57,492 KB
testcase_05 AC 130 ms
57,600 KB
testcase_06 AC 129 ms
57,404 KB
testcase_07 AC 129 ms
57,536 KB
testcase_08 AC 129 ms
55,720 KB
testcase_09 AC 129 ms
57,304 KB
testcase_10 AC 130 ms
57,552 KB
testcase_11 AC 131 ms
57,340 KB
testcase_12 AC 134 ms
57,484 KB
testcase_13 AC 133 ms
57,456 KB
testcase_14 AC 138 ms
57,584 KB
testcase_15 AC 140 ms
57,432 KB
testcase_16 AC 134 ms
57,308 KB
testcase_17 AC 149 ms
57,520 KB
testcase_18 AC 142 ms
57,640 KB
testcase_19 AC 135 ms
57,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int w = stdin.nextInt();
        int h = stdin.nextInt();
        
        char[][] matrix = new char[h][w];
        for (int i = 0; i < h; i++) {
            matrix[i] = stdin.next().toCharArray();
        }
        
        Set<Tuple> blanks1 = new HashSet<>();
        Set<Tuple> blanks2 = new HashSet<>();
        ArrayDeque<Tuple> stack = new ArrayDeque<>();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (matrix[i][j] != '.') {
                    continue;
                }
                
                if (blanks1.isEmpty()) {
                    blanks1.add(new Tuple(i, j));
                    stack.addFirst(new Tuple(i, j));
                } else {
                    blanks2.add(new Tuple(i, j));
                }
            }
        }
        
        List<Tuple> diffs = new ArrayList<>();
        diffs.add(new Tuple( 1,  0));
        diffs.add(new Tuple(-1,  0));
        diffs.add(new Tuple( 0,  1));
        diffs.add(new Tuple( 0, -1));
        
        while (!stack.isEmpty()) {
            Tuple t = stack.removeFirst();
            for (Tuple d : diffs) {
                int x = t.x + d.x;
                int y = t.y + d.y;
                Tuple n = new Tuple(x, y);
                if (0 <= x && x < h && 0 <= y && y < w && blanks2.contains(n)) {
                    stack.add(n);
                    blanks1.add(n);
                    blanks2.remove(n);
                }
            }
        }
        
        int ans = Integer.MAX_VALUE;
        for (Tuple blank1 : blanks1) {
            for (Tuple blank2 : blanks2) {
                int len = Math.abs(blank1.x - blank2.x) + Math.abs(blank1.y - blank2.y);
                ans = Math.min(ans, len);
            }
        }
        System.out.println(ans - 1);
    }
    
    private static class Tuple {
        private int x;
        private int y;
        
        public Tuple(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public boolean equals(Object obj) {
            if ((obj == null) || !(obj instanceof Tuple)) {
                return false;
            }
            
            Tuple other = (Tuple) obj;
            return this.x == other.x && this.y == other.y;
        }
    }
}
0