Week4 Assignment - Without Full Score - Princeton-Algorithms-PartI

本文介绍了使用A*搜索算法解决8-Puzzle问题的过程,包括算法思路、最佳优先搜索策略、优先队列优化、游戏树构建、不可行谜题检测以及算法的时间和空间复杂度分析。作者分享了自己实现该算法遇到的问题,如重复搜索节点、内存管理bug等,并寻求社区帮助解决未通过的部分测试案例。
摘要由CSDN通过智能技术生成

题注

第一次跟Princeton的《Algorithm》课没有跟下来的一个核心原因就是这道题无论如何拿不到full score,第一次做的时候分数只有不到85分,因为最后的测试总是有一些test不能通过。经过了大概6个月后,现在再编这个题目,结果还是拿不到full score,不过分数大约提高了10分,只有部分的test不能通过。然而,我自己还是找不到为何通不过的原因,因此先把现在的代码贴出来,和大家分享,也希望朋友们一起帮忙找找错误…

题目

8 Puzzle

Write a program to solve the 8-puzzle problem(and its natural generalizations) using the A* search algorithm.

The problem.The 8-puzzle problemis a puzzle invented and popularized by Noyes Palmer Chapman in the 1870s. It is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order, usingas few moves as possible. You are permitted to slide blocks horizontally or verticallyinto the blank square. The following shows a sequence of legal moves from an initial board (left)to the goal board (right).

 
 
 
1 3 1 3 1 2 3 1 2 3 1 2 3 4 2 5 => 4 2 5 => 4 5 => 4 5 => 4 5 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 initial 1 left 2 up 5 left goal

Best-first search.Now, we describe a solution to the problem that illustrates a general artificial intelligence methodology known as theA* search algorithm.We define a search node of the game to be a board, the numberof moves made to reach the board, and the previous search node.First, insert the initial search node(the initial board, 0 moves, and a null previous search node) into a priority queue. Then,delete from the priority queue the search node with the minimum priority,and insert onto the priority queue all neighboring search nodes(those that can be reached in one move from the dequeued search node).Repeat this procedure until the search node dequeued corresponds to a goal board.The success of this approachhinges on the choice of priority function for a search node. We consider two priority functions:

  • Hamming priority function.The number of blocks in the wrong position,plus the number of moves made so far to get to the search node.Intutively, a search node with a small number of blocks in the wrong positionis close to the goal, and we prefer a search node thathave been reached using a small number of moves.
  • Manhattan priority function.The sum of the Manhattan distances (sum of the vertical and horizontal distance)from the blocks to their goal positions,plus the number of moves made so far to get to the search node.
For example, the Hamming and Manhattan priorities of the initial search nodebelow are 5 and 10, respectively.
 
 
 
8 1 3 1 2 3 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 4 2 4 5 6 ---------------------- ---------------------- 7 6 5 7 8 1 1 0 0 1 1 0 1 1 2 0 0 2 2 0 3 initial goal Hamming = 5 + 0 Manhattan = 10 + 0

We make a key oberservation: To solve the puzzle froma given search node on the priority queue, the total number of moves weneed to make (including those already made) is at least its priority,using either the Hamming or Manhattan priority function.(For Hamming priority, this is true because each block that is out of placemust move at least once to reach its goal position.For Manhattan priority, this is true because each block must moveits Manhattan distance from its goal position.Note that we do not count the blank square when computing theHamming or Manhattan priorities.)Consequently, when the goal board is dequeued, wehave discovered not only a sequence of moves from theinitial board to the goal board, but one that makes the fewest number of moves. (Challenge for the mathematically inclined: prove this fact.)

A critical optimization.Best-first search has one annoying feature:search nodes corresponding to the same boardare enqueued on the priority queue many times.To reduce unnecessary exploration of useless search nodes,when considering the neighbors of a search node, don't enqueuea neighbor if its board is the same as the board of theprevious search node.

 
 
 
8 1 3 8 1 3 8 1 8 1 3 8 1 3 4 2 4 2 4 2 3 4 2 4 2 5 7 6 5 7 6 5 7 6 5 7 6 5 7 6 previous search node neighbor neighbor neighbor (disallow)

Game tree.One way to view the computation is as a game tree, where each search nodeis a node in the game tree and the children of a node correspond to itsneighboring search nodes. The root of the game tree is the initial search node;the internal nodes have already been processed; the leaf nodes are maintainedin a priority queue; at each step, the A* algorithm removes the node with the smallestpriority from the priority queue and processes it (by adding its childrento both the game tree and the priority queue).

8puzzle game tree

Detecting infeasible puzzles.Not all initial boards can lead to the goal board such as the one below.

 
 
 
1 2 3 4 5 6 8 7 infeasible
To detect such situations,use the fact that boardsare divided into two equivalence classes with respect to reachability:(i) those that lead to the goal board and (ii) those thatlead to the goal board if we modify the initial board byswapping any pair of adjacent (non-blank) blocks in the same row.(Difficult challenge for the mathematically inclined: prove this fact.)To apply the fact,run the A* algorithm simultaneously on two puzzle instances—one with theinitial board and one with the initial board modified byswapping a pair of adjacent blocks in the same row. Exactly one ofthe t
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值