Created
January 1, 2014 16:09
-
-
Save cocodrips/8209131 to your computer and use it in GitHub Desktop.
Codeforces Round #222 (Div. 2) C. Maze 解けない。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Queue | |
def solve(k, strs): | |
strsX = [] | |
for str in strs: | |
strsX.append(list(str.replace('.', 'X'))) | |
num_X = countX(strsX) | |
strsX = replaceX(strsX, num_X - k) | |
for str in strsX: | |
print ''.join(str) | |
def replaceX(strs, num): | |
candis = [(0, 1), (1, 0), (0, -1), (-1, 0)] | |
queue = Queue.deque() | |
queue.append(firstX(strs)) | |
visited = [] | |
while num > 0 and queue: | |
q = queue.pop() | |
x, y = q[0], q[1] | |
strs[x][y] = '.' | |
num -= 1 | |
for candi in candis: | |
nx, ny = x + candi[0], y + candi[1] | |
if 0 <= nx < len(strs) and 0 <= ny < len(strs[0]): | |
if strs[nx][ny] == 'X' and (nx, ny) not in visited: | |
queue.append((nx, ny)) | |
visited.append((nx, ny)) | |
return strs | |
def countX(str): | |
counter = 0 | |
for i in xrange(len(str)): | |
for j in xrange(len(str[i])): | |
if str[i][j] == 'X': | |
counter += 1 | |
return counter | |
def firstX(str): | |
for i in xrange(len(str)): | |
for j in xrange(len(str[i])): | |
if str[i][j] == 'X': | |
return (i, j) | |
if __name__ == "__main__": | |
n, m, k = map(int, raw_input().split()) | |
strs = [] | |
for i in xrange(n): | |
strs.append(raw_input()) | |
solve(k, strs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment