mebipenny/clear-the-way/clear_the_way.py

75 lines
1.7 KiB
Python
Raw Normal View History

2012-09-08 09:27:46 -07:00
import sys
from collections import defaultdict
from itertools import product
_around_stencil = [(i, j) for i, j in product([-1, 0, 1], repeat=2)
if (i, j) != (0, 0)]
def empty_board(n, m):
board = []
for i in xrange(N):
board.append([False] * m)
return board
def around(I, J):
def f(i, j):
for _i, _j in _around_stencil:
yield (i + _i, j + _j)
return f(I, J)
N, M, K = [int(i) for i in sys.stdin.readline().split()]
board = empty_board(N, M)
bombs = []
for line in range(K):
x, y = [int(i) for i in sys.stdin.readline().strip().split()]
board[x][y] = True
2012-09-08 11:46:20 -07:00
bombs.append((x, y))
2012-09-08 09:27:46 -07:00
2012-09-08 11:46:20 -07:00
click = tuple(int(i) for i in sys.stdin.readline().strip().split())
if click in bombs:
sys.exit()
2012-09-08 09:27:46 -07:00
2012-09-08 11:46:20 -07:00
adjacent_to_bombs = defaultdict(int)
2012-09-08 09:27:46 -07:00
for bomb in bombs:
for ni, nj in around(*bomb):
if ni >= 0 and nj >= 0 and ni < N and nj < M and [ni, nj] not in bombs:
2012-09-08 11:46:20 -07:00
adjacent_to_bombs[ni, nj] += 1
if tuple(click) in adjacent_to_bombs.keys():
sys.exit()
2012-09-08 09:27:46 -07:00
for line in board:
print line
2012-09-08 11:46:20 -07:00
unobscured = {}
visited = {}
def seek_and_destroy(dude, unobscured):
if dude in unobscured:
return
unobscured[dude] = True
to_recurse = {}
for ni, nj in around(*dude):
if (ni >= 0 and nj >= 0 and ni < N and nj < M) and \
((ni, nj) not in bombs):
candidate = (ni, nj)
unobscured[candidate] = True
to_recurse[candidate] = True
print ">>>", to_recurse.keys()
for c in to_recurse.keys():
seek_and_destroy(c, unobscured)
seek_and_destroy(click, unobscured)
print "\n\n"
print "\n".join('{} {}'.format(*i) for i in sorted(unobscured.keys()))