坛友fishiss的帖子发了算法,但没有gui界面
https://www.52pojie.cn/thread-2001798-1-1.html
于是干了2天给加上了GUI界面,可以直接用鼠标左键点选格子设置起点、终点、空白。
并让通义优化了算法,优化后快了0.5秒左右。
使用DEEPSEEK R1再次优化了代码,速度进一步改善。
行列固定为7行5列,自定义行数涉及格子大小调整、画线坐标调整,不太好弄。
欢迎坛友继续改进优化。
成品已发布到原创工具:
https://www.52pojie.cn/thread-2002242-1-1.html
源码:
优化版:
[Python] 纯文本查看 复制代码"""
界面采用python 标准库 tkinter 来实现
"""
from tkinter import *
def main():
root = Tk() # 创建tkinter窗口
root.title('2025一笔连福辅助工具-可鼠标直接点选 52PJ版') # 设置标题文字
# root.overrideredirect(boolean=True)
root.resizable(width=False, height=False) # 固定宽和高
w = 510
h = 840
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
x = (sw - w) / 6
y = (sh - h) / 2
size = '%dx%d+%d+%d' % (w, h, x, y)
root.geometry(size)
matrix = [
['A1', 'B1', 'C1', 'D1', 'E1'],
['A2', 'B2', 'C2', 'D2', 'E2'],
['A3', 'B3', 'C3', 'D3', 'E3'],
['A4', 'B4', 'C4', 'D4', 'E4'],
['A5', 'B5', 'C5', 'D5', 'E5'],
['A6', 'B6', 'C6', 'D6', 'E6'],
['A7', 'B7', 'C7', 'D7', 'E7']
]
# 设置游戏中每个数据对应色块的颜色
mapcolor = {
"start_bh": "#90EE90",
"end_bh": "#F67C5F",
"blank_bh": "#776e65",
"normal": "#EEE4DA",
}
# 以下设置重新开始按钮
def reset_game():
canvas.delete("all") # 清空之前的内容
# 清空输入框内容""
entry_blank.delete(0, END)
entry_end.delete(0, END)
entry_start.delete(0, END)
update_ui()
selected_points.clear() # 重置 selected_points 列表
def update_ui():
for r in range(len(matrix)):
for c in range(len(matrix[0])):
bh = matrix[r][c]
label = map_labels[r][c]
label['text'] = bh
label['bg'] = "#EEE4DA"
label['foreground'] = "#8f7a66"
def dfs(matrix, start, end, missing):
max_path = []
rows = len(matrix)
cols = len(matrix[0]) if rows > 0 else 0
start_row, start_col, end_row, end_col = None, None, None, None
# 查找起点和终点坐标
for i in range(rows):
for j in range(cols):
if matrix[j] == start:
start_row, start_col = i, j
if matrix[j] == end:
end_row, end_col = i, j
if start_row is None or end_row is None:
print("起点或终点不存在!")
return []
missing_set = set(missing) # 转换为集合提高查询效率
visited = [[False for _ in range(cols)] for _ in range(rows)]
def helper(x, y, current_path):
nonlocal max_path
# 边界检查
if x = rows or y = cols:
return
# 障碍检查及访问状态检查
if matrix[x][y] in missing_set or visited[x][y]:
return
# 加入当前节点并标记访问
current_path.append(matrix[x][y])
visited[x][y] = True
# 找到终点时更新最长路径
if x == end_row and y == end_col:
if len(current_path) > len(max_path):
max_path = list(current_path) # 创建副本保存结果
else:
# 生成四个方向并按曼哈顿距离排序
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
candidates = []
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0
第一版,做对比参考
[Python] 纯文本查看 复制代码"""
一致部分略
"""
def dfs(matrix, start, end, blank):
max_path = []
rows = len(matrix)
cols = len(matrix[0])
start_row = None
start_col = None
end_row = None
end_col = None
# 查找开始位置和结束位置的坐标
for i in range(rows):
for j in range(cols):
if matrix[j] == start:
start_row = i
start_col = j
if matrix[j] == end:
end_row = i
end_col = j
def helper(x, y, path, visited):
nonlocal max_path
if x = rows or y = cols or matrix[x][y] in blank or (x, y) in visited:
return
new_path = path + [matrix[x][y]]
visited.add((x, y))
if matrix[x][y] == end:
if len(new_path) > len(max_path):
max_path = new_path
# 优化:简化递归调用
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
helper(x + dx, y + dy, new_path, visited)
visited.remove((x, y)) # 回溯
helper(start_row, start_col, [], set())
return max_path