2016-04-19 16 views
-1

enter image description hereは、唯一のIFテスト= TRUE

こんにちは場合の要素のリンクを作り、私はPythonでグラフを作成したいと思います。私はテキストファイルの配列を読み、配列の各要素を、上、下、左、右にある要素と関連付ける必要があります。画像が示すように、要素1と要素4が空いている場合、要素 "0"のテストを行います。そして、グラフモデルにグラフを生成する:

グラフは、辞書( http://www.tutorialspoint.com/python/python_dictionary.htm)私は右アムいるドキュメントに記載されてい
graph= {0:[1,4], 
     1:[2], ... 
    } 

テストにリンクしている辞書の要素をグループ化する方法についてのヒントは本当ですか?


マイコード@TonyBabarino:maze.txtで

111111111111111111111 
121000100000013000001 
101011111011111110101 
101010000010000000101 
101010111110111111101 
100010100000100010001 
101110001110101010101 
100000100010001000101 
111111111111111111111 

いくつかのセクションでは、テストのためだけです。

import numpy as np 

with open ('maze.txt') as buffer: 
    M = list(map(int, line.strip()) for line in buffer) 

print(np.matrix(M)) 

def encontra_inicio(matriz): # retorna (x,y) do inicio 
    for i in range (len(matriz)): 
     for j in range(len(matriz[i])): 
      if (matriz[i][j] == 2): 
       return [i,j] 

def encontra_fim(matriz): 
    for i in range (len(matriz)): 
     for j in range(len(matriz[i])): 
      if (matriz[i][j] == 3): 
       return [i,j] 

def freePath(M,xy,stack): 
    x=xy[0] 
    y=xy[1] 
    if(xy in stack): 
     if(M[x+1][y] == 0): #baixo 
      jaVisitado([x+1,y],stack) 

     if(M[x-1][y] == 0): #cima 
      jaVisitado([x-1,y],stack) 

     if(M[x][y+1] == 0): #direita 
      jaVisitado([x,y+1],stack) 

     if(M[x][y-1] == 0): #esquerda 
      jaVisitado([x,y-1],stack) 
    return 

def jaVisitado(xy, stack): 
    if(xy not in stack): 
     stack.append(xy) 
    else: 
     return 

def posCursor(xy,stack,i): 
    aux=i 
    x=stack[aux-1][0] #salva a coordenada x da stack na variavel x 
    y=stack[aux-1][1] #salva a coordenada y da stack na variavel y 
    xy[0]=x 
    xy[1]=y 
    return xy 


i=0 
stack=[] 
xy=[] 
xy=encontra_inicio(M) 
stack.append(encontra_inicio(M)) 

for j in range(20): 
    freePath(M,xy,stack) 
    posCursor(xy,stack,i) 
print i 
print 
print stack 
+0

は、あなたは本当にdictのグラフを作成する必要がありますか?あなたの最初のイメージのようなマトリックスを使う方が簡単でしょうか?また、私は 'IF(M [0] [1] == FREE)'の意味を理解していません。 'FREE'とは何ですか?入力例と期待される出力を与えてください。 –

+0

FREE = 0、BUSY = 1。私の行列は0と1で作られています。だから私は壁が1で表され、フリーウェイは0で表される迷路を持っています.DFSまたはBFSグラフ検索アルゴリズムで使用するためにGRAPHを構築する必要があります。 – ialsafid

+0

ああ、私は今理解しています。そしてなぜあなたはその行列で作業したいのですか?あなたは、行列内のセルを与えた簡単な関数を書くことができます。 –

答えて

0

私の解決策は、DFSを使用して迷路内のパスを見つけることです。それはスタックにまだいくつかの関数呼び出しがあるので、終了後いくつかのセルを返しますが、私はあなたがそれを調整することができると確信しているので、あなたの問題でより良く適合します。

import numpy as np 
from sets import Set 

with open('maze.txt') as buffer: 
    M = np.matrix(list(map(int, line.strip()) for line in buffer)) 

print M 

def encontra_inicio(matriz): # retorna (x,y) do inicio 
    foo = np.where(M == 2) 
    return foo[0][0], foo[1][0] 

def freePath(M, xy, visited): 
    print str(xy) + '->', 
    x = xy[0] 
    y = xy[1] 
    if M[x+1, y] == 3 or M[x-1, y] == 3 or M[x, y+1] == 3 or M[x, y-1] == 3: 
     print 'FINISHED!' 
     return 
    if M[x+1, y] == 0 and not (x+1, y) in visited: 
     visited.add((x+1, y)) 
     freePath(M, (x+1, y), visited) 
    if M[x-1, y] == 0 and not (x-1, y) in visited: 
     visited.add((x-1, y)) 
     freePath(M, (x-1, y), visited) 
    if M[x, y+1] == 0 and not (x, y+1) in visited: 
     visited.add((x, y+1)) 
     freePath(M, (x, y+1), visited) 
    if M[x, y-1] == 0 and not (x, y-1) in visited: 
     visited.add((x, y-1)) 
     freePath(M, (x, y-1), visited) 
    return 

visited = Set([]) 
xy = encontra_inicio(M) 
freePath(M, xy, visited) 

リターン:

[[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 
[1 2 1 0 0 0 1 0 0 0 0 0 0 1 3 0 0 0 0 0 1] 
[1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1] 
[1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1] 
[1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1] 
[1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1] 
[1 0 1 1 1 0 0 0 1 1 1 0 1 0 1 0 1 0 1 0 1] 
[1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1] 
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]] 
(1, 1)-> (2, 1)-> (3, 1)-> (4, 1)-> (5, 1)-> (6, 1)-> (7, 1)-> (7, 2)-> (7, 3)-> (7, 4)-> (7, 5)-> (6, 5)-> (5, 5)-> (4, 5)-> (3, 5)-> (3, 6)-> (3, 7)-> (3, 8)-> (3, 9)-> (2, 9)-> (1, 9)-> (1, 10)-> (1, 11)-> (1, 12)-> (1, 8)-> (1, 7)-> (6, 6)-> (6, 7)-> (7, 7)-> (7, 8)-> (7, 9)-> (5, 7)-> (5, 8)-> (5, 9)-> (5, 10)-> (5, 11)-> (6, 11)-> (7, 11)-> (7, 12)-> (7, 13)-> (6, 13)-> (5, 13)-> (5, 14)-> (5, 15)-> (6, 15)-> (7, 15)-> (7, 16)-> (7, 17)-> (6, 17)-> (5, 17)-> (5, 18)-> (5, 19)-> (6, 19)-> (7, 19)-> (4, 19)-> (3, 19)-> (2, 19)-> (1, 19)-> (1, 18)-> (1, 17)-> (2, 17)-> (3, 17)-> (3, 16)-> (3, 15)-> (3, 14)-> (3, 13)-> (3, 12)-> (3, 11)-> (4, 11)-> (1, 16)-> (1, 15)-> FINISHED! 
(5, 2)-> (5, 3)-> (4, 3)-> (3, 3)-> (2, 3)-> (1, 3)-> (1, 4)-> (1, 5)->