91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 5KB
    文件類型: .py
    金幣: 2
    下載: 1 次
    發布日期: 2021-06-17
  • 語言: Python
  • 標簽: 源碼??python??

資源簡介

騎士周游問題,采用多種方法解決騎士周游問題,如有其它需要,請留言

資源截圖

代碼片段和文件信息

#騎士周游問題
#遞歸方法,這種方法,我電腦的配置計算6*6的棋盤就十分困難
def?knight_1(x?y?n):
????move_pace?=?{(21)?(2-1)?(12)?(1-2)?(-21)?(-2-1)?(-12)?(-1-2)}
????solutions?=?[]
????#下面這個函數獲取當前位置所有可能下一步,并返回所有新位置構成的列表
????def?posible_position(pos?state):#state?中存儲已經走過的位置
????????posible_pos?=?[]
????????for?pace?in?move_pace:
????????????new_pos?=?(pos[0]+pace[0]?pos[1]+pace[1])
????????????if?new_pos?not?in?state?and?0?<=new_pos[0]?????????????????posible_pos.append(new_pos)
????????return?posible_pos
????#遞歸函數
????def?knight(state?=?((xy))):
????????if?len(state)?==?n*n:
????????????#print(state)
????????????solutions.append(list(state))
????????????return?True
????????else:
????????????for?pos?in?posible_position(state[-1]?state):
????????????????knight(state?+?(pos))
????knight()
????return?solutions
#這個方法也不好
#只能運行6格棋盤
#非遞歸方法
def?knight_2(x?y?n):
????move_pace?=?[(21)?(2-1)?(12)?(1-2)?(-21)?(-2-1)?(-12)?(-1-2)]
????st?=?ESStack()
????state?=?((x?y))
????st.push((state0))
????def?posible_pace(pace?state):
????????new_pos?=?(state[-1][0]+pace[0]?state[-1][1]+pace[1])
????????if?new_pos?not?in?state?and?0?<=new_pos[0]?????????????return?new_pos
????????return?False
????while?not?st.is_empty():
????????state?pace_index?=?st.pop()
????????if?len(state)?==?n*n:
????????????print(“find“)
????????????return?state
????????for?pace?in?range(pace_index?8):
????????????new_pos?=?posible_pace(move_pace[pace]?state)
????????????if?new_pos:
????????????????st.push((state?pace+1))
????????????????st.push((state?+?(new_pos)?0))
????????????????#print(st.top())
????????????????break
#這個稍微優化一點,換湯不換藥,只是避免了每次都把走過的路徑亞入棧中
#仍然只能計算到6
def?knight_3(x?y?n):
????move_pace?=?[(21)?(2-1)?(12)?(1-2)?(-21)?(-2-1)?(-12)?(-1-2)]
????st?=?ESStack()
????state?=?[(x?y)]
????st.push(((xy)0))
????count?=?0
????def?posible_pace(pace?state):
????????new_pos?=?(state[-1][0]+pace[0]?state[-1][1]+pace[1])
????????if?new_pos?not?in?state?and?0?<=new_pos[0]?????????????return?new_pos
????????return?False
????while?not?st.is_empty():
????????count?+=?1
????????pos?pace_index?=?st.pop()
????????#print(state)
????????if?len(state)?==?n*n:
????????????prin

評論

共有 條評論