A* 空间会被卡,只有22分
查看原帖
A* 空间会被卡,只有22分
524801
不食嗟来之食楼主2023/5/11 09:47
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int N=10;
typedef long long ll;
#define RE register
template<typename T>
inline T read(){
	T x=0;short f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}
struct peo{
	int a[N];
	void clear(){memset(a,0,sizeof(a));}
}a;
int f(peo ax){
	int K=0;
	for(int i=1;i<=9;i++) {
		if(!ax.a[i])continue;
		K+=(4-ax.a[i]/3);
	}
	return K;
}
//struct peo2{
//	int k;
//	peo x;
//	string S;
//	bool operator<(const peo2 &ax) const {
//		return (k+f(x))!=(ax.k+f(ax.x))?(k+f(x))>(ax.k+f(ax.x)):(S>ax.S);
//	}
//};
struct peo2{
	int k;
	peo x;
	string S;
	bool operator<(const peo2 &ax) const {
		return (k)!=(ax.k)?(k)>(ax.k):(S>ax.S);
	}
};
priority_queue<peo2> q;
//map<string,bool> M;
string Step[10]={"","1245","123","2356","147","24568","369","4578","789","5689"};
void print(string S){
	for(int i=0;i<S.length();i++) printf("%c ",S[i]);
	return ;
}
void DeBug(peo2 tmp){
	string S=tmp.S;
	for(int i=0;i<S.length();i++) printf("%c ",S[i]);
	printf("\n");
	printf("!  %d\n",tmp.k+f(tmp.x));
	return ;
}
int main(){
	a.clear();
	a.a[1]=read<int>();a.a[2]=read<int>();a.a[3]=read<int>();
	a.a[4]=read<int>();a.a[5]=read<int>();a.a[6]=read<int>();
	a.a[7]=read<int>();a.a[8]=read<int>();a.a[9]=read<int>();
	q.push({0,a,""});
	while(!q.empty()){
		peo2 tmp=q.top();
//		if(tmp.S[0]>'5'&&tmp.){
//			break;
//		}
		q.pop();
//		DeBug(tmp);
		if(!f(tmp.x)){
//			printf("%d\n",tmp.k);
			print(tmp.S);
			return 0;
		}
		for(int i=1;i<=9;i++) {
			peo2 S=tmp;
			for(int j=0;j<Step[i].length();j++)(S.x.a[Step[i][j]-'0']+=3)%=12;
			S.S.push_back(i+'0');S.k=tmp.k+1;
//			if(M[S.S]) continue;
			q.push(S);
//			M[S.S]=1;
//			DeBug(S);
		}
	}
	return 0;
}

2023/5/11 09:47
加载中...