POJ 1077
2013-08-14 11:24
141 查看
TLE的两个代码:
①
②
③
①
#include <iostream> #include <cstdio> #include <queue> #include <map> #include <string> using namespace std; long int fac[]={1,1,2,6,24,120,720,5040,40320,362880}; struct eight { int st[10]; string path; int pos; }; int Cantor(int *s,int n) { int i,j,num,temp; num=0; for (i=0;i<n;i++) { temp=0; for (j=i+1;j<n;j++) if (s[j]<s[i]) temp++; num+=fac[n-1-i]*temp; } return num; } int main() { int i; char c; eight start; queue<eight> A; int aim[9]={1,2,3,4,5,6,7,8,0}; int Aim=Cantor(aim,9); int visit[362881]; for (i=0;i<=8;i++) { scanf("%c",&c); if (c=='x') { start.st[i]=0; start.pos=i; } else start.st[i]=c-'0'; getchar(); } A.push(start); int can; while (!A.empty() && Cantor(A.front().st,9)!=Aim) { eight temp; for (i=0;i<=8;i++) temp.st[i]=A.front().st[i]; temp.pos=A.front().pos; temp.path=A.front().path; if (A.front().pos-3>=0) { temp.pos=A.front().pos-3; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]=0; temp.path+='u'; } can=Cantor(temp.st,9); if (visit[can]!=1) { A.push(temp); visit[can]=1; } for (i=0;i<=8;i++) temp.st[i]=A.front().st[i]; temp.pos=A.front().pos; temp.path=A.front().path; if (A.front().pos+3<=8) { temp.pos=A.front().pos+3; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]=0; temp.path+='d'; } can=Cantor(temp.st,9); if (visit[can]!=1) { A.push(temp); visit[can]=1; } for (i=0;i<=8;i++) temp.st[i]=A.front().st[i]; temp.pos=A.front().pos; temp.path=A.front().path; if (A.front().pos!=0 && A.front().pos!=3 && A.front().pos!=6) { temp.pos=A.front().pos-1; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]=0; temp.path+='l'; } can=Cantor(temp.st,9); if (visit[can]!=1) { A.push(temp); visit[can]=1; } for (i=0;i<=8;i++) temp.st[i]=A.front().st[i]; temp.pos=A.front().pos; temp.path=A.front().path; if (A.front().pos!=2 && A.front().pos!=5 && A.front().pos!=8) { temp.pos=A.front().pos+1; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]=0; temp.path+='r'; } can=Cantor(temp.st,9); if (visit[can]!=1) { A.push(temp); visit[can]=1; } A.pop(); } if (A.empty()) cout << "unsolvable" << endl; else cout << A.front().path << endl; return 0; }
②
#include <iostream> #include <cstdio> #include <queue> #include <map> #include <string> using namespace std; struct eight { string st; string path; int pos; }; int main() { int i; eight start; queue<eight> A; map<string,int> visit; char str[10]; string aim("12345678x"); str[9]='\0'; for (i=0;i<=8;i++) { scanf("%c",&str[i]); if (str[i]=='x') start.pos=i; getchar(); } start.st=str; A.push(start); while (!A.empty() && A.front().st!=aim) { eight temp; temp.st=A.front().st; temp.path=A.front().path; if (A.front().pos-3>=0) { temp.pos=A.front().pos-3; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]='x'; temp.path+='u'; } if (visit[temp.st]!=1) { A.push(temp); visit[temp.st]=1; } temp.st=A.front().st; temp.path=A.front().path; if (A.front().pos+3<=8) { temp.pos=A.front().pos+3; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]='x'; temp.path+='d'; } if (visit[temp.st]!=1) { A.push(temp); visit[temp.st]=1; } temp.st=A.front().st; temp.path=A.front().path; if (A.front().pos!=0 && A.front().pos!=3 && A.front().pos!=6) { temp.pos=A.front().pos-1; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]='x'; temp.path+='l'; } if (visit[temp.st]!=1) { A.push(temp); visit[temp.st]=1; } temp.st=A.front().st; temp.path=A.front().path; if (A.front().pos!=2 && A.front().pos!=5 && A.front().pos!=8) { temp.pos=A.front().pos+1; temp.st[A.front().pos]=temp.st[temp.pos]; temp.st[temp.pos]='x'; temp.path+='r'; } if (visit[temp.st]!=1) { A.push(temp); visit[temp.st]=1; } A.pop(); } if (A.empty()) cout << "unsolvable" << endl; else cout << A.front().path << endl; return 0; }
③
#include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <queue> #include <vector> #include <map> #include <string> using namespace std; long int fac[]={1,1,2,6,24,120,720,5040,40320,362880}; struct eight { int st[9]; string path; int pos; int g; int h; eight(){g=0; h=0; pos=0;} }; int Cantor(const int *s,int n) { int i,j,num,temp; num=0; for (i=0;i<n;i++) { temp=0; for (j=i+1;j<n;j++) if (s[j]<s[i]) temp++; num+=fac[n-1-i]*temp; } return num; } struct ComEdge { bool operator()(eight a,eight b) { return a.g+a.h>b.g+b.h; } }; int main() { while (1) { int i; char c; eight start; priority_queue<eight,vector<eight>,ComEdge> A; int aim[9]={1,2,3,4,5,6,7,8,0}; int Aim=Cantor(aim,9); int visit[362881]; memset(visit,0,sizeof(visit)); for (i=0;i<=8;i++) { if (!(cin >> c)) return 0; if (c=='x') { start.st[i]=0; start.pos=i; } else start.st[i]=c-'0'; } for (i=0;i<=7;i++) { if (start.st[i]!=i+1) start.h++; } if (start.st[8]!=0) start.h++; A.push(start); int can; while (!A.empty() && Cantor(A.top().st,9)!=Aim) { eight temp1,temp2,temp3,temp4; memcpy(temp1.st,A.top().st,sizeof(int)*9); temp1.pos=A.top().pos; temp1.path=A.top().path; temp1.h=A.top().h; if (A.top().pos-3>=0) { temp1.pos=A.top().pos-3; if (A.top().pos==8) temp1.h++; else if (A.top().pos-3==8) temp1.h--; if (A.top().st[temp1.pos]==A.top().pos+1) temp1.h--; else if (A.top().st[temp1.pos]==A.top().pos-2) temp1.h++; temp1.st[A.top().pos]=temp1.st[temp1.pos]; temp1.st[temp1.pos]=0; temp1.path+='u'; } memcpy(temp2.st,A.top().st,sizeof(A.top().st)); temp2.pos=A.top().pos; temp2.path=A.top().path; temp2.h=A.top().h; if (A.top().pos+3<=8) { temp2.pos=A.top().pos+3; if (A.top().pos==8) temp2.h++; else if (A.top().pos+3==8) temp2.h--; if (A.top().st[temp2.pos]==A.top().pos+1) temp2.h--; else if (A.top().st[temp2.pos]==A.top().pos+4) temp2.h++; temp2.st[A.top().pos]=temp2.st[temp2.pos]; temp2.st[temp2.pos]=0; temp2.path+='d'; } memcpy(temp3.st,A.top().st,sizeof(A.top().st)); temp3.pos=A.top().pos; temp3.path=A.top().path; temp3.h=A.top().h; if (A.top().pos!=0 && A.top().pos!=3 && A.top().pos!=6) { temp3.pos=A.top().pos-1; if (A.top().pos==8) temp3.h++; else if (A.top().pos-1==8) temp3.h--; if (A.top().st[temp3.pos]==A.top().pos+1) temp3.h--; else if (A.top().st[temp3.pos]==A.top().pos) temp3.h++; temp3.st[A.top().pos]=temp3.st[temp3.pos]; temp3.st[temp3.pos]=0; temp3.path+='l'; } memcpy(temp4.st,A.top().st,sizeof(A.top().st)); temp4.pos=A.top().pos; temp4.path=A.top().path; temp4.h=A.top().h; if (A.top().pos!=2 && A.top().pos!=5 && A.top().pos!=8) { temp4.pos=A.top().pos+1; if (A.top().pos==8) temp4.h++; else if (A.top().pos+1==8) temp4.h--; if (A.top().st[temp4.pos]==A.top().pos+1) temp4.h--; else if (A.top().st[temp4.pos]==A.top().pos+2) temp4.h++; temp4.st[A.top().pos]=temp4.st[temp4.pos]; temp4.st[temp4.pos]=0; temp4.path+='r'; } A.pop(); can=Cantor(temp1.st,9); if (visit[can]!=1) { temp1.g=temp1.path.length(); A.push(temp1); visit[can]=1; } can=Cantor(temp2.st,9); if (visit[can]!=1) { temp2.g=temp2.path.length(); A.push(temp2); visit[can]=1; } can=Cantor(temp3.st,9); if (visit[can]!=1) { temp3.g=temp3.path.length(); A.push(temp3); visit[can]=1; } can=Cantor(temp4.st,9); if (visit[can]!=1) { temp4.g=temp4.path.length(); A.push(temp4); visit[can]=1; } } if (A.empty()) cout << "unsolvable" << endl; else cout << A.top().path << endl; } return 0; }
相关文章推荐
- Poj 1077 Eight 八数码
- poj 1077 八数码(BFS+康托展开)
- 八数码A*【POJ-1077 HDU-1043】
- poj 1077 八数码难题
- poj-1077-Eight
- POJ - 1077 - 康托展开
- POJ 1077 Eight
- HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)
- poj 1077 Eight
- POJ 1077 Eight (正向BFS + 康托展开)
- POJ 1077 eight DBFS
- POJ 1077 Eight(八数码A*+IDA*)
- POJ 1077 Eight
- POJ 1077 Eight【广搜】
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
- poj 1077 hdu 1043 八数码
- POJ 1077 八数码难题
- POJ 1077 Eight(BFS + A* + 逆序对剪枝)
- POJ1077——Eight
- POJ 1077 Eight(BFS八数码问题)