{:7_113:}
学习
看看
请教一下你这个除了输入0,0有输出之外其他的好像都没输出
看看
HAHH
n = 5 # 8太慢了,改为5
p = [(-2,1),(-1,2),(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1)] # 状态空间,8个方向
entry = (2,2) # 出发地
x = *(n*n) # 一个解,长度固定64,形如[(2,2),(4,3),...]
X = [] # 一组解
# 冲突检测
def conflict(k):
global n,p, x, X
# 步子 x 超出边界
if x < 0 or x >= n or x < 0 or x >= n:
return True
# 步子 x 已经走过
if x in x[:k]:
return True
return False # 无冲突
# 回溯法(递归版本)
def subsets(k): # 到达第k个元素
global n, p, x, X
if k == n*n:# 超出最尾的元素
print(x)
#X.append(x[:]) # 保存(一个解)
else:
for i in p: # 遍历元素 x 的状态空间: 8个方向
x = (x + i, x + i)
if not conflict(k): # 剪枝
subsets(k+1)
# 测试
x = entry # 入口
subsets(1) # 开始走第k=1步
i love fishc
参考一下谢谢
咦
学习一下
从任意位置出发,不重复地走完所有64个格子.py求源码
观摩学习
想看
{:10_254:}
学习学习
想学习
good!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{:5_102:}
楼主我按小甲鱼c的思路用python写了代码,但找不出哪里错了,能帮忙看下吗
global X
global Y
X=8
Y=8
global board
board=[ for i in range(Y)]
def nextxy(temp,count):
if count==0:
if (temp-1>=0 and temp-2>=0 and board-1]-2]==0):
temp=temp-1
temp=temp-2
return 1
else:
return 0
if count==1:
if (temp+1<=X-1 and temp-2>=0 and board+1]-2]==0):
temp=temp+1
temp=temp-2
return 1
else:
return 0
if count==2:
if (temp+2<=X-1 and temp-1>=0 and board+2]-1]==0):
temp=temp+2
temp=temp-1
return 1
else:
return 0
if count==3:
if (temp+2<=X-1 and temp+1<=Y-1 and board+2]+1]==0):
temp=temp+2
temp=temp+1
return 1
else:
return 0
if count==4:
if (temp+1<=X-1 and temp+2<=Y-1 and board+1]+2]==0):
temp=temp+1
temp=temp+2
return 1
else:
return 0
if count==5:
if (temp-1>=0 and temp+2<=Y-1 and board-1]+2]==0):
temp=temp-1
temp=temp+2
return 1
else:
return 0
if count==6:
if (temp-2>=0 and temp+1<=Y-1 and board-2]+1]==0):
temp=temp-2
temp=temp+1
return 1
else:
return 0
if count==7:
if (temp-2>=0 and temp-1>=0 and board-2]-1]==0):
temp=temp-2
temp=temp-1
return 1
else:
return 0
return 0
def TravelChessBoard(x,y,tag):
count=0
board=tag
if tag==X*Y:
return 1
temp=
flag=nextxy(temp,count)
while flag==0 and count<X-1:
count+=1
flag=nextxy(temp,count)
while flag==1:
x1,y1=temp,temp
if TravelChessBoard(x1,y1,tag+1):
return 1
count+=1
temp=
flag=nextxy(temp,count)
while flag==0 and count<X-1:
count+=1
flag=nextxy(temp,count)
if flag==0:
board=0
return 0
TravelChessBoard(2,0,1)