2013-06-04 103 views
0

我想寫一個函數,它需要一個網格中的數字或者行和列,模擬從網格中心開始的隨機遊走,並且計算每個交叉點的次數由隨機遊走訪問。然後,一旦隨機遊走到網格外,就會一行一行地打印表格 到目前爲止,我有這個,但是我無法讓它正常工作。曼哈頓網格方向隨機遊走

def manhattan(x,y): 
    'int,int==>nonetype' 
    import random 
    res=[] 
    for i in range(x): 
     res.append([]) 
    for i in res: 
     for j in range(y): 
      i.append(0) 
    position=(x//2+1,y//2+1) 
    z=position[0] 
    v=position[1] 

    while z!=-1 or z!=x or v!=-1 or v!=y: 
     direction=random.randrange(1,5) 
     if direction==1: 
      v+=1 
     elif direction==2: 
      z+=1 
     elif direction==3: 
      v-=1 
     else: 
      z-=1 
     for i in range(len(res)): 
      if i ==z: 
       res[i]+=1 
      for j in range(i): 
       if v==j: 
        i[j]+=1 
    for i in res: 
     print(i) 

完成後它應該閱讀:

manhattan(5,11) 
[0,0,0,0,0,0,0,0,0,0,0] 
[0,0,0,0,0,0,0,0,0,0,0] 
[0,0,0,0,0,1,1,1,1,2,2] 
[0,0,0,0,0,0,0,0,0,0,0] 
[0,0,0,0,0,0,0,0,0,0,0] 
+1

什麼是不工作?或者這是作業嗎? – Schorsch

回答

1

你非常接近,請嘗試以下操作:

def manhattan(x,y): 
    'int,int==>nonetype' 
    import random 
    res=[] 
    for i in range(x): 
     res.append([]) 
    for i in res: 
     for j in range(y): 
      i.append(0) 
    position=(x//2+1,y//2+1) 
    z=position[0] 
    v=position[1] 

    while z!=-1 and z!=x and v!=-1 and v!=y: 
     res[z][v] += 1 
     direction=random.randrange(1,5) 
     if direction==1: 
      v+=1 
     elif direction==2: 
      z+=1 
     elif direction==3: 
      v-=1 
     else: 
      z-=1 
    for i in res: 
     print(i) 

沒有什麼,直到while環不同,只有一對夫婦的變化。首先,您需要在循環條件檢查中使用and而不是or,因爲如果滿足這些條件中的任何一個,就要退出。

另一個變化是由res[z][v] += 1while循環的底部取出for循環和更換,這樣做是因爲zv代表的交集,你已經初始化res是所有的二維表交叉點如此循環是不必要的。我也將其移至循環的頂部,否則您可能會嘗試在移過邊界後修改res

+0

這裏有一些錯誤。它不是以第一個位置上的1開始,它不跟蹤我們移動的第一個位置,所以說它獲取方向2然後是3然後2它應該向右移動1然後向下移動1然後再向右移動1並繼續。在這裏停止並給出[0,0,0,0,0,0,0,0,0,0] [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 0] [0,0,0,0,0,0,0,0,0,0] [0,0,0,0,0,0,0,0,0,0] [0,0,0,0,0,1,1,0,0,0,0] – blandman1990

+0

沒關係,我已經開始工作了。需要的位置是x // 2,y // 2否則加1 – blandman1990

1

這是一個不太冗長的版本,它使用random.choice而不是你的鏈接elif陳述。我發現它在學習python以不同方式查看相同問題時很有用,所以這裏是一個純python和一個numpy + python實現。

純Python

import random 

def manhattan(n,m): 
    grid = [[0,]*m for _ in xrange(n)] 
    directions = [[-1,0],[1,0],[0,-1],[0,1]] 
    pt = [n//2, m//2] 

    while pt[0]>=0 and pt[0]<n and pt[1]>=0 and pt[1]<m: 
     grid[pt[0]][pt[1]] += 1 
     d = random.choice(directions) 
     pt[0] += d[0] 
     pt[1] += d[1] 
    return grid 

for row in manhattan(5,11): 
    print row 

這給了,例如,

[0, 0, 0, 1, 3, 3, 0, 0, 0, 0, 0] 
[0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0] 
[0, 0, 0, 0, 1, 3, 3, 2, 0, 0, 0] 
[0, 0, 0, 0, 0, 1, 2, 2, 1, 0, 0] 
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0] 

的Python + numpy的

import numpy as np 
import random 

def manhattan(n,m): 
    grid = np.zeros((n,m),dtype=int) 
    directions = [[-1,0],[1,0],[0,-1],[0,1]] 
    pt = np.array([n//2, m//2]) 

    while (pt>=0).all() and (pt<grid.shape).all(): 
     grid[pt[0],pt[1]] += 1  
     pt += random.choice(directions) 
    return grid 

print manhattan(5,11)