結果

問題 No.157 2つの空洞
ユーザー lethe2211lethe2211
提出日時 2015-03-02 11:16:29
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 2,236 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 6,660 KB
実行使用メモリ 24,764 KB
最終ジャッジ日時 2023-09-06 06:28:03
合計ジャッジ時間 7,258 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,516 KB
testcase_01 AC 13 ms
6,516 KB
testcase_02 AC 14 ms
6,276 KB
testcase_03 AC 13 ms
6,364 KB
testcase_04 AC 13 ms
6,304 KB
testcase_05 AC 14 ms
6,288 KB
testcase_06 AC 47 ms
7,096 KB
testcase_07 AC 13 ms
6,320 KB
testcase_08 AC 13 ms
6,516 KB
testcase_09 AC 13 ms
6,280 KB
testcase_10 AC 13 ms
6,284 KB
testcase_11 AC 48 ms
6,792 KB
testcase_12 AC 1,697 ms
17,176 KB
testcase_13 AC 537 ms
12,016 KB
testcase_14 AC 14 ms
6,328 KB
testcase_15 AC 16 ms
6,412 KB
testcase_16 AC 16 ms
6,588 KB
testcase_17 AC 15 ms
6,452 KB
testcase_18 AC 99 ms
8,084 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)
        used = set()
        for y, x, num in q:
            used.add((y, x))
        while q:
            y, x, num = q.pop(0)
            # print y, x, num
            if board[y][x] == '.':
                print num - 1
                break
            board[y][x] = str(num)
            used.add((y, x))
            # for i in range(len(board)):
            #     print ''.join(board[i])

            for dy, dx in d:
                if 0 <= y + dy < h and 0 <= x + dx < w and (not (y + dy, x + dx) in used):
                    # for i in range(num+1):
                    #     if (y + dy, x + dx, i) in cave:
                    #         break
                    if board[y+dy][x+dx] == '#' or board[y+dy][x+dx] == '.':
                        q.append((y + dy, x + dx, num + 1))
        
        return None

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