Last active
February 5, 2019 19:41
-
-
Save TheAnimatrix/7e28806320b1d5cf0d3433f96ca16b16 to your computer and use it in GitHub Desktop.
bomberman.py
This file contains 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
#!/bin/python | |
import math | |
import os | |
import random | |
import re | |
import sys | |
import copy | |
# Complete the bomberMan function below. | |
def bomberMan(row, col, n, grid): | |
states = [] | |
states.append(copy.deepcopy(grid)) | |
for state in range(1,n+1): | |
for r in range(row): | |
for c in range(col): | |
if grid[r][c] == '.': | |
grid[r][c] = 4 | |
continue | |
if grid[r][c] == 'a': | |
grid[r][c] = '.' | |
if grid[r][c] == 'O' or grid[r][c] == 0: | |
grid[r][c] = 3 | |
continue | |
if type(grid[r][c]) is int: | |
grid[r][c] -= 1 | |
if grid[r][c] == 1: | |
grid[r][c] = '.' | |
if r-1 >= 0 and grid[r-1][c] != 'a': grid[r-1][c] = '.' | |
if c-1 >= 0 and grid[r][c-1] != 'a': grid[r][c-1] = '.' | |
if r+1 < row and grid[r+1][c] != '.' and grid[r+1][c] != 2: | |
grid[r+1][c] = 'a' | |
if c+1 < col and grid[r][c+1] != '.' and grid[r][c+1] != 2: | |
grid[r][c+1] = 'a' | |
states.append(copy.deepcopy(grid)) | |
print len(states) | |
return states[n] | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
rcn = raw_input().split() | |
r = int(rcn[0]) | |
c = int(rcn[1]) | |
n = int(rcn[2]) | |
grid = [] | |
for _ in xrange(r): | |
grid_item = raw_input() | |
grid.append(list(grid_item)) | |
result = bomberMan(r,c,n, grid) | |
crap="" | |
for i in range(len(result)): | |
y="" | |
for x in result[i]: | |
x=str(x) | |
if x != '.': | |
x='O' | |
y+=x+"" | |
print y | |
crap+= y+"\n" | |
# print ' '.join(str(x) for x in result[i]) | |
fptr.write(crap) | |
fptr.write('\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment