【问题标题】:What is the best algorithm to solve my maze?解决我的迷宫的最佳算法是什么?
【发布时间】:2023-03-20 14:43:01
【问题描述】:

我正在尝试使用简单的 AI 解决地图求解问题。

我们有 3 个函数可以使用:

  • move:根据冒险者(代理)的方向(上、下、左、右)向前移动 1 个格子。

  • rotate:将冒险者旋转并顺时针改变方向 90 度。

  • scan:根据方向返回最近的物体给冒险者。它还会扫描到地图的边缘,只有在没有找到对象时才会返回空。

扫描结果有4种。

  • 公主:这是冒险者要达到的目标。

  • 尖峰:冒险者必须避开尖峰,否则游戏结束。

  • 塔:这告诉冒险家公主沿着塔的轴线(公主和塔之间不能有钉子)

  • 空:扫描轴内没有物体。

用户不知道地图的内容,会从0知识开始,应该找到到达公主的最有效方式。

鉴于尖峰、公主和塔的位置是随机生成的,我可以使用什么方法来解决这个问题?

这是一个示例地图:

红色 - 冒险家,粉色 - 公主,黑色 - 斯派克

【问题讨论】:

  • 扫描是否也返回距离?
  • 我不明白塔是什么意思。
  • @Surt no,它只会返回找到的最近的对象。
  • @trincot 塔就像一个物体,通知代理公主在塔的轴线内。例如,如果塔位于 (1,0),那么公主必须在 (2,0) 到 (n,0) 之间(n 取决于地图的大小)
  • 例如,您可以使用修改后的深度优先搜索。通过扫描获取所有代理的邻居,旋转 4 次。如果其中一个邻居是:Prince - 达到目标。塔 - 添加到堆栈并忽略其他。其他明智的做法是让所有邻居都接受堆栈中的尖峰。

标签: algorithm artificial-intelligence depth-first-search breadth-first-search maze


【解决方案1】:

我会在这个假设下试一试

  • 当我们遇到高峰时,我们可以在同一张地图上重新开始。
  • 我们知道地图的大小
  • 我们知道我们从地图上的哪个位置开始

想法

  • 使用 BFS 找到包含未知内容的方块,移动到那里,扫描
  • 重复直到找到公主或塔
  • 移动到塔或公主
  • 如果尖峰命中以现在已知的方格重新开始。

更详细的伪代码

make a map of 0..size+1 with all squares marked unknown
make start empty 
mark all edge squares as edges.

start:
Repeat
  Find nearest square where there is a direction with an unknown as nearest none empty
  Move to that square
  if step on spike 
    mark square as spike
    goto start:  // replace with return/throw/whatever

  While direction with an unknown as nearest none empty
    turn if needed
    scan in that direction
    mark the first unknown in that direction with maybe result of scan
    call CheckScan


CheckScan:
  if maybe empty found
    mark all squares in that direction empty until edge.
  if maybe princess is found 
    move in that direction repeatedly, finish when reached

  if maybe tower is found 
    move in that direction until reached
    mark it tower

  if tower is found 
    for each direction 
      if direction with an unknown as nearest none empty
        scan
      if maybe princess is found 
        move in that direction repeatedly, finish when reached
      turn 90

肯定会有错误和可能的改进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多