求助TLE on 10
查看原帖
求助TLE on 10
832774
wish_you_wa_happy楼主2025/2/6 11:06
#include <bits/stdc++.h>
using namespace std;
struct node{
	int x[13];
	int f;
	int path[18];
	int step;
	bool operator < (const node &x)const{
		return f>x.f;
	}
}now;
int a[13][5];
map<long long,int> vis;
priority_queue<node> q;
int f(int x[])
{
	int cnt=0;
	for (int i=1;i<=12;i++)
	{
		if (x[i]==2)cnt+=3;
		else if (x[i]==3)cnt+=2;
		else if (x[i]==4)cnt+=1;
	}
	return cnt;
}
long long tolong(int x[])
{
	long long ret=0;
	for (int i=1;i<=12;i++)
	{
		ret*=10;
		ret+=x[i];
	}
	return ret;
}
void astar()
{
	now.f+=f(now.x);
	q.push(now);
	vis[tolong(now.x)]=1;
	while (!q.empty())
	{
		node t=q.top();
		now=t;
		q.pop();
		for (int i=1;i<=12;i++)
		{
			int y=a[i][now.x[i]];
			now.x[y]++;
			if (now.x[y]==5)now.x[y]=1;
			now.x[i]++;
			if (now.x[i]==5)now.x[i]=1;
			long long b=tolong(now.x);
			if (vis[b]==1)
			{
				now=t;
				continue;
			}
			now.path[++now.step]=i;
			if (b==111111111111)
			{
				cout<<now.step<<'\n';
				for (int i=1;i<=now.step;i++)
				{
					cout<<now.path[i]<<' ';
				}
				return;
			}
			now.f+=f(now.x);
			q.push(now);
			vis[b]=1;
			now=t;
		}
	}
}
int main ()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 
	for (int i=1;i<=12;i++)
	{
		for (int j=0;j<=4;j++)
		{
			cin>>a[i][j];
		}
	}
	for (int i=1;i<=12;i++)
	{
		now.x[i]=a[i][0];
	}
	astar();
	return 0;
}

给出正确解决方案必关

2025/2/6 11:06
加载中...