本帖最后由 WEZL 于 2011-1-20 02:02 编辑
八皇后问题 代码有些不明白
在main中 声明For i=0 To n-1
arrResult(0)=array(0,i,0)
应该只会调用 queenspuzzle方法8次,而得到8个值 。但是为什么有92个结果啊
代码如下
Option Explicit
'Script written by <insert name>
'Script copyrighted by <insert company name>
'Script version 2011年1月19日 11:57:51
Call Main()
Sub Main()
Dim n, i,j
Dim kk,arrResult(),count
count=0
n=rhino.GetInteger("number of queens please",8)
kk=0
ReDim arrResult(kk)
For i=0 To n-1
arrResult(0)=array(0,i,0)
Call queenspuzzle(arrResult,1,n,count)
Next
End Sub
Function queenspuzzle(Byval pttest,ByVal column,ByVal n,ByRef count)
Dim k, arrResult(),blnTest,i,j,ptqueen
If column>n Then Exit Function
If column=n Then
'z方向增加
count =count+1
For i=0 To ubound(pttest)
pttest(i)(2)=pttest(i)(2)+count
rhino.Print i
Call addtube(pttest(i))
Next
End If
ReDim arrResult(UBound(pttest))
For i=0 To UBound(pttest)
arrResult(i)=pttest(i)
Next
For i=0 To n-1
'For a=0 To n-1
ptqueen=array(column,i,0)
blnTest=True
For j=0 To ubound(pttest)
'判定能不能相互攻击
If blnTest Then
If (pttest(j)(0)=ptqueen(0) )Or(pttest(j)(1)=ptqueen(1) )Or (abs(pttest(j)(1)+pttest(j)(0))=abs(ptqueen(1)+ptqueen(0)))Or (pttest(j)(1)-pttest(j)(0)=ptqueen(1)-ptqueen(0)) Then
blnTest=False
End If
End If
Next
If blnTest Then
ReDim Preserve arrResult(ubound(pttest)+1)
k=ubound(arrResult)
arrResult(k)=ptqueen
Call queenspuzzle(arrResult,column+1,n,count)
End If
Next
End Function
''''''''''''''
'创建方块
Function addtube(arrpt)
Dim i,edge,arrPtpoly(4),polyline,arrsur,strcur
edge=1
arrPtpoly(0)=arrpt
arrPtpoly(4)=arrpt
arrPtpoly(1)=array(arrpt(0)+edge,arrpt(1),arrpt(2))
arrPtpoly(2)=array(arrpt(0)+edge,arrpt(1)+edge,arrpt(2))
arrPtpoly(3)=array(arrpt(0),arrpt(1)+edge,arrpt(2))
polyline=rhino.AddPolyline(arrptpoly)
arrsur=rhino.AddPlanarSrf(array(polyline))
strcur=rhino.AddLine(array(0,0,0),array(0,0,1))
Call rhino.ExtrudeSurface(arrsur(0),strcur)
End Function |