码迷,mamicode.com
首页 > 其他好文 > 详细

8puzzle & kd-tree & Boggle

时间:2016-06-06 23:21:34      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/lvlvlvlvlv/p/5565334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!