Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def testMaze(n):
- # there is a doorway connecting cell a,b to cell a,b+1 iff
- # columnConnections[a][b] = 1
- columnConnections = [[0 for x in range(n-1)] for x in range(n)];
- for a in range(0, n):
- for b in range(0, n-1):
- columnConnections[a][b] = random.choice([0,1])
- # there is a doorway connecting cell a,b to cell a+1,b iff
- # rowConnections[a][b] = 1
- rowConnections = [[0 for x in range(n)] for x in range(n-1)];
- for a in range(0, n-1):
- for b in range(0, n):
- rowConnections[a][b] = random.choice([0,1])
- #print columnConnections
- #print rowConnections
- connectivityMatrix = [[0 for x in range(n)] for x in range(n)]
- connectivityMatrix[random.choice(range(n))][random.choice(range(n))] = 1
- #connectivityMatrix[0][0] = 1
- for repeat in range(n*n):
- for x in range(n):
- for y in range(n):
- if connectivityMatrix[x][y] == 1:
- if y < n-1 and columnConnections[x][y] == 1:
- connectivityMatrix[x][y+1] = 1;
- if y > 0 and columnConnections[x][y-1] == 1:
- connectivityMatrix[x][y-1] = 1;
- if x < n-1 and rowConnections[x][y] == 1:
- connectivityMatrix[x+1][y] = 1;
- if x > 0 and rowConnections[x-1][y] == 1:
- connectivityMatrix[x-1][y] = 1;
- #print connectivityMatrix
- total = -1;
- for row in connectivityMatrix:
- total = total + sum(row)
- #print total
- #Test if this randomly generated maze is connected.
- return total
- total = 0
- n = 3
- rep = 500
- for x in range(rep):
- total = total + testMaze(n)
- print total / (rep + 0.0)
Advertisement
Add Comment
Please, Sign In to add comment