Starting from any configuration, some series of moves can always make the puzzle look identical to either one shown in Figures 2 and 3<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPi4gWW91ciB0YXNrIGlzIHRvIGNhbGN1bGF0ZSB0aGUgbWluaW11bSBudW1iZXIgb2YgbW92ZXMgdG8gbWFrZSBpdCBiZWNvbWUgdGhlIG9uZSBpbjxzdHJvbmc+RmlndXJlCiAyPC9zdHJvbmc+LjwvcD4KCjxwIGNsYXNzPQ=="pst">Input
The input contains multiple test cases. The first contains an integer N (0 ≤ N ≤ 5,040), the number of test cases. Then follow N lines, each with a permutation of {0, 1, 2, 3, 4, 5, 6} describing a starting configuration of the puzzle. The ith digit in the permutation is the one in the face numbered i ? 1. A zero means the face is empty.
Output
For each test cases, output the minimum number of moves the configuration takes to reach the one shown in Figure 2. If this is impossible, just output “-1” and nothing else.
Sample Input
3
1324506
2410653
0123456
Sample Output
10
-1
0
渣渣水平看了题解,果断bfs走起。ps:博客写的太简单了,因为时间比较紧。所以以后时间充足的话还是详细点好。
题意:由当前状态得到最终状态需要最小步数。只允许在空格处移动=-=,从最终状态开始搜
所以就记录0所在位子以及由此0可以移动到的位置。比如:0在位置2,则3,0,1号位置都可以移动到此位置。
另外的对于我来说就是STL里map的使用了,映射关系map
visit记录有木有被访问过,map
ans记录到谋一状态所需的最小步数。下面贴挫码。思路来源:点击打开链接
#include
#include
#include
#include
#include
#include
#include