程序执行时间 0.05S
from Stack import Stack # 建设你已经定义好一个栈类
import datetime
import operator
'''
优化算法思路:每一步都选择重复概率小的方向前进,即选择的方向可达到的棋盘格子数量最少,
'''
def passable(next_loct,move_road):
i = next_loct[0]
j = next_loct[1]
# print ("当前经过的路线为{0}".format(move_road))
if i >= 0 and i <= 7 and j >= 0 and j <= 7 and (not (next_loct in move_road)):
# 如果下一步移动位置位于棋盘内且未经过,返回True
return True
return False
def get_nextgrid(loct, move_road, horse_dirc):
# 记录当前格子可用的下一位置,并分别记录下一位置的可达性,按照可达性由易到难的顺序排序
temp_dict = {}
# 记录排序后的下一可用位置
sorted_next = []
for n in range(8):
i = loct[0] + horse_dirc[n][0]
j = loct[1] + horse_dirc[n][1]
index = (i,j)
if passable(index ,move_road):
count = 0
for k in range(8):