标签:
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:
A 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.
Boggle:
这部分主要使用trie数据结构存储一个字典,然后在boggle面板中使用dfs进行深度优先搜索单词。最需要注意的一个地方,就是在做剪枝的决策时,要以字典中的trie树节点不为空为轴心,来迅速放弃一些不必要的搜索可能,而不是先构造局部字符串,然后再回去字典中搜索看找不找得到,这样的话就会节省一大部分时间,也是最后拿满分的关键。
标签:
原文地址:http://www.cnblogs.com/lvlvlvlvlv/p/5565334.html