Sicily1150 && Sicily1151(广搜)
来源:互联网 发布:淘宝客服个人工作经验 编辑:程序博客网 时间:2024/05/06 02:59
用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;//Atemp=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);}//Btemp=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);}//Ctemp=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;}elseprintf("-1\n");}}
- Sicily1150 && Sicily1151(广搜)
- sicily1151魔板
- sicily1150 & 魔板
- sicily1151&魔板2
- Sicily1150 简单魔板
- 广搜
- 广搜
- 广搜
- 广搜
- 广搜
- 广搜
- POJ_3278_广搜
- 深搜 广搜
- hdu 1026 广搜
- 1242 rescue 广搜
- zoj 1091 广搜。
- POJ3083 广搜&模拟
- poj3278基本广搜
- tomcat配置gzip压缩,提升网站的相应
- Posix互斥量pthread_mutex_t
- IE can't move focus 问题
- 8 11 周赛
- 【7gyy】老旧的回忆xp主题介绍_8.12
- Sicily1150 && Sicily1151(广搜)
- 面向接口编程的好处分析
- 李彦宏搭台,百度的夏日狂欢
- FromHandle
- fck在上传文件的时候 FCKeditor 报错
- 一个单例
- picture control控件
- Mybatis 3.1中 Mapper XML 文件 的学习详解
- JsonUtils工具类 把java对象转成Json串的工具类