結果

問題 No.157 2つの空洞
ユーザー lethe2211lethe2211
提出日時 2015-03-02 10:58:21
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 1,951 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 6,672 KB
実行使用メモリ 22,704 KB
最終ジャッジ日時 2023-09-06 06:23:03
合計ジャッジ時間 8,060 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
10,744 KB
testcase_01 AC 13 ms
6,292 KB
testcase_02 AC 13 ms
6,268 KB
testcase_03 AC 13 ms
6,344 KB
testcase_04 AC 12 ms
6,404 KB
testcase_05 AC 12 ms
6,280 KB
testcase_06 AC 101 ms
7,152 KB
testcase_07 AC 12 ms
6,496 KB
testcase_08 AC 12 ms
6,288 KB
testcase_09 AC 12 ms
6,352 KB
testcase_10 AC 13 ms
6,268 KB
testcase_11 AC 99 ms
7,032 KB
testcase_12 TLE -
testcase_13 AC 942 ms
11,980 KB
testcase_14 AC 15 ms
6,472 KB
testcase_15 AC 17 ms
6,368 KB
testcase_16 AC 20 ms
6,588 KB
testcase_17 AC 15 ms
6,424 KB
testcase_18 AC 215 ms
7,940 KB
testcase_19 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        w, h = map(int, raw_input().split())
        board = []
        for i in range(h):
            row = list(raw_input())
            board.append(row)
        cave = set()
        d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        flg = False
        for i in range(h):
            for j in range(w):
                if board[i][j] == '.':
                    s = [(i, j, 0)]
                    while s:
                        y, x, num = s.pop()
                        # print y, x
                        board[y][x] = '&'
                        cave.add((y, x, 0))
                        for dy, dx in d:
                            if 0 <= y + dy < h and 0 <= x + dx < w and (not (y + dy, x + dx, 0) in cave) and board[y+dy][x+dx] == '.':
                                s.append((y + dy, x + dx, 0))
                    flg = True
                    break
            if flg:
                break        
            
        # print cave
        # for i in range(len(board)):
        #     print ''.join(board[i])
        # print board
        
        q = list(cave)
        while q:
            y, x, num = q.pop(0)
            # print y, x, num
            if board[y][x] == '.':
                print num - 1
                break
            cave.add((y, x, num))
            for dy, dx in d:
                if 0 <= y + dy < h and 0 <= x + dx < w:
                    for i in range(num+1):
                        if (y + dy, x + dx, i) in cave:
                            break
                    else:
                        q.append((y + dy, x + dx, num + 1))
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()
0