逻辑转体解法
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人