| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 898 人关注过本帖
标题:求人帮忙找错啊~function自循环做穷举法解数独
只看楼主 加入收藏
run93
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-1-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求人帮忙找错啊~function自循环做穷举法解数独
vb初学者。突发奇想用vb写个程序解数独。可是卡住了。
解释一下代码吧。我是81个文本框。(现在知道可以用数组控件,这个问题不大)
XX数组是三维的,前两维确定他的位置,第三个维度是用来方便计算的,第三维通过前两维求出。
x数组是二维的,第一维度是他的位置(第x个数),第二个维度用来储存可能的数值,这个维度先dim了1-9,再通过XX的三维查找出存在的数字,把x里面的对应的数字变为0,最后对非0数操作,这样就保证了每行每列每个九宫格都是1-9。
其他的i,j,n全是存数字用,没有含义。

但是。。。。。。做出来不对啊不对啊不对啊啊啊啊啊。。。。。


Option Explicit
Dim XX(1 To 9, 1 To 9, 1 To 9) As Integer
Dim x(1 To 81, 1 To 9) As Integer


Private Function jisuan(ByVal a As Integer, ByVal b As Integer) As Boolean
Dim c, i, j, n, As Integer
    If a = 10 Then Exit Function
            If a > 6 Then                           '判断第三维
                If b > 6 Then
                    c = 9
                ElseIf b > 3 Then
                    c = 8
                Else
                    c = 7
                End If
            ElseIf a > 3 Then
                If b > 6 Then
                    c = 6
                ElseIf b > 3 Then
                    c = 5
                Else
                    c = 4
                End If
            Else
                If b > 6 Then
                    c = 3
                ElseIf b > 3 Then
                    c = 2
                Else
                    c = 1
                End If
            End If                                      '定位完毕

                n = a * 9 + b - 9                       'n是所在的个数        
      
           If XX(a, b, c) = 0 Then                     '如果这个数是未知
                 For i = 1 To 9
                    x(n, i) = i
                Next i
               

                For i = 1 To 9                        '排除法去已知数
                    For j = 1 To 9
                        If XX(i, j, c) <> 0 Then x(n, XX(i, j, c)) = 0
                        If XX(i, b, j) <> 0 Then x(n, XX(i, b, j)) = 0
                        If XX(a, i, j) <> 0 Then x(n, XX(a, i, j)) = 0
                    Next j
                Next i
               
                For i = 1 To 9
                    jisuan = False
                    
                        If x(n, i) <> 0 Then
                            jisuan = True
                            XX(a, b, c) = x(n, i)
                        End If
                    
                    If jisuan = True Then
                            If b = 9 Then
                                If jisuan(a + 1, 1) = True Then Exit Function
                            Else
                                If jisuan(a, b + 1) = True Then Exit Function
                            End If
                    End If
                 Next i

            Else     '如果这个数已知  很怀疑这里有问题

                            If b = 9 Then
                                jisuan = jisuan(a + 1, 1)
                            Else
                                jisuan = jisuan(a, b + 1)
                            End If
            End If
End Function





Private Sub Command1_Click()
        XX(1, 1, 1) = Val(Text1.Text)
        XX(1, 2, 1) = Val(Text2.Text)
        XX(1, 3, 1) = Val(Text3.Text)
        XX(1, 4, 2) = Val(Text4.Text)
        XX(1, 5, 2) = Val(Text5.Text)
        XX(1, 6, 2) = Val(Text6.Text)
        XX(1, 7, 3) = Val(Text7.Text)
        XX(1, 8, 3) = Val(Text8.Text)
        XX(1, 9, 3) = Val(Text9.Text)
        XX(2, 1, 1) = Val(Text10.Text)
        XX(2, 2, 1) = Val(Text11.Text)
        XX(2, 3, 1) = Val(Text12.Text)
        XX(2, 4, 2) = Val(Text13.Text)
        XX(2, 5, 2) = Val(Text14.Text)
        XX(2, 6, 2) = Val(Text15.Text)
        XX(2, 7, 3) = Val(Text16.Text)
        XX(2, 8, 3) = Val(Text17.Text)
        XX(2, 9, 3) = Val(Text18.Text)
        XX(3, 1, 1) = Val(Text19.Text)
        XX(3, 2, 1) = Val(Text20.Text)
        XX(3, 3, 1) = Val(Text21.Text)
        XX(3, 4, 2) = Val(Text22.Text)
        XX(3, 5, 2) = Val(Text23.Text)
        XX(3, 6, 2) = Val(Text24.Text)
        XX(3, 7, 3) = Val(Text25.Text)
        XX(3, 8, 3) = Val(Text26.Text)
        XX(3, 9, 3) = Val(Text27.Text)
        XX(4, 1, 4) = Val(Text28.Text)
        XX(4, 2, 4) = Val(Text29.Text)
        XX(4, 3, 4) = Val(Text30.Text)
        XX(4, 4, 5) = Val(Text31.Text)
        XX(4, 5, 5) = Val(Text32.Text)
        XX(4, 6, 5) = Val(Text33.Text)
        XX(4, 7, 6) = Val(Text34.Text)
        XX(4, 8, 6) = Val(Text35.Text)
        XX(4, 9, 6) = Val(Text36.Text)
        XX(5, 1, 4) = Val(Text37.Text)
        XX(5, 2, 4) = Val(Text38.Text)
        XX(5, 3, 4) = Val(Text39.Text)
        XX(5, 4, 5) = Val(Text40.Text)
        XX(5, 5, 5) = Val(Text41.Text)
        XX(5, 6, 5) = Val(Text42.Text)
        XX(5, 7, 6) = Val(Text43.Text)
        XX(5, 8, 6) = Val(Text44.Text)
        XX(5, 9, 6) = Val(Text45.Text)
        XX(6, 1, 4) = Val(Text46.Text)
        XX(6, 2, 4) = Val(Text47.Text)
        XX(6, 3, 4) = Val(Text48.Text)
        XX(6, 4, 5) = Val(Text49.Text)
        XX(6, 5, 5) = Val(Text50.Text)
        XX(6, 6, 5) = Val(Text51.Text)
        XX(6, 7, 6) = Val(Text52.Text)
        XX(6, 8, 6) = Val(Text53.Text)
        XX(6, 9, 6) = Val(Text54.Text)
        XX(7, 1, 7) = Val(Text55.Text)
        XX(7, 2, 7) = Val(Text56.Text)
        XX(7, 3, 7) = Val(Text57.Text)
        XX(7, 4, 8) = Val(Text58.Text)
        XX(7, 5, 8) = Val(Text59.Text)
        XX(7, 6, 8) = Val(Text60.Text)
        XX(7, 7, 9) = Val(Text61.Text)
        XX(7, 8, 9) = Val(Text62.Text)
        XX(7, 9, 9) = Val(Text63.Text)
        XX(8, 1, 7) = Val(Text64.Text)
        XX(8, 2, 7) = Val(Text65.Text)
        XX(8, 3, 7) = Val(Text66.Text)
        XX(8, 4, 8) = Val(Text67.Text)
        XX(8, 5, 8) = Val(Text68.Text)
        XX(8, 6, 8) = Val(Text69.Text)
        XX(8, 7, 9) = Val(Text70.Text)
        XX(8, 8, 9) = Val(Text71.Text)
        XX(8, 9, 9) = Val(Text72.Text)
        XX(9, 1, 7) = Val(Text73.Text)
        XX(9, 2, 7) = Val(Text74.Text)
        XX(9, 3, 7) = Val(Text75.Text)
        XX(9, 4, 8) = Val(Text76.Text)
        XX(9, 5, 8) = Val(Text77.Text)
        XX(9, 6, 8) = Val(Text78.Text)
        XX(9, 7, 9) = Val(Text79.Text)
        XX(9, 8, 9) = Val(Text80.Text)
        XX(9, 9, 9) = Val(Text81.Text)


    Call jisuan(1, 1)
   
   
        Text1.Text = CStr(XX(1, 1, 1))
        Text2.Text = CStr(XX(1, 2, 1))
        Text3.Text = CStr(XX(1, 3, 1))
        Text4.Text = CStr(XX(1, 4, 2))
        Text5.Text = CStr(XX(1, 5, 2))
        Text6.Text = CStr(XX(1, 6, 2))
        Text7.Text = CStr(XX(1, 7, 3))
        Text8.Text = CStr(XX(1, 8, 3))
        Text9.Text = CStr(XX(1, 9, 3))
        Text10.Text = CStr(XX(2, 1, 1))
        Text11.Text = CStr(XX(2, 2, 1))
        Text12.Text = CStr(XX(2, 3, 1))
        Text13.Text = CStr(XX(2, 4, 2))
        Text14.Text = CStr(XX(2, 5, 2))
        Text15.Text = CStr(XX(2, 6, 2))
        Text16.Text = CStr(XX(2, 7, 3))
        Text17.Text = CStr(XX(2, 8, 3))
        Text18.Text = CStr(XX(2, 9, 3))
        Text19.Text = CStr(XX(3, 1, 1))
        Text20.Text = CStr(XX(3, 2, 1))
        Text21.Text = CStr(XX(3, 3, 1))
        Text22.Text = CStr(XX(3, 4, 2))
        Text23.Text = CStr(XX(3, 5, 2))
        Text24.Text = CStr(XX(3, 6, 2))
        Text25.Text = CStr(XX(3, 7, 3))
        Text26.Text = CStr(XX(3, 8, 3))
        Text27.Text = CStr(XX(3, 9, 3))
        Text28.Text = CStr(XX(4, 1, 4))
        Text29.Text = CStr(XX(4, 2, 4))
        Text30.Text = CStr(XX(4, 3, 4))
        Text31.Text = CStr(XX(4, 4, 5))
        Text32.Text = CStr(XX(4, 5, 5))
        Text33.Text = CStr(XX(4, 6, 5))
        Text34.Text = CStr(XX(4, 7, 6))
        Text35.Text = CStr(XX(4, 8, 6))
        Text36.Text = CStr(XX(4, 9, 6))
        Text37.Text = CStr(XX(5, 1, 4))
        Text38.Text = CStr(XX(5, 2, 4))
        Text39.Text = CStr(XX(5, 3, 4))
        Text40.Text = CStr(XX(5, 4, 5))
        Text41.Text = CStr(XX(5, 5, 5))
        Text42.Text = CStr(XX(5, 6, 5))
        Text43.Text = CStr(XX(5, 7, 6))
        Text44.Text = CStr(XX(5, 8, 6))
        Text45.Text = CStr(XX(5, 9, 6))
        Text46.Text = CStr(XX(6, 1, 4))
        Text47.Text = CStr(XX(6, 2, 4))
        Text48.Text = CStr(XX(6, 3, 4))
        Text49.Text = CStr(XX(6, 4, 5))
        Text50.Text = CStr(XX(6, 5, 5))
        Text51.Text = CStr(XX(6, 6, 5))
        Text52.Text = CStr(XX(6, 7, 6))
        Text53.Text = CStr(XX(6, 8, 6))
        Text54.Text = CStr(XX(6, 9, 6))
        Text55.Text = CStr(XX(7, 1, 7))
        Text56.Text = CStr(XX(7, 2, 7))
        Text57.Text = CStr(XX(7, 3, 7))
        Text58.Text = CStr(XX(7, 4, 8))
        Text59.Text = CStr(XX(7, 5, 8))
        Text60.Text = CStr(XX(7, 6, 8))
        Text61.Text = CStr(XX(7, 7, 9))
        Text62.Text = CStr(XX(7, 8, 9))
        Text63.Text = CStr(XX(7, 9, 9))
        Text64.Text = CStr(XX(8, 1, 7))
        Text65.Text = CStr(XX(8, 2, 7))
        Text66.Text = CStr(XX(8, 3, 7))
        Text67.Text = CStr(XX(8, 4, 8))
        Text68.Text = CStr(XX(8, 5, 8))
        Text69.Text = CStr(XX(8, 6, 8))
        Text70.Text = CStr(XX(8, 7, 9))
        Text71.Text = CStr(XX(8, 8, 9))
        Text72.Text = CStr(XX(8, 9, 9))
        Text73.Text = CStr(XX(9, 1, 7))
        Text74.Text = CStr(XX(9, 2, 7))
        Text75.Text = CStr(XX(9, 3, 7))
        Text76.Text = CStr(XX(9, 4, 8))
        Text77.Text = CStr(XX(9, 5, 8))
        Text78.Text = CStr(XX(9, 6, 8))
        Text79.Text = CStr(XX(9, 7, 9))
        Text80.Text = CStr(XX(9, 8, 9))
        Text81.Text = CStr(XX(9, 9, 9))

End Sub



搜索更多相关主题的帖子: 三维 计算 function 突发奇想 九宫格 
2012-01-13 20:30
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:20 
我用穷举法,试过几次,都失败了。

最后用随机法,倒写成了一个。不过不在这里。

授人于鱼,不如授人于渔
早已停用QQ了
2012-01-14 21:03
快速回复:求人帮忙找错啊~function自循环做穷举法解数独
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023692 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved