本文共 3508 字,大约阅读时间需要 11 分钟。
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.
There are four identical pieces on the board. In one move it is allowed to: > move a piece to an empty neighboring field (up, down, left or right), > jump over one neighboring piece to an empty field (up, down, left or right). There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right. Write a program that: > reads two chessboard configurations from the standard input, > verifies whether the second one is reachable from the first one in at most 8 moves, > writes the result to the standard output.Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
Output
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
Sample Input
4 4 4 5 5 4 6 52 4 3 3 3 6 4 6
Sample Output
YES
第一次做的是用的单向bfs,然后简单的剪了一下枝,然后tle。
然后网上的都是用的双向bfs,于是就学了一手双向bfs,建了两个队列和两个hash表,然后从起点和终点开始搜索,双向bfs一次是搜索队列的一层,而不仅仅是一个节点。
当两个hash表中都有那个元素的时候就说明找到了。
hash表的建立是运用了移位运算,因为每一位是0-7,所以只占三位,因此表示每十进制一位数的时候只需要移动三位就可以了。
#include#include #include #include #include
转载地址:http://vwoen.baihongyu.com/