求助关于n<=8的做法
查看原帖
求助关于n<=8的做法
727411
invisible_person楼主2023/2/17 20:53

rt,感觉直接暴力记搜就行,但是挂了。

#include <bits/stdc++.h>
#define int long long
using namespace std;

int read(int w = 10) {
	int s = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch >= '0' && ch <= '9')
		s = s * w + (ch ^ 48), ch = getchar();
	return s * f;
}

const int N = (1 << 10) + 5;

int T, n;
int d[N][N] = {{0}};

int srh(int x, int y) {
//	cout << x << ' ' << y << endl;
	if (!x)
		return 0;
	if (d[x][y])
		return d[x][y];
	d[x][y] = 1e18;
	for (int i = 0; i < n; i++) {
		int xx = x, yy = y;
		yy ^= (1 << i);
		for (int i = 0; i < n; i++)
			if ((yy >> i) & 1)
				xx ^= (1 << i);
		int lst = yy & 1;
		yy >>= 1, yy += lst << (n - 1);
		d[x][y] = min(d[x][y], srh(xx, yy) + 1);
	}
	return d[x][y];
}

signed main() {
	T = read(), n = read();
	while (T--) {
		int x = read(2), y = read(2);
		printf("%lld\n", srh(x, y));
	}
	return 0;
}
2023/2/17 20:53
加载中...