Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Forked from delevaa/gist:4065245
Created November 13, 2012 11:09
Show Gist options
  • Save whoeverest/4065253 to your computer and use it in GitHub Desktop.
Save whoeverest/4065253 to your computer and use it in GitHub Desktop.
table=[" x x xx",
" x xx x",
" x xx x",
" x x ",
" x x "]
def numAllPercs(table, numStreams):
matrica=[list(x) for x in table]
def sosedi2(e):
temp=[]
y=e[1]
x=e[0]
'''left neighbour'''
if y>0 & y<=len(table[0])-1 & x>=0 & x<=len(table)-1:
if matrica[x][y-1]==" ":
temp.append((x,y-1))
'''right neighbour'''
if len(table[0])-1>y & y>=0 & x>=0 & x<=len(table)-1:
if matrica[x][y+1]==" ":
temp.append((x,y+1))
'''neighbour in next row'''
if x!=len(matrica)-1:
if len(table)-1>=x & y>=0 & x>=0 &y<=len(table[0])-1:
if matrica[x+1][y]==" ":
temp.append((x+1,y))
return temp
def find_all_paths(graph, start, end,path=[]):
path = path + [start]
if start == end:
return [path]
if not graph.has_key(start):
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def next_stream_paths(matrix,numStreams):
pocetni_sostojbiF=[]
[[pocetni_sostojbiF.append(((0,i)))for mmmm in matrix[0][i] if mmmm==" "] for i in range(0,len(table[0]))]
krajni_sostojbiF=[]
[[krajni_sostojbiF.append(((len(matrix)-1,i)))for mmmm in matrica[len(matrix)-1][i] if mmmm==" "] for i in range(0,len(table[0]))]
adjacency_matrixF={}
elF=[]
[[ elF.append((mattttt,maaaaa)) for maaaaa in range(len(matrix[mattttt])) if matrix[mattttt][maaaaa]==" " ]for mattttt in range(len(table))]
elF
pateki=[]
for e in range(len(elF)):
adjacency_matrixF[elF[e]]=sosedi2(elF[e])
for i in range(len(pocetni_sostojbiF)):
for e in range(len(krajni_sostojbiF)):
if find_all_paths(adjacency_matrixF,pocetni_sostojbiF[i],krajni_sostojbiF[e])!=[]:
pateki=pateki+find_all_paths(adjacency_matrixF,pocetni_sostojbiF[i],krajni_sostojbiF[e])
if numStreams==1:
print len(pateki)
return len(pateki)
else:
for pat in range(len(pateki)):
m=matrix
for p in pateki[pat]:
m[p[0]][p[1]]='x'
next_stream_paths(matrix,numStreams-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment