Sicily1150 && Sicily1151(广搜)

用string存储状态,map判重,0.99s险些超时:

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <string>

using namespace std;

struct magic
{
	string st;
	string path;
};

int main()
{
	int m,i;
	while (1)
	{
		queue<magic> A;
		map<string,int> visit;
		string aim;
		magic start;
		start.st="12348765";
		magic temp;
		char tempch[9];
		tempch[8]='\0';
		A.push(start);
		char s[9];
		s[8]='\0';
		scanf("%d",&m);
		if (m==-1)
			break;
		getchar();
		for (i=0;i<=7;i++)
		{
			scanf("%c",&s[i]);
			getchar();
		}
		aim=s;
    while (!A.empty() && A.front().st!=aim)
    {
      for (i=0;i<=3;i++)
      	tempch[i]=A.front().st[i+4];
      for (i=4;i<=7;i++)
      	tempch[i]=A.front().st[i-4];
      temp.st=tempch;
      temp.path=A.front().path+'A';
      if (visit[temp.st]!=1)
      {
        A.push(temp);
        visit[temp.st]=1;
      }

      tempch[0]=A.front().st[3];
      for (i=1;i<=3;i++)
      	tempch[i]=A.front().st[i-1];
      tempch[4]=A.front().st[7];
      for (i=5;i<=7;i++)
      	tempch[i]=A.front().st[i-1];
      temp.st=tempch;
      temp.path=A.front().path+'B';
      if (visit[temp.st]!=1)
      {
        A.push(temp);
        visit[temp.st]=1;
      }

      tempch[0]=A.front().st[0];
      tempch[3]=A.front().st[3];
      tempch[4]=A.front().st[4];
      tempch[7]=A.front().st[7];
      tempch[1]=A.front().st[5];
      tempch[2]=A.front().st[1];
      tempch[6]=A.front().st[2];
      tempch[5]=A.front().st[6];
      temp.st=tempch;
      temp.path=A.front().path+'C';
      if (visit[temp.st]!=1)
      {
        A.push(temp);
        visit[temp.st]=1;
      }

      A.pop();
    }

    if (A.empty())
    	cout << "-1" << endl;
    else
    	cout << A.front().path.length() << ' ' << A.front().path << endl;
	}
	return 0;
}

数组存储状态,康托展开hash判重,0.14s:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std;

struct node
{
	int A[8];
	int length;
	string path;
};

int fac[]={1,1,2,6,24,120,720,5040};
int vstd[50000];
int cantor(int A[])
{
	int i,j;
	int ans=0;
	int record;
	for (i=0;i<8;i++)
	{
		record=0;
		for (j=i+1;j<8;j++)
			if (A[j]<A[i])
				record++;
		ans+=record*fac[7-i];
	}
	return ans+1;
}

void swap(int &a,int &b)
{
	int temp=a;
	a=b;
	b=temp;
}

int main()
{
	int aim;
	int i;
	while (1)
	{
		memset(vstd,0,sizeof(vstd));
		scanf("%d",&aim);
		if (aim == -1) break;
		struct node ans;
		for (i=0;i<8;i++)
			scanf("%d",&ans.A[i]);
		int anss=cantor(ans.A);

		struct node init={{1,2,3,4,8,7,6,5},0};
		queue<struct node> Q;
		vstd[cantor(init.A)]=1;
		Q.push(init);

		bool flag=true;
		while (!Q.empty())
		{
			struct node front=Q.front();
			if (front.length>aim)
			{
				flag=false;
				break;
			}
			if (cantor(front.A)==anss) break;

			struct node temp;
			int Cantor;
			int t;
			//A
			temp=front;
			swap(temp.A[0],temp.A[4]);
			swap(temp.A[1],temp.A[5]);
			swap(temp.A[2],temp.A[6]);
			swap(temp.A[3],temp.A[7]);
			Cantor=cantor(temp.A);
			if (!vstd[Cantor])
			{
				temp.path+='A';
				temp.length=front.length+1;
				vstd[Cantor]=1;
				Q.push(temp);
			}

			//B
			temp=front;
			t=temp.A[3];
			temp.A[3]=temp.A[2];
			temp.A[2]=temp.A[1];
			temp.A[1]=temp.A[0];
			temp.A[0]=t;
			t=temp.A[7];
			temp.A[7]=temp.A[6];
			temp.A[6]=temp.A[5];
			temp.A[5]=temp.A[4];
			temp.A[4]=t;
			Cantor=cantor(temp.A);
			if (!vstd[Cantor])
			{
				temp.path+='B';
				temp.length=front.length+1;
				vstd[Cantor]=1;
				Q.push(temp);
			}

			//C
			temp=front;
			t=temp.A[6];
			temp.A[6]=temp.A[2];
			temp.A[2]=temp.A[1];
			temp.A[1]=temp.A[5];
			temp.A[5]=t;
			Cantor=cantor(temp.A);
			if (!vstd[Cantor])
			{
				temp.path+='B';
				temp.length=front.length+1;
				vstd[Cantor]=1;
				Q.push(temp);
			}

			Q.pop();
		}
		if (flag && !Q.empty())
		{
			printf("%d ",Q.front().length);
		    cout << Q.front().path << endl;
		}
		else
			printf("-1\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值