8puzzle & kd-tree & Boggle

Posted 吕吕吕吕吕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8puzzle & kd-tree & Boggle相关的知识,希望对你有一定的参考价值。

8puzzle:

using A* algorithm to solve 8-puzzle question.

1.define a state of board position

2.the number of moves made to reach the board position

3.the previous state

insert initial board state, 0 moves, null previous state into priority queue.

the every time delete from priority queue the state with minimum priority, and insert all its neibors state(those can be reached in one move)

repeat the procedure until the state dequeued is goal state.

To calculate priority: there are two priority functions:

(1) hamming priority function:The number of blocks in the wrong position, plus the number of moves made so far to get to the state

(2) manhatan priority function:The sum of the 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 state.

A critical optimization. After implementing best-first search, you will notice one annoying feature: states corresponding to the same board position are enqueued on the priority queue many times. To prevent unnecessary exploration of useless states, when considering the neighbors of a state, don‘t enqueue the neighbor if its board position is the same as the previous state.

 

kd-tree:

2d-tree is a generalization of a BST to two-dimensional keys. The idea is to build a BST with points in the nodes, using the x- and y-coordinates of the points as keys in strictly alternating sequence.

The prime advantage of a 2d-tree over a BST is that it supports efficient implementation of range search and nearest neighbor search. Each node corresponds to an axis-aligned rectangle in the unit square, which encloses all of the points in its subtree. The root corresponds to the unit square; the left and right children of the root corresponds to the two rectangles split by the x-coordinate of the point at the root; and so forth.

 

  • Range search. To find all points contained in a given query rectangle, start at the root and recursively search for points in both subtrees using the following pruning rule: if the query rectangle does not intersect the rectangle corresponding to a node, there is no need to explore that node (or its subtrees). A subtree is searched only if it might contain a point contained in the query rectangle.
  • Nearest neighbor search. To find a closest point to a given query point, start at the root and recursively search in both subtrees using the following pruning rule: if the closest point discovered so far is closer than the distance between the query point and the rectangle corresponding to a node, there is no need to explore that node (or its subtrees). That is, a node is searched only if it might contain a point that is closer than the best one found so far. The effectiveness of the pruning rule depends on quickly finding a nearby point. To do this, organize your recursive method so that when there are two possible subtrees to go down, you always choose the subtree that is on the same side of the splitting line as the query point as the first subtree to explore—the closest point found while exploring the first subtree may enable pruning of the second subtree.

 

Boggle:

这部分主要使用trie数据结构存储一个字典,然后在boggle面板中使用dfs进行深度优先搜索单词。最需要注意的一个地方,就是在做剪枝的决策时,要以字典中的trie树节点不为空为轴心,来迅速放弃一些不必要的搜索可能,而不是先构造局部字符串,然后再回去字典中搜索看找不找得到,这样的话就会节省一大部分时间,也是最后拿满分的关键。

 

以上是关于8puzzle & kd-tree & Boggle的主要内容,如果未能解决你的问题,请参考以下文章

KD-Tree

Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

Kd-tree算法原理

Kd-Tree 插入顺序

赶鸭子上架的cdq分治

KD-tree讲解