逻辑转体解法
from py2neo import Graph, Node, Relationship graphdb = Graph(host='localhost', http_port=7978, user='neo4j', password='123456') # 检查当前步是否有效 def checkEnable(p): count = 0 for i in range(0, 4): if p[i] > 56: return False if p[i] < 0: return False if p[i] == 13 or p[i] == 43: return False if p[i] == 0: count = count + 1 # 不可能是单个坐标是0 if count % 2 > 0: return False return True # 获取可以移动的位置 def move(x): paths = [] left = None right = None # 平躺 if x[1] > 0 and x[3] > 0: up = (x[0] + 14, x[1] + 14, 0, 0, "上") down = (x[0] - 7, x[1] - 7, 0, 0, "下") if x[1] / 7 != 0: left = (x[0] + 2, 0, x[2] + 2, 0, "左") if (x[0] - 1) / 7 != 0: right = (x[0] - 1, 0, x[2] - 1, 0, "右") # 立起时 else: # 立Y if x[1] == 0 and x[3] == 0: up = (x[0] + 14, 0, x[2] + 14, 0, "上") down = (x[0] - 14, 0, x[2] - 14, 0, "下") if x[0] % 7 < 6: left = (x[0] + 1, x[0] + 2, x[2] + 1, x[2] + 2, "左") if x[2] % 7 > 2 or x[2] % 7 == 0: right = (x[0] - 2, x[0] - 1, x[2] - 2, x[2] - 1, "右") # 立X else: up = (x[0] + 7, x[1] + 7, x[0] + 14, x[1] + 14, "上") down = (x[0] - 14, x[1] - 14, x[0] - 7, x[1] - 7, "下") if x[0] % 7 < 5: left = (x[0] + 2, x[1] + 2, 0, 0, "左") if x[1] % 7 > 2 or x[1] % 7 == 0: right = (x[0] - 2, x[1] - 2, 0, 0, "右") if checkEnable(up): paths.append(up) if checkEnable(down): paths.append(down) if left and checkEnable(left): paths.append(left) if right and checkEnable(right): paths.append(right) return paths def nodeHash(node): if node == None: return ""; key = [] for i in range(0, 4): key.append(str(node[i])) return "#".join(key) def buildMap(node, path): dic = {} key = nodeHash(node) dic[key] = path return dic; def buildGraph(): graphdb.delete_all() # 平躺 x = (1, 2, 8, 9) for i in range(0, 6): sx = (x[0] + i, x[1] + i, x[2] + i, x[3] + i) for j in range(0, 7): sy = (sx[0] + j * 7, sx[1] + j * 7, sx[2] + j * 7, sx[3] + j * 7) # print(sy) if checkEnable(sy): path = move(sy) # map = buildMap(sy, path) # print(map) writedb(sy, path) # x站立 x = (1, 2, 0, 0) for i in range(0, 6): sx = (x[0] + i, x[1] + i, 0, 0) for j in range(0, 8): sy = (sx[0] + j * 7, sx[1] + j * 7, 0, 0) if checkEnable(sy): path = move(sy) # map = buildMap(sy, path) # print(map) writedb(sy, path) # y站立 x = (1, 0, 8, 0) for i in range(0, 7): sx = (x[0] + i, 0, x[2] + i, 0) for j in range(0, 7): sy = (sx[0] + j * 7, 0, sx[2] + j * 7, 0) if checkEnable(sy): path = move(sy) # map = buildMap(sy, path) # print(map) writedb(sy, path) def writedb(node, path): a = Node("mg", name=nodeHash(node)) graphdb.merge(a, "mg", "name") for p in path: b = Node("mg", name=nodeHash(p)) relationship = Relationship(a, p[4], b) relationship['date'] = p[4] graphdb.merge(b, "mg", "name") graphdb.create(relationship) if __name__ == '__main__': # buildGraph() data = graphdb.run(''' MATCH (p1:mg{name:"2#3#9#10"}),(p2:mg{name:"34#35#41#42"}), p=shortestpath((p1)-[*..10]->(p2)) RETURN p ''').data() for record in data: for k in record: a = record[k] for t in a: for t1 in t.values(): print(t1, end="->")
ps:所有的1、2、3、4阶题目均已求解完成,全套题库已生成,欢迎大家来交流,在此强烈鄙视那些采集博客的网站
原创地址:https://www.cnblogs.com/wujf/p/13390707.html