八皇后演算法

2021-06-06 13:42:21 字數 3067 閱讀 3644

imports system.math

public class eightqueen

dim topnum as int16 = 7

dim printb as boolean

dim j2 as int16 = 0

dim eightqueenplace2((topnum * 2) * (topnum * 2), topnum, topnum) as int16

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

dim eightqueenplace1(topnum, topnum) as int16

for i = 0 to topnum

for j = 0 to topnum

eightqueenplace1(i, j) = 1  '初始化

next

next

bu(eightqueenplace1, 0, 0, 0)

textbox1.text = textbox1.text + chr(13) + chr(10) + j2.tostring + "end"   '輸出總種數和和結束標誌

end sub

private function check(byref eightqueenplace1(,) as int16, byval ii as int16, byref jj as int16)

dim x, y as int16

for i = 0 to topnum

for j = 0 to topnum

if eightqueenplace1(i, j) = 2 then

x = 0

y = 0

x = i - ii

y = j - jj

if abs(x) = abs(y) or abs(x) = 0 or abs(y) = 0 then

return 1

exit for

end if

end if

next

next

return 2

end function

private sub print(byref eightqueenplace1(,) as int16)           '輸出符合條件的排列

for ii = 0 to topnum

for jj = 0 to topnum

textbox1.text = textbox1.text + "  " + eightqueenplace1(ii, jj).tostring

next

textbox1.text = textbox1.text + chr(13) + chr(10)

next

textbox1.text = textbox1.text + chr(13) + chr(10)

end sub

private sub bu(byval eightqueenplace1(,) as int16, byval ii as int16, byref jj as int16, byval numm as int16)   '遞迴

dim eightqueenplace(topnum, topnum) as int16

dim i as int16

dim j1 as int16

j1 = jj

for i = 0 to topnum

for j = 0 to topnum

eightqueenplace(i, j) = eightqueenplace1(i, j)

next

next

for i = ii to topnum

for j = j1 to topnum

if check(eightqueenplace, i, j) = 2 then

if  j<>topnum then

eightqueenplace(i, j) = 1

bu(eightqueenplace, i, j + 1, numm)    '能放但不放的分支

end if

numm = numm + 1

eightqueenplace(i, j) = 2

bu(eightqueenplace, i, j + 1, numm)   '能放就放的分支

end if

next

j1 = 0

next

if i = topnum + 1 and numm > 7 and jj <> numm + 1 then   '判斷是否符合條件

if j2 > (topnum * 2) * (topnum * 2) then exit sub

for j = 0 to (topnum * 2) * (topnum * 2)                               '判斷是否已經輸出過

printb = true

for a1 = 0 to topnum

for a2 = 0 to topnum

if eightqueenplace2(j, a1, a2) <> eightqueenplace(a1, a2) then

goto gggg

end if

next

next

printb = false

exit for

gggg:

next

if printb = true then                             '沒有輸出過,記錄下來

for a1 = 0 to topnum

for a2 = 0 to topnum

eightqueenplace2(j2, a1, a2) = eightqueenplace(a1, a2)

next

next

call print(eightqueenplace)

j2 = j2 + 1

end if

end if

end sub

end class

八皇后演算法

1.回溯法 回溯法,又被稱為 試探法 解決問題時,每進行一步,都是抱著試試 或者這麼走下去肯定達不到目標,立刻做回退操作重新選擇。這種走不通 就回退再走的方法就是回溯法。問題 列舉集合 中所有子集的問題中 使用回溯法。從集合的開頭元素開始,對每個元素都有兩直到集合最後乙個元素。其中的每個操作都可以看...

回溯演算法 八皇后

總時間限制 1000ms 記憶體限制 65536kb 描述 會下西洋棋的人都很清楚 皇后可以在橫 豎 斜線上不限步數地吃掉其他棋子。如何將8個皇后放在棋盤上 有8 8個方格 使它們誰也不能被吃掉!這就是著名的八皇后問題。對於某個滿足要求的8皇后的擺放方法,定義乙個皇后串a與之對應,即a b 1b2....

回溯演算法 八皇后

今天學習了下回溯演算法,順便看了下經典案例 八皇后問題。該問題是國際西洋棋棋手馬克斯 貝瑟爾於1848年提出 在8 8格的西洋棋上擺放八個皇后,使其不能互相攻擊,即任意兩個皇后都不能處於同一行 同一列或同一斜線上,問有多少種擺法。回溯演算法的搜尋邏輯是深度優先,即,從一條路往前走,能進則進,不能進則...