|
楼主 |
发表于 2008-11-9 09:26
|
显示全部楼层
以下是有点像灌水原理解题代码
Sub xi()
Dim i As Long
Dim j As Long
Dim jj As Long
Dim x As Long
Dim y As Long
t = Timer
arr = Range("A1:AE" & [b65536].End(xlUp).Row)
ReDim arr1(1 To 1000, 1 To 4) As Long
y = 1: yy = 3: x = 1
arr1(1, 1) = 2
arr1(1, 2) = 2
For z = 1 To 999999999 '灌水
xx = x
x = 0
For i = 1 To xx
For j = -1 To 1
For jj = -1 To 1
If j * jj = 0 Then
If arr(arr1(i, y) - j, arr1(i, y + 1) - jj) = -1 Then
arr(arr1(i, y) - j, arr1(i, y + 1) - jj) = z
x = x + 1
arr1(x, yy) = arr1(i, y) - j '记录位置
arr1(x, yy + 1) = arr1(i, y + 1) - jj
End If
End If
Next
Next
Next i
If y = 1 Then '交换位置
y = 3: yy = 1
Else
y = 1: yy = 3
End If
If x = 0 Then Exit For
Next z
x = UBound(arr) - 1
y = 2
For i = arr(x, y) To 1 Step -1 '找到回家的路
For j = -1 To 1 '
For jj = -1 To 1
If j * jj = 0 Then
If arr(x - j, y - jj) = i Then
arr(x, y) = 1
x = x - j
y = y - jj
GoTo ren:
End If
End If
Next
Next
ren:
Next i
Range("A1:AD" & [b65536].End(xlUp).Row) = arr
MsgBox ("耗时" & Timer - t & "秒")
End Sub
[ 本帖最后由 彭希仁 于 2008-11-9 20:47 编辑 ] |
|