什么是双向广搜?
如果把bfs想象成在平静的池塘丢一颗石头,激起的波浪一层层扩散到整个空间直到到达目标,就得到起点到终点的最优路径。那么双向广搜就是在起点和终点同时丢石头,两个波浪将在中间某个位置相遇,即得到最优路径。
传送门: HDU-1401
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 5
2 4 3 3 3 6 4 6
Sample Output:
YES
题意
在8*8棋盘有四颗棋子,输入开始状态和目标状态的棋子坐标,每次可以移动一个棋子也可以跳过另一个棋子,问能否在8步内走到目标状态。
分析用双向广搜求解,8个坐标值位压缩成为10进制作为hash值(或者8维数组?)并用unordered_set判重,当hash值出现在另一个分支即相遇。注意剪枝:每个棋子最多只能走4步,因为如果多于4步,那么他们步数之和就会大于8。
代码#include
#include
using namespace std;
typedef long long ll;
int stepx[4] = { -1,1,0,0 };
int stepy[4] = { 0,0,-1,1 };
struct node {
int p[4], vis;
bool check(int v){ //查重
for (int i : p)
if (i == v)
return 1;
return 0;
}
int hash(){
int a[4];
memcpy(a, p, sizeof(a));
sort(a, a + 4);
int res = 0;
for (int i : a)
res = res * 100 + i;
return res;
}
};
int dbfs() {
queue q[2];
unordered_set st[2]; //存hash
node t;
int a;
t.vis = 0;
for (int k = 0; k < 2; ++k) {
for (int i = 0; i < 4; ++i) {
t.p[i] = 0;
for (int j = 0; j < 2; ++j){
if (scanf("%d", &a) == EOF)
exit(0);
t.p[i] = t.p[i] * 10 + a;
}
}
q[k].push(t);
st[k].insert(t.hash());
}
while (!q[0].empty() || !q[1].empty()) {
for (int i = 0; i < 2; ++i) { //2个队列
if (!q[i].empty()) {
node f = q[i].front(); q[i].pop();
if (st[!i].find(f.hash()) != st[!i].end()) //相遇
return 1;
f.vis++;
for (int j = 0; j < 4; ++j) { //4个点
int x = f.p[j] / 10, y = f.p[j] % 10;
for (int k = 0; k = 1 && nx = 1 && ny = 1 && nx = 1 && ny <= 8 && f.vis <= 4 && //小于四步
!f.check(nx * 10 + ny)){
f.p[j] = nx * 10 + ny;
if (st[i].find(f.hash()) == st[i].end())
q[i].push(f), st[i].insert(f.hash()); //入队
}
}
f.p[j] = x * 10 + y; //回溯
}
}
}
}
return 0;
}
int main() {
while (true){
if (dbfs())printf("YES\n");
else printf("NO\n");
}
return 0;
}
小结
1.爆栈:在入队前做答案检查,谜之出队才检查。
2.如何保存中间状态?位压缩.位运算.unordered_set…数组?
3.String判重TLE让你怀疑人生,string复制会超时,还是老老实实哈希或者康托展开什么的。
4.神奇剪枝起死回生,一定要注意考虑剪枝,因题制宜。
你的点赞将会是我最大的动力