【问题标题】:problem with breadth first tree generation广度优先树生成问题
【发布时间】:2010-01-31 13:00:31
【问题描述】:

我在使用广度优先算法时遇到问题,我的脚本在 Maya 中生成曲线,定位它们,旋转和缩放它们,以便它们给我树形, 我有这些变量

cs=当前状态,

p=父母,

nodes=未访问的节点列表

lvl=当前深度

maxlvl= 最大深度

问题是我无法确定当前深度,并在那里终止树, 它存在而不访问所有节点

这是我的脚本

import random
import math
import maya.cmds as mc
#----define set-----
#--- curve, #children,angle,l--------
#----define rule----
#    l=80%   ch<=3   angle<=45
l=0.8
ch=6
ang=50
#---list to track----
nodes=[]
#--- begin-----
#---start curve-----
nodes.append(mc.curve(n="a",d=3,p=((0,0,0),(0,1,0),(0,3,0),(0,5,0),(0,6,0)),k=(0,0,0,1,2,2,2)))
cs="" #---current state----
p="a" #---paretn of the current state----
maxlvl=6 #--max depth for the tree
lvl=1 #-- cuurent level----
while (len(nodes)!=0 and lvl<5):
 #---generate children number----
 chN=random.randint(0,ch)
 for j in range(chN):
  #----generate node----
  mc.select(p,r=1)
  cs=mc.duplicate(rr=1)[0]
  #---append to node lst----
  nodes.append(cs)
  #---adjust pos---
  pos=mc.pointPosition(p+".cv[4]")
  mc.xform(cs,t=(pos[0],pos[1],pos[2]),ws=1)
  #---adjust rotation---
  mc.rotate(0,0,(random.random()*45+5)*math.pow(-1,j%2),cs,r=1)
  #---adjust scale---
  mc.scale(0,math.pow(0.8,lvl),0,cs)
  j+=1
 #--- go to next parent---
 nodes.pop(0)
 if nodes:
  p=nodes[0]
 lvl+=1

提前谢谢你

【问题讨论】:

    标签: python tree breadth-first-search


    【解决方案1】:

    您需要将深度与每个节点相关联。要么将其作为节点类的成员,要么让您的队列条目存储深度和节点,如下所示:

    nodes.append((1, firstNode))
    while nodes and lvl<5:
        lvl, p = nodes.pop(0)
    
        For each child:
            #...create child
            nodes.append((lvl+1, cs))
    

    一些不相关的注释:

    • 使用collections.deque 而不是list:FIFO 队列更快。

    • j+=1 语句在 for 循环中是不必要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2011-02-19
      • 2016-02-15
      相关资源
      最近更新 更多