跳到正文

剪贴板

剪贴板 vx0p9mxt

原文
剪贴板信息

剪贴板信息

收录
更新

P12533 题解代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int r[] = {2, 5, 8, 1, 4, 7, 0, 3, 6}, l[] = {6, 3, 0, 7, 4, 1, 8, 5, 2};

int n, step[520][520], lst[520][520], sa, sb;

string s;

bitset <520> vis[520];

queue <pii> q;
/*
lst:
0:AL
1:AR
2:BL
3:BR
4:swap+3
*/

int L (const int x) {

	int ans = 0;

	for (int i = 0; i < 9; ++ i)
		ans |= (x >> i & 1) << l[i];

	return ans;

}

int R (const int x) {

	int ans = 0;

	for (int i = 0; i < 9; ++ i)
		ans |= (x >> i & 1) << r[i];

	return ans;

}

void swap (int& a, int& b, const int x) {

	for (int i = 3 - x; i < 9; i += 3) {

		const int x = a & 1 << i, y = b & 1 << i, inf = 1023 ^ (1 << i);

		b = (b & inf) | x, a = (a & inf) | y;

	}

	return ;

}

void print (int x, int y) {
	cerr << lst[x][y] << ':' << x << ',' << y << '\n';
	if (! step[x][y])
		return ;

	if (lst[x][y] == 0) {

		print (R(x), y);

		cout << "AL\n";

	} else if (lst[x][y] == 1) {

		print (L(x), y);

		cout << "AR\n";

	} else if (lst[x][y] == 2) {

		print (x, R(y));

		cout << "BL\n";

	} else if (lst[x][y] == 3) {

		print (x, L(y));

		cout << "BR\n";

	} else {

		lst[x][y] -= 3;

		int X = x, Y = y;

		swap(X, Y, lst[x][y]), print(X, Y);

		cout << "C" << lst[x][y] << '\n';

	}

	return ;

}

int main () {

	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
//	int a = 0b100000000, b = 0b111111011;
//	cout << a << ' ' << b << endl;
//	swap(a, b, 3);
//	cout << a << ' ' << b << endl;
	for (int i = 0; i < 3; ++ i) {

		cin >> s;

		for (int j = 0; j < 3; ++ j)
			sa = sa << 1 | (s[j] - '0');

	}

	for (int i = 0; i < 3; ++ i) {

		cin >> s;

		for (int j = 0; j < 3; ++ j)
			sb = sb << 1 | (s[j] - '0');

	}

	q.push ({sa, sb}), vis[sa][sb] = 1;

	while (! q.empty ()) {

		const pii now = q.front ();

		if (now.first == 0 && now.second == 511)
			break ;

		q.pop ();

		int x = L(now.first), y = now.second;
		if (! vis[x][y]) {
			vis[x][y] = 1;
			lst[x][y] = 0;
			step[x][y] = step[now.first][now.second] + 1;
			q.push({x, y});
		}

		x = R(now.first), y = now.second;
		if (! vis[x][y]) {
			vis[x][y] = 1;
			lst[x][y] = 1;
			step[x][y] = step[now.first][now.second] + 1;
			q.push({x, y});
		}

		x = now.first, y = L(now.second);
		if (! vis[x][y]) {
			vis[x][y] = 1;
			lst[x][y] = 2;
			step[x][y] = step[now.first][now.second] + 1;
			q.push({x, y});
		}

		x = now.first, y = R(now.second);
		if (! vis[x][y]) {
			vis[x][y] = 1;
			lst[x][y] = 3;
			step[x][y] = step[now.first][now.second] + 1;
			q.push({x, y});
		}

		const int X = now.first, Y = now.second;
		for (int i = 1; i <= 3; ++ i) {
			x = X, y = Y;
			swap(x, y, i);
			if (! vis[x][y]) {
				vis[x][y] = 1;
				lst[x][y] = i + 3;
				step[x][y] = step[now.first][now.second] + 1;
				q.push({x, y});
			}
		}

	}

	cout << step[0][511] << '\n';
	print(0, 511);

	return 0;
}