import itertools nums = [i for i in range(9)] deng=[(1,1),(2,5),(3,3),(4,7),(5,9),(6,6),(7,8),(8,2),(9,4)] def bianli(i,j): i_=i temp=i j_=j while(i_<8): if matrix[i_+1][j]==1: break else: matrix[i_+1][j]=1 i_+=1 while (i > 0): if matrix[i -1][j] == 1: break else: matrix[i -1][j] = 1 i -= 1 while (j_ < 8): if matrix[temp][j_+1] == 1: break else: matrix[temp][j_+1] = 1 j_ += 1 while (j > 0): if matrix[temp][j-1] == 1: break else: matrix[temp][j-1] = 1 j -= 1 def isfull(): for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]==0: return False return True for num in itertools.permutations(nums): matrix = [[0 for i in range(9)] for i in range(9)] for i in num: matrix[deng[i][0]-1][deng[i][1]-1]=1 bianli(deng[i][0]-1,deng[i][1]-1) if isfull(): for i in num: print(i+1,end=",") break