1const goal = [1,2,...,15,0]; // 目标布局
2const dist = manhattanTable(); // 曼哈顿距离表
3let bound = h(start); // 初始阈值 = 曼哈顿距离
4function ida(state, g, bound, prev) { // 迭代加深 A*
5 const f = g + h(state); // 已走步数 + 启发
6 if (f > bound) return f; // 超出阈值 → 剪枝
7 if (state === goal) return FOUND; // 找到最优解
8 for (const dir of 上下左右) {
9 if (dir 是 prev 的反方向) continue; // 不回退
10 const ns = move(state, dir); // 空格沿 dir 滑动
11 const res = ida(ns, g + 1, bound, dir);
12 if (res === FOUND) return FOUND;
13 }
14 return 最小的新阈值; // 提高阈值继续
15}