3 条题解

  • 1
    @ 2024-6-17 15:21:47
    #include <bits/stdc++.h>
    using namespace std;
    typedef short sh;
    #define st pair<int, sh>
    #define B first
    #define e second
    sh n, m, T;
    bool a;
    bool B_W(st a, sh x) {
    	return a.B >> x & 1;
    }
    st now, t, res;
    map<st, bool> vis;
    queue<pair<st, vector<sh>>> lis;
    vector<sh> pa;
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr), cout.tie(nullptr);
    	cin >> n;
    	m = (n << 1) + 1;
    	now.e = res.e = n;
    	for (sh i = 0; i < n; ++i)
    		now.B += 1 << i, res.B += 1 << i + 1 + n;
    	lis.push({now, pa}), vis[now] = 1;
    	while (!lis.empty()) {
    		now = lis.front().B, pa = lis.front().e;
    		if (now == res) {
    			for (sh i : pa)
    				cout << i + 1 << (++T % 20 ? ' ' : '\n');
    			break;
    		} lis.pop(), pa.push_back(0), t = now;
    		if (t.e - 2 >= 0 && B_W(t, t.e - 2) && !B_W(t, t.e - 1)) {
    			pa.back() = t.e - 2;
    			t.B -= 1 << t.e - 2, t.B += 1 << t.e, t.e -= 2;
    			if (!vis[t]) lis.push({t, pa}), vis[t] = 1;
    		} else if (t.e - 1 >= 0 && B_W(t, t.e - 1)) {
    			pa.back() = t.e - 1;
    			t.B -= 1 << t.e - 1, t.B += 1 << t.e, --t.e;
    			if (!vis[t]) lis.push({t, pa}), vis[t] = 1;
    		} t = now;
    		if (t.e + 1 < m && !B_W(t, t.e + 1)) {
    			pa.back() = t.e + 1;
    			++t.e;
    			if (!vis[t]) lis.push({t, pa}), vis[t] = 1;
    		} else if (t.e + 2 < m && !B_W(t, t.e + 2) && B_W(t, t.e + 1)) {
    			pa.back() = t.e + 2;
    			t.e += 2;
    			if (!vis[t]) lis.push({t, pa}), vis[t] = 1;
    		}
    	}
    	return 0;
    }
    
    

    信息

    ID
    558
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    57
    已通过
    9
    上传者