玄关求条,ans*3
查看原帖
玄关求条,ans*3
1573746
wyxing楼主2025/7/26 10:51

题目输出:4 5 8 9

我的输出:4 4 4 5 5 5 8 8 8 9 9 9

人傻了

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

ll target=444444444;//结果为9个4
unordered_set<ll> sg;//哈希判重

vector<vector<int>> arr= {//规则
	{},
	{1,2,4,5},{1,2,3},{2,3,5,6},
	{1,4,7},{2,4,5,6,8},{3,6,9},
	{4,5,7,8},{7,8,9},{5,6,8,9}
};

struct node {
	vector<int> v,p;
	ll hash;
	node():v(10) {}
} st,nx;

void bfs() {
	queue<node> q;
	q.push(st);
	sg.insert(st.hash);

	while(!q.empty()) {
		st=q.front();
		q.pop();

		if(st.hash==target) {//得到结果
			sort(st.p.begin(),st.p.end());
			for(int &it:st.p)
				cout<<it<<' ';
			return;
		}

		for(int i=1; i<10; i++) {//枚举规则
			nx=st;
			nx.hash=0;

			for(int &it:arr[i]) {//旋转
				nx.v[it]-=1;
				if(nx.v[it]==0)nx.v[it]=4;
			}

			for(int j=1; j<10; j++)//获取哈希值
				nx.hash=(nx.hash<<3)+(nx.hash<<1)+nx.v[j];

			if(sg.insert(nx.hash).second) {//尝试添加进队列
				nx.p.emplace_back(i);
				sg.insert(nx.hash);
				q.push(nx);
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);

	for(int i=1; i<10; i++) {
		cin>>st.v[i];
		st.v[i]/=3;//状态为1-4
		st.hash=(st.hash<<3)+(st.hash<<1)+st.v[i];//初始哈希值
	}

	bfs();
	return 0;
}
2025/7/26 10:51
加载中...