python发送文件给其他人如何打开_如何将python列表动态传递给另一个python文件

I am developing an N-Queen Simulation using pygame.

class NQ:

def __init__(self,n):

self.size = n

self.columns = [] * self.size

self.places = 0

self.backtracks = 0

def place(self, startRow=0):

if len(self.columns) == self.size:

return self.columns

else:

for row in range(startRow, self.size):

if self.isSafe(len(self.columns), row) is True:

self.columns.append(row)

self.places += 1

return self.place()

else:

lastRow = self.columns.pop()

self.backtracks += 1

return self.place(startRow=lastRow + 1)

def isSafe(self, col, row):

for threatRow in self.columns:

threatCol = self.columns.index(threatRow)

if row == threatRow or col == self.columns.index(threatRow):

return False

elif threatRow + threatCol == row + col or threatRow - threatCol == row - col:

return False

return True

def process(n):

nqueens = NQ(n)

nqueens.place(0)

return nqueens.columns

Also I have a pygame procedure in another file to draw chess board which takes a list as input and place them accordingly.

If I want to show the movement of queens, how can i pass the list dynamically from the recursivecode procedure so that the exact backtracking procedure is visible.

Thank you

解决方案

If you want to know what is going on inside recursive function you can add external function as argument and than you can use it inside recursion to print current state of algorythm or draw queens on chess board.

In example I use show_colums() to print self.columns every time self.place() is running.

file: nq.py

class NQ:

def __init__(self,n, callback): # added callback

self.size = n

self.columns = []

self.places = 0

self.backtracks = 0

self.callback = callback # added callback

def place(self, startRow=0):

self.callback(self.columns) # added callback

if len(self.columns) == self.size:

return self.columns

else:

for row in range(startRow, self.size):

if self.isSafe(len(self.columns), row) is True:

self.columns.append(row)

self.places += 1

return self.place()

else:

lastRow = self.columns.pop()

self.backtracks += 1

return self.place(startRow=lastRow + 1)

def isSafe(self, col, row):

for threatRow in self.columns:

threatCol = self.columns.index(threatRow)

if row == threatRow or col == self.columns.index(threatRow):

return False

elif threatRow + threatCol == row + col or threatRow - threatCol == row - col:

return False

return True

file: main.py

from nq inport NQ

def show_columns(x):

print "columns:", x

def process(n):

nqueens = NQ(n, show_columns)

nqueens.place(0)

return nqueens.columns

process(8)

part of result

columns: []

columns: [0]

columns: [0, 2]

columns: [0, 2, 4]

columns: [0, 2, 4, 1]

columns: [0, 2, 4, 1, 3]

columns: [0, 2, 4, 1]

columns: [0, 2, 4, 1, 7]

columns: [0, 2, 4, 1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值