我试图使用这些启发式的A *搜索来解决8-Puzzle: - h1:错位瓦片的数量 - h2:总曼哈顿距离 - h3:以上的总和

移动的图块称为0 .

我的目标是解决这些问题:

4 1 2

5 8 3

7 0 6

8 6 7

2 5 4

3 0 1

我遇到的问题是,通过我目前的A *实现,它能够解决第一个问题,但不能解决第二个问题 .

所以请帮助我理解我的A *代码有什么问题:

int [,] current =从控制台输入为字符串(412583706)并转换为表示拼图的2D int . 正确相同,其中0位于右下角 .

var openList = new List { current };

var closedList = new List();

while (openList.Count > 0)

{

steps++;

current = GetBestNodeFromList(correct, dimensions, openList, useHeuristic);

// "GetBestNodeFromList()" finds the cheapest node in the openList.

// cheapest node: lowest value of h3.

openList.Remove(current);

h1 = getHeuristic1b(current, correct, dimensions);

h2 = getHeuristic2b(current, correct, dimensions);

h3 = h1 + h2;

if (h1 == 0 && h2 == 0) { break; }

openList = Puzzle_PossibleNext(current, closedList);

// the method "PossibleNext()" finds possible next moves from the current

// position. if the next move exists in the closedList, it is discarded.

// Drawing the puzzle and showing heuristics.

DrawCurrentState(h1, h2, h3, current, steps);

// adding last visited position to the closedlist.

closedList.Add(current);

}

第一个问题通过7个步骤解决 . 根据我测试的不同程序,下一个问题可以通过32个步骤解决 .

我的程序与另一个程序的不同之处在于前4个步骤是相同的,然后另一个程序选择不同的路径,而我的程序一直在继续,无法找到解决方案 . 看起来我的程序确实选择了最便宜的节点,所以这就是为什么我无法理解什么是错的 .

这是我第一次使用寻路算法,所以我想解决它 . 我已经有这个问题3天,我觉得我已经尝试了很多解决方案,但没有一个工作T_T

最好的祝福 .

----编辑-----附加代码:

// Put heuristic value from all in list, then return list item with lowest h-value.

static int[,] GetBestNodeFromList(int[,] correct, int d, List list, string useHeuristic)

{

int[,] n = new int[d,d];

if (list.Count > 0)

{

List heuristicsValueList = new List();

for (int i = 0; i < list.Count; i++)

{

if (useHeuristic == "h1") { heuristicsValueList.Add(getHeuristic1b(list[i], correct, d)); }

else if (useHeuristic == "h2") { heuristicsValueList.Add(getHeuristic2b(list[i], correct, d)); }

else { heuristicsValueList.Add(getHeuristic3(list[i], correct, d)); }

}

n = list[heuristicsValueList.IndexOf(heuristicsValueList.Min())];

}

return n;

}

---------编辑2 --------改变了我的代码,但仍然没有运气拼图设置/节点及其启发式都在PuzzleNode对象中 .

//从当前节点返回下一个可能移动的列表 . //不包括在closedNodeList中找到的移动 .

static List Puzzle_GenerateNextNodes(PuzzleNode node, List closedNodeList)

{

List nextList = new List();

Point isNow = new Point(0, 0);

// 1) Find where [0] is.

int dimensions = (int)Math.Sqrt((double)node.n.Length);

for (int x = 0; x < dimensions; x++) {

for (int y = 0; y < dimensions; y++) { if (node.n[x, y] == 0) { isNow.X = y; isNow.Y = x; break; } }

}

// 2) Check possible moves.

bool moveUp = false, moveDown = false, moveLeft = false, moveRight = false;

if (isNow.X == 0)

{

moveRight = true;

if (isNow.Y == 0) { moveDown = true; }

else if (isNow.Y == 1) { moveUp = true; moveDown = true; }

else if (isNow.Y == 2) { moveUp = true; }

}

else if (isNow.X == 1)

{

moveRight = true;

moveLeft = true;

if (isNow.Y == 0) { moveDown = true; }

else if (isNow.Y == 1) { moveUp = true; moveDown = true; }

else if (isNow.Y == 2) { moveUp = true; }

}

else if (isNow.X == 2)

{

moveLeft = true;

if (isNow.Y == 0) { moveDown = true; }

else if (isNow.Y == 1) { moveUp = true; moveDown = true; }

else if (isNow.Y == 2) { moveUp = true; }

}

// 3) Create list of possible moves.

// Add moved puzzle node to list over next moves

if (moveRight)

{

int[,] right = new int[dimensions, dimensions];

Array.Copy(node.n, right, node.n.Length);

PuzzleNode tmp = new PuzzleNode( PuzzleMoveRight(right, isNow.X, isNow.Y) );

if (!ListHasThisValue(tmp.n, closedNodeList, dimensions)) { nextList.Add(tmp); }

}

// moveleft, up, down, same structure as moveRight

if (moveLeft)

{

..

}

if (moveUp)

{

..

}

if (moveDown)

{

..

}

return nextList;

}

-----------编辑3 ----------------

顺便说一句,我想问一下,如果我对A *的不同步骤的实现得到了正确的理解 . 目前,我的程序的A *搜索执行此操作:

创建初始列表OPEN和CLOSED,将起始节点添加到OPEN

启动循环,从OPEN中删除最便宜的节点,将其添加到CLOSED *最便宜的节点由其曼哈顿距离值确定 .

使用节点查找邻居/子节点/下一步移动,将这些节点添加到SUCCESSOR列表中 .

探索SUCCESSOR列表,检查其中是否包含目标状态,否则添加到OPEN列表

重复2-4,探索列表中的节点 .

当我用Q1尝试这些步骤时,我得到7个步骤的解决方案,这是正确的 . 这也可以手工找到 . 但是对于Q2,它一直持续到OPEN列表为空,没有其他东西可以探索 . 那我错过了什么?

8 puzzle java_我的A *搜索8-Puzzle有什么问题?相关推荐

  1. Programming Assignment 4: 8 puzzle

    8 puzzle 使用A*搜索算法解决8-puzzle问题. Board.java Board类用来表示一个 n∗n n ∗ n n*n的网格,其中有 n2−1 n 2 − 1 n^2-1个方块,每个 ...

  2. leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算)

    题目 https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/ 题解 看了答案,堪称力扣最详细的答案,从时间复杂度的角度 ...

  3. 【UVA - 227】Puzzle (模拟,水题)

    题干: Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contain ...

  4. 【Puzzle】基于 Vue 和 Webpack4 的可插拔式微前端架构

    基于 Vue 和 Webpack4 的可插拔式微前端架构 - Puzzle 演示环境:PuzzleDemo 什么是 Puzzle Puzzle 是基于 Vue 和 Webpack4 实现的一种项目结构 ...

  5. 猜字谜 外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧。 字谜的迷面 puzzle 按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:

    今天是元宵节,力扣也细心地为我们准备了一道有关节日的题,一起看看吧: 1178. 猜字谜 外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧. 字谜的迷面 puzzle 按字符串形式给出 ...

  6. Solve Slide Puzzle with Hill Climbing Search Algorithm

    Hill climbing search algorithm is one of the simplest algorithm which falls under local search and o ...

  7. html puzzle游戏,Blockly培训案例-puzzle游戏的制作(一)

    去年的两期培训,发现老师们对blockly-games兴趣较高,而且参训老师在返校之后,很多都使用blockly-games进行了教学尝试.而设计这个案例的原因一是老师们对blockly-games较 ...

  8. Swift 进阶 | 看得见的算法

    GitHub Repo:coderZsq.target.swift Follow: coderZsq · GitHub Resume: coderzsq.github.io/coderZsq.we- ...

  9. WaWa的奇妙冒险(第一周集训自闭现场)

    第一周周记 (一)例题记录 A-Download Manager (水题) HDU - 3233 Input Output Sample Input Sample Output 理解 AC代码 B-J ...

最新文章

  1. 如何在CPU上优化GEMM(下)
  2. google guava工具包collect包HashMultiMap基本用法
  3. Node核心模块Buffer
  4. centos系统下安装python3以及pip3
  5. Python笔记之读取yaml文件
  6. c++读取文件夹下特定文件
  7. 使用javascript生成的植物显示过程特效
  8. java解析json list
  9. wps演示怎么提高列表级别_wps文字如何设置标题级别
  10. 安卓微信支付回调出现白页面
  11. 【寒江雪】空间中的点线和面
  12. MySQL5.7.xx安装卡在Staring the server解决方案--亲测有效
  13. hadoop java 文件追加_HDFS追加文件
  14. 6-1 插入法建立有序链表
  15. L - 芜湖塔台请求起飞
  16. activemq如何保证消息按顺序消费
  17. 带头结点单链表的基本使用
  18. 算法工程师0——算法工程师学习进阶路线
  19. 麒麟子Javascript游戏编程零基础教程一:序言
  20. Python实现按键精灵(一)录制脚本

热门文章

  1. python函数实现生日歌的输出_python如何实现生日快乐代码
  2. Python知识结构图
  3. apache-apollo-1.7.1下载及安装
  4. PS 移动端 百度觅题-ICON思考与绘制-百度UE讲堂-专题视频课程
  5. 秋招每日一题T22——幂次方
  6. 远程桌面连服务器踩过的所有坑(一、win10升级专业版)
  7. 开发真理-觉悟(哈哈哈)
  8. 【移动端适配 视口viewport】移动端meta属性设置的理想视口是什么
  9. Android组件系列----ContentProvider内容提供者 和 android:authorities
  10. 千年老二 1376 set容器简单使用