lineUsed=[-1,-1,-1,-1,-1,-1,-1,-1]#lineUsed[i]=-1 means line i is empty,lineUsed[i]=j means line i is used by a Queen who is right in row jrowUsed=[-1,-1,-1,-1,-1,-1,-1,-1] #rowUsed[i]=-1 means row i is empty,rowUsed[i]=j means row i is used by a Queen who is right in line jboxUsed=[]diagonal_1_used=[]diagonal_2_used=[]def Test(boxList): lineUsed2=[-1,-1,-1,-1,-1,-1,-1,-1] rowUsed2=[-1,-1,-1,-1,-1,-1,-1,-1] diagonal_1_used2=[] diagonal_2_used2=[] for m in range(0,len(boxList)): lineUsed2[boxList[m]//8]=boxList[m]%8 rowUsed2[boxList[m]%8]=boxList[m]//8 diagonal_1_used2.append((boxList[m]%8)+(boxList[m]//8)) diagonal_2_used2.append((boxList[m]//8)-(boxList[m]%8)) for n in range(0,64): if lineUsed2[n//8]==-1 and rowUsed2[n%8]==-1 and (not (n//8)+(n%8) in diagonal_1_used2) and (not (n//8)-(n%8) in diagonal_2_used2): return False return True def myPrint(boxList): for i in range(0,len(boxList)): L_N=boxList[i]//8 R_N=boxList[i]%8 print L_N,'-',R_N,' ', print ''count=0def Try(boxNum): global count if boxNum<64: for set in [True,False]: lineNum=boxNum//8 rowNum=boxNum%8 if (lineUsed[lineNum]==-1) and (rowUsed[rowNum]==-1) and (not lineNum+rowNum in diagonal_1_used) and (not lineNum-rowNum in diagonal_2_used) and (not boxNum in boxUsed) and set: boxUsed.append(boxNum) lineUsed[lineNum]=rowNum rowUsed[rowNum]=lineNum diagonal_1_used.append(lineNum+rowNum) diagonal_2_used.append(lineNum-rowNum) if len(boxUsed)<5: Try(boxNum+8-rowNum) if len(boxUsed)==5: if Test(boxUsed): count+=1 print count,' ' myPrint(boxUsed) boxUsed.pop() lineUsed[lineNum]=-1 rowUsed[rowNum]=-1 diagonal_1_used.pop() diagonal_2_used.pop() if set==False: Try(boxNum+1)Try(0)input()
引用第2楼fengtao57于2007-01-09 14:32发表的:程序不是重要的主要是算法你直接写算法流程就行了嘛
引用第4楼战无敌平琼宇于2007-01-13 11:31发表的:楼主强啊,用的什么语言哦
引用第5楼richardxx于2007-01-14 00:01发表的:棋盘多项式直接数学解出。