自回避随机行走(seleavoid.py)

 1 import random
 2 import sys
 3 import stdio
 4 import stdarray
 5 n = int(sys.argv[1])
 6 trials = int(sys.argv[2])
 7 deadEnds = 0
 8 
 9 for t in range(trials):
10     a = stdarray.create2D(n, n, False)
11     x = n//2
12     y = n//2
13     while (x > 0) and (x < n-1) and (y > 0) and (y < n-1):
14         #Check for dead end and make a random move.
15         a[x][y] = True
16         if a[x+1][y] and a[x][y+1] and a[x-1][y] and a[x][y-1]:
17             deadEnds += 1
18             break
19         r = random.randrange(1, 5)
20         if (r == 1) and (not a[x+1][y]): x += 1
21         if (r == 2) and (not a[x-1][y]): x -= 1
22         if (r == 3) and (not a[x][y+1]): y += 1
23         if (r == 4) and (not a[x][y-1]): y -= 1
24 stdio.writeln(str(100*deadEnds//trials)+'% dead ends')

 

转载于:https://www.cnblogs.com/cdf4745/p/7273558.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值