双向广搜-HDU1401 Solitaire

2020-09-15 11:26:11 浏览数 (1)

文章目录

  • 双向广搜
  • 例题
    • 题意
    • 分析
    • 代码
  • 小结

双向广搜


什么是双向广搜? 如果把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:

代码语言:javascript复制
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6

Sample Output:

代码语言:javascript复制
YES

题意

在8*8棋盘有四颗棋子,输入开始状态和目标状态的棋子坐标,每次可以移动一个棋子也可以跳过另一个棋子,问能否在8步内走到目标状态。

分析

用双向广搜求解,8个坐标值位压缩成为10进制作为hash值(或者8维数组?)并用unordered_set判重,当hash值出现在另一个分支即相遇。注意剪枝:每个棋子最多只能走4步,因为如果多于4步,那么他们步数之和就会大于8。

代码

代码语言:javascript复制
#include <bits/stdc  .h>
#include <unordered_set>
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;
	}
};

unordered_set<int> st[2]; //存hashc
queue<node> q[2];

void clear() {
	for (int i = 0; i < 2; i  ) {
		queue<node>c;
		swap(q[i], c);
		st[i].clear();
	}
}
bool ans(int i, int hash) {//队列下标和待查hash
	if (st[i].find(hash) != st[i].end()) //相遇
		return true;
	return false;
}
bool dbfs() {
	while (!q[0].empty() || !q[1].empty()) {
		for (int i = 0; i < 2;   i) { //2个队列
			if (!q[i].empty()) {
				node cur = q[i].front(); q[i].pop();
				cur.vis  ;
				for (int j = 0; j < 4;   j) { //4个点
					int x = cur.p[j] / 10, y = cur.p[j] % 10;
					for (int k = 0; k < 4;   k) { //4个方向
						int nx = x   stepx[k], ny = y   stepy[k];
						if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8 && cur.check(nx * 10   ny))
							nx  = stepx[k], ny  = stepy[k];
						if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8 && cur.vis <= 4 && //小于四步
							!cur.check(nx * 10   ny)) {
							cur.p[j] = nx * 10   ny;
							if (st[i].find(cur.hash()) == st[i].end()) {
								if (ans(!i, cur.hash()))return true;
								q[i].push(cur), st[i].insert(cur.hash()); //入队
							}
						}
					}
					cur.p[j] = x * 10   y; //回溯
				}
			}
		}
	}
	return false;
}
int main() {
	while (true) {
		clear();
		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());
		}
		if (ans(0, q[1].front().hash()) || dbfs())
			printf("YESn");
		else printf("NOn");
	}
	return 0;
}

小结


1.如果爆栈:考虑在入队前做答案检查,出队才检查会多一轮消耗。 2.如何保存中间状态?位压缩.位运算.unordered_set…数组? 3.String判重TLE让你怀疑人生,string复制会超时,还是老老实实哈希或者康托展开什么的。 4.神奇剪枝起死回生,一定要注意考虑剪枝,因题制宜。

原创不易,请勿转载本不富裕的访问量雪上加霜 ) 博主首页:https://blog.csdn.net/qq_45034708

0 人点赞