雙向廣搜-HDU1401 Solitaire

雙向廣搜

什麼是雙向廣搜?
如果把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 <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("YES\n");
		else printf("NO\n");
	}
	return 0;
}

小結

1.如果爆棧:考慮在入隊前做答案檢查,出隊才檢查會多一輪消耗。
2.如何保存中間狀態?位壓縮.位運算.unordered_set…數組?
3.String判重TLE讓你懷疑人生,string複製會超時,還是老老實實哈希或者康託展開什麼的。
4.神奇剪枝起死回生,一定要注意考慮剪枝,因題制宜。

你的點贊將會是我最大的動力

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章