1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| def jump_node(self, now, pre): dir = [a != b and (a - b)/abs(a-b) or 0 for a, b in zip(now, pre)] print "now = %s, pre = %s, dir = %s" %(now, pre, dir)
if now == self.e_pos: return now
if self.is_pass(now[0], now[1]) is False: return None if dir[0] != 0 and dir[1] != 0: if (self.is_pass(now[0] - dir[0], now[1] + dir[1]) and not self.is_pass(now[0]-dir[0], now[1])) or (self.is_pass(now[0] + dir[0], now[1] - dir[1]) and not self.is_pass(now[0], now[1]-dir[1])): return now else: if dir[0] != 0: ''' * 1 0 0 0 0 0 → 0 0 0 0 * 1 0 0 0 0 ''' print '水平方向:', self.is_pass(now[0] + dir[0], now[1] + 1), self.is_pass(now[0], now[1]+1), self.is_pass(now[0] + dir[0], now[1] - 1), self.is_pass(now[0], now[1]-1) if (self.is_pass(now[0] + dir[0], now[1] + 1) and not self.is_pass(now[0], now[1]+1)) or (self.is_pass(now[0] + dir[0], now[1] - 1) and not self.is_pass(now[0], now[1]-1)): return now else: ''' 0 0 0 1 ↓ 1 0 0 0 ''' print '垂直方向:', self.is_pass(now[0] + 1, now[1] + dir[1]), self.is_pass(now[0] + 1, now[1]), self.is_pass(now[0]-1 , now[1] + dir[1]), self.is_pass(now[0] - 1, now[1]) if (self.is_pass(now[0] + 1, now[1] + dir[1]) and not self.is_pass(now[0]+1, now[1])) or (self.is_pass(now[0] - 1, now[1] + dir[1]) and not self.is_pass(now[0]-1, now[1])): return now
if dir[0] != 0 and dir[1] != 0: t1 = self.jump_node([now[0]+dir[0], now[1]], now) t2 = self.jump_node([now[0], now[1] + dir[1]], now) if t1 or t2: return now if self.is_pass(now[0] + dir[0], now[1]) or self.is_pass(now[0], now[1] + dir[1]): t = self.jump_node([now[0] + dir[0], now[1] + dir[1]], now) if t: return t
return None
|