[.NET] VB2008 8 Queen Problem的寫法

看板Visual_Basic作者 (阿年:))時間14年前 (2009/12/20 10:42), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/2 (看更多)
請輸入專案類型(網站專案或者應用程式專案): Public Class Form1 Dim chess_board(8, 8) As Boolean Function safe(ByVal chess_board(,) As Boolean, ByVal x As Integer, ByVal y As Integer) As Boolean Dim is_safe As Boolean Dim i, x2, y2, x3, y3, dp, dq As Integer is_safe = True For i = 1 To 8 If chess_board(x, i) Then is_safe = False ElseIf chess_board(i, y) Then is_safe = False End If Next For dp = -7 To 7 x2 = x + dp y2 = y + dp If x2 >= 1 And x2 <= 8 Then If y2 >= 1 And y2 <= 8 Then If chess_board(x2, y2) Then is_safe = False End If End If End If Next For dq = -7 To 7 x3 = x + dq y3 = y - dq If x3 >= 1 And x3 <= 8 Then If y3 >= 1 And y3 <= 8 Then If chess_board(x3, y3) Then is_safe = False End If End If End If Next Return is_safe End Function Sub draw_chess_board(ByVal chess_board) Dim g = Me.CreateGraphics() Dim i, x, y As Integer For i = 1 To 9 g.DrawLine(Pens.Black, i * 15, 15, i * 15, 135) g.DrawLine(Pens.Black, 15, i * 15, 135, i * 15) Next For x = 1 To 8 For y = 1 To 8 If chess_board(x, y) Then g.DrawEllipse(Pens.Red, x * 15 + 4, y * 15 + 4, 7, 7) End If Next Next End Sub Sub PlaceAQueen(ByVal chess_board(,) As Boolean, ByVal x As Integer) For x = 1 To 8 For y = 1 To 8 If safe(chess_board, x, y) = True Then chess_board(x, y) = True Else chess_board(x, y) = False End If If chess_board(8, y) Then draw_chess_board(chess_board) MsgBox("我找到一組解了,厲害吧!:)") Else PlaceAQueen(chess_board, x + 1) End If Next Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click PlaceAQueen(chess_board, 1) End Sub End Class 我不知道錯在哪, 可是執行的時候, 在Function第一行會出現"An unhandled exception of type 'System.StackOverflowException' occurred in 程式設計091222.exe", 請幫我找一下bug吧!(感激 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.127.32.231 ※ 編輯: ruby791104 來自: 120.127.32.231 (12/20 10:43)

12/20 17:12, , 1F
阿不是就跟你講很清楚了 錯在Stack Overflow
12/20 17:12, 1F

12/20 17:47, , 2F
簡單的說就是溢位啦~~無限迴圈導致記憶體被用光~~
12/20 17:47, 2F

12/20 20:10, , 3F
這個時候就會想到那個神燈的笑話
12/20 20:10, 3F
文章代碼(AID): #1BBOyFji (Visual_Basic)
文章代碼(AID): #1BBOyFji (Visual_Basic)