/
percolation.py
88 lines (69 loc) · 2.21 KB
/
percolation.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
#-----------------------------------------------------------------------
# percolation.py
#-----------------------------------------------------------------------
import stdio
import stdarray
import stddraw
import stdrandom
#-----------------------------------------------------------------------
# Fill every site reachable from the top row.
def flow(open):
n = len(open)
full = stdarray.create2D(n, n, False)
# Percolation flow computation goes here.
for j in range(n):
flowHelp(open, full, 0, j)
return full
#-----------------------------------------------------------------------
# Fill every site reachable from (i, j).
def flowHelp(open, full, i, j):
n = len(full)
if (i < 0) or (i >= n):
return
if (j < 0) or (j >= n):
return
if not open[i][j]:
return
if full[i][j]:
return
full[i][j] = True
flowHelp(open, full, i+1, j ) # Down.
flowHelp(open, full, i , j+1) # Right.
flowHelp(open, full, i , j-1) # Left.
flowHelp(open, full, i-1, j ) # Up.
#-----------------------------------------------------------------------
def percolates(open):
full = flow(open)
n = len(full)
for j in range(n):
if full[n-1][j]:
return True
return False
#-----------------------------------------------------------------------
def show(a, which):
n = len(a)
stddraw.setXscale(-1, n)
stddraw.setYscale(-1, n)
for i in range(n):
for j in range(n):
if a[i][j] == which:
stddraw.filledSquare(j, n-i-1, .5)
stddraw.show()
#-----------------------------------------------------------------------
def random(n, p):
a = stdarray.create2D(n, n, False)
for i in range(n):
for j in range(n):
a[i][j] = stdrandom.bernoulli(p)
return a
#-----------------------------------------------------------------------
def main():
open = stdarray.readBoolean2D()
stdarray.write2D(flow(open))
stdio.writeln(percolates(open))
if __name__ == '__main__':
main()
#-----------------------------------------------------------------------
# Example executions:
# python percolation.py < test5.txt
# python percolation.py < test8.txt