-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine.py
147 lines (119 loc) · 4.41 KB
/
mine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
import heapq
class tile:
val = 0
added = False
cleared = False
tnt_seen = False
def __init__(self, val):
self.val = val
def print_grid(grid):
for x in grid:
print x.val,
def add_tiles(grid, pq, current, size):
row = current[2]
col = current[1]
up = (row-1)*size + col
down = (row+1)*size + col
left = row * size + (col-1)
right = row * size + (col + 1)
if grid[up].added == False:
grid[up].added = True
x = (grid[up].val, up % size, up / size) #rubble, col, row
heapq.heappush(pq, x)
if grid[down].added == False:
grid[down].added = True
x = (grid[down].val, down % size, up / size) #rubble, col, row
heapq.heappush(pq, x)
if grid[left].added == False:
grid[left].added = True
x = (grid[left].val, left % size, left / size) #rubble, col, row
heapq.heappush(pq, x)
if grid[right].added == False:
grid[right].added = True
x = (grid[right].val, right % size, right / size) #rubble, col, row
heapq.heappush(pq, x)
def add_tiles_TNT(grid, pq, current, size):
row = current[2]
col = current[1]
up = (row-1)*size + col
down = (row+1)*size + col
left = row * size + (col-1)
right = row * size + (col + 1)
if row != 0 and not grid[up].cleared and not grid[up].tnt_seen:
grid[up].tnt_seen = True
x = (grid[up].val, up % size, up / size)
heapq.heappush(pq, x)
if row != size - 1 and not grid[down].cleared and not grid[down].tnt_seen:
grid[down].tnt_seen = True
x = (grid[down].val, down % size, down / size)
heapq.heappush(pq, x)
if col != 0 and not grid[left].cleared and not grid[left].tnt_seen:
grid[left].tnt_seen = True
x = (grid[left].val, left % size, left / size)
heapq.heappush(pq, x)
if col != size - 1 and not grid[right].cleared and not grid[right].tnt_seen:
grid[right].tnt_seen = True
x = (grid[right].val, right % size, right / size)
heapq.heappush(pq, x)
def TNT_det(grid, pq, current, size, count, tot):
add_tiles_TNT(grid, pq, current, size)
while pq:
current = pq[0]
pos = current[2] * size + current[1]
grid[pos].tnt_seen = False
heapq.heappop(pq)
tile_val = 0
if not grid[pos].cleared:
tile_val = grid[pos].val
grid[pos].cleared = True
if tile_val == -1:
TNT_det(grid,pq,current,size,count,tot)
elif tile_val > 0:
count[0] += 1
tot[0] += tile_val
#else: do nothing because tile = 0
def main():
#get essential numbers from the file
type = raw_input()
minesize = int(raw_input().split()[1]) #size: 7, grab second elt
temp = raw_input().split()
rowstart = int(temp[1])
colstart = int(temp[2])
vals = [] #get all grid values as a list
for x in xrange(minesize):
a = raw_input().split() #default separator is " " (whitespace) so this is the same as inp[x].split(" ")
vals += a
grid = [] #make grid of tiles
for x in vals:
# grid += tile(x) #why doesn't this work?
grid.append(tile(int(x)))
current = (grid[rowstart * minesize + colstart].val, colstart, rowstart)
cleared_count = [0]
total_rubble = [0]
pq = [] #initialize empty pq (list to be manipulated by heapq functions)
#pushed to PQ will be a tuple: rubble, col, row
pos = current[2]*minesize + current[1]
grid[pos].added = True
heapq.heappush(pq,current)
while(True):
current = pq[0]
heapq.heappop(pq)
add_tiles(grid, pq, current, minesize)
pos = current[2]*minesize + current[1] #grid index
if grid[pos].cleared == False:
tile_val = grid[pos].val
grid[pos].cleared = True
if tile_val > 0:
cleared_count[0] += 1
total_rubble[0] += tile_val
else:
q = []
TNT_det(grid, q, current, minesize, cleared_count, total_rubble)
#update priorities
#print current[2], current[1]
if (current[2] == 0 or current[2] == minesize - 1 or current[1] == 0 or current[1] == minesize - 1):
break
print "Cleared %d tiles containing %d rubble and escaped." % (cleared_count[0], total_rubble[0])
if __name__ == "__main__":
main()