1 import random
2 import sys
3 import stdio
4 import stdarray
5 n = int(sys.argv[1])
6 trials = int(sys.argv[2])
7 deadEnds = 0
8
9 for t in range(trials):
10 a = stdarray.create2D(n, n, False)
11 x = n//2
12 y = n//2
13 while (x > 0) and (x < n-1) and (y > 0) and (y < n-1):
14 #Check for dead end and make a random move.
15 a[x][y] = True
16 if a[x+1][y] and a[x][y+1] and a[x-1][y] and a[x][y-1]:
17 deadEnds += 1
18 break
19 r = random.randrange(1, 5)
20 if (r == 1) and (not a[x+1][y]): x += 1
21 if (r == 2) and (not a[x-1][y]): x -= 1
22 if (r == 3) and (not a[x][y+1]): y += 1
23 if (r == 4) and (not a[x][y-1]): y -= 1
24 stdio.writeln(str(100*deadEnds//trials)+'% dead ends')