用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; }
#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"); } }