五一假日闲来无事,学习了下Python,用马的遍历算法作为这几天学习的总结:

上述代码在A=8时,即8*8的棋盘大小,从(0,0)开始时count = 16501401费时359.331413 秒(Python2.51)
在IronPython 2 a中运行是300秒,速度稍快一点。
马的遍历(Python版)#coding=utf-8
马的遍历(Python版)#
马的遍历算法
马的遍历(Python版)
import time, sys
马的遍历(Python版)
马的遍历(Python版)
class hourse:
马的遍历(Python版)    
def __init__(self, A = 6):
马的遍历(Python版)        self.zf 
= [[1,2],[2,1],[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2]]
马的遍历(Python版)        self.A 
= A
马的遍历(Python版)                                            
#zf为马的八种走法
马的遍历(Python版)
        print 'A = %d' % self.A
马的遍历(Python版)        self.X 
= [[0   for   a   in   range(self.A)]   for   b   in   range(self.A)]
马的遍历(Python版)        self.finish 
= [[0,0,-1]]                         #已完成的,这是入口
马的遍历(Python版)
        self.X[0][0] = 1
马的遍历(Python版)                                            
马的遍历(Python版)    
def next(self, cur_zf):
马的遍历(Python版)        
if cur_zf[2>= 7 :
马的遍历(Python版)            
return  None 
马的遍历(Python版)        cur_zf[
2= cur_zf[2+ 1               #下一个可行的路线
马的遍历(Python版)
        m = cur_zf[0] + self.zf[cur_zf[2]][0]        #下一个可行路线的m坐标值
马的遍历(Python版)
        n = cur_zf[1+ self.zf[cur_zf[2]][1]        #下一个可行路线的n坐标值
马的遍历(Python版)
        if m < 0 or m >= self.A or n < 0 or n >= self.A or self.X[m][n] != 0:
马的遍历(Python版)            
return self.next(cur_zf)                 #如果超出了棋盘的大小,则取下一个走法 
马的遍历(Python版)
        else:
马的遍历(Python版)            
#print [m, n, -1]
马的遍历(Python版)
            return [m, n, -1]
马的遍历(Python版)
马的遍历(Python版)    
def run(self):
马的遍历(Python版)        start 
= time.clock()
马的遍历(Python版)        count 
= 0
马的遍历(Python版)        
while True:
马的遍历(Python版)            count 
= count + 1
马的遍历(Python版)            last 
= self.finish[-1]
马的遍历(Python版)            node 
= self.next(last)
马的遍历(Python版)            
if node == None:
马的遍历(Python版)                self.X[last[0]][last[
1]] = 0              #未使用
马的遍历(Python版)
                self.finish.pop()
马的遍历(Python版)                
#print 'pop',last
马的遍历(Python版)
                if len(self.finish) <= 0:
马的遍历(Python版)                    
print 'over'
马的遍历(Python版)                    
break
马的遍历(Python版)            
else:
马的遍历(Python版)                self.X[node[0]][node[
1]] = 1              #已使用
马的遍历(Python版)
                self.finish.append(node)
马的遍历(Python版)                
#print 'append',node
马的遍历(Python版)
                if len(self.finish) == self.A * self.A:
马的遍历(Python版)                    
print 'ok'
马的遍历(Python版)                    
print self.finish
马的遍历(Python版)                    
break
马的遍历(Python版)
马的遍历(Python版)        
print 'count = %d' % count
马的遍历(Python版)        
print '费时%f秒' % (time.clock() - start)
马的遍历(Python版)
马的遍历(Python版)
马的遍历(Python版)
if __name__ == '__main__':
马的遍历(Python版)    
if len(sys.argv) == 2:
马的遍历(Python版)        x 
= hourse(int(sys.argv[1]))
马的遍历(Python版)    
else:
马的遍历(Python版)        x 
= hourse()
马的遍历(Python版)    x.run()
马的遍历(Python版)    
马的遍历(Python版)    

另:在本代码中,为了定义一个初始为0的二维数组,很花了点时间,下一节再说。

相关文章:

  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-08-29
  • 2021-09-16
  • 2022-02-28
  • 2022-12-23
猜你喜欢
  • 2021-05-31
  • 2021-09-19
  • 2022-12-23
  • 2021-04-17
相关资源
相似解决方案