結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-03-02 11:16:29 |
| 言語 | Python2 (2.7.18) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,236 bytes |
| コンパイル時間 | 176 ms |
| コンパイル使用メモリ | 6,912 KB |
| 実行使用メモリ | 27,680 KB |
| 最終ジャッジ日時 | 2024-06-24 01:07:56 |
| 合計ジャッジ時間 | 6,465 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 TLE * 1 |
ソースコード
#! /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()