题目描述:
八方块移动游戏要求从一个含8个数字(用1-8表示)的方块以及一个空格方块(用0表示)的3x3矩阵的起始状态开始,不断移动该空格方块以使其和相邻的方块互换,直至达到所定义的目标状态。空格方块在中间位置时有上、下、左、右4个方向可移动,在四个角落上有2个方向可移动,在其他位置上有3个方向可移动。例如,假设一个3x3矩阵的初始状态为:
8 0 3
2 1 4
7 6 5
目标状态为:
1 2 3
8 0 4
7 6 5
则一个合法的移动路径为:
8 0 3 8 1 3 8 1 3 0 1 3 1 0 3 1 2 3
2 1 4 => 2 0 4 => 0 2 4 => 8 2 4 => 8 2 4 => 8 0 4
7 6 5 7 6 5 7 6 5 7 6 5 7 6 5 7 6 5
8 0 3
2 1 4
7 6 5
目标状态为:
1 2 3
8 0 4
7 6 5
则一个合法的移动路径为:
8 0 3 8 1 3 8 1 3 0 1 3 1 0 3 1 2 3
2 1 4 => 2 0 4 => 0 2 4 => 8 2 4 => 8 2 4 => 8 0 4
7 6 5 7 6 5 7 6 5 7 6 5 7 6 5 7 6 5
另外,在所有可能的从初始状态到目标状态的移动路径中,步数最少的路径被称为最短路径;在上面的例子中,最短路径为5。如果不存在从初试状态到目标状态的任何路径,则称该组状态无解。
请设计有效的(细节请见评分规则)算法找到从八方块的某初试状态到某目标状态的所有可能路径中的最短路径,并用C/C++实现。
输入数据:
程序需读入已被命名为start.txt的初始状态和已被命名为goal.txt的目标状态,这两个文件都由9个数字组成(0表示空格,1-8表示8个数字方块),每行3个数字,数字之间用空格隔开。
输出数据:
如果输入数据有解,输出一个表示最短路径的非负的整数;如果输入数据无解,输出-1。
自测用例:
评分规则:
1)我们将首先使用和自测用例不同的10个start.txt以及相同的goal.txt,每个测试用例的运行时间在一台Intel Xeon 2.80GHz 4 CPU/6G 内存的Linux机器上应不超过10秒(内存使用不限制),否则该用例不得分;
2)每个选手的总分(精确到小数点后6位)=10秒钟内能产生正确结果的测试用例数量x10+(1/产生这些正确结果的测试用例的平均运行毫秒);
3)如果按此评分统计仍不能得出总决赛将决出的一、二、三等奖共计九名获奖者,我们将先设N=2,然后重复下述过程直至产生最高的9位得分:用随机生成的另外10个有解的start.txt再做测试,并对这10*N个测试用例用2)中公式重新计算总分,N++。
//ACRush的代码
#include <stdio.h> #include <stdlib.h> #include <string.h> const int hashsize=70001; const int maxnode=50000; const int maxp=40; const int ten[]={1,10,100,1000,10000,100000,1000000,10000000,100000000}; const int C[]={2,3,2,3,4,3,2,3,2}; const int EP[][4]={{1,3,0,0},{0,2,4,0},{1,5,0,0},{0,4,6,0},{1,3,5,7},{2,4,8,0},{3,7,0,0},{4,6,8,0},{5,7,0,0}}; struct Tlist { int data,d; Tlist *next; }; struct Thashpoint { int data; Thashpoint *next; }; //Memory int ID; Tlist listM[maxnode],*q; Thashpoint hashM[maxnode],*p; //data int src,dest; //heap Tlist *head[maxp],*expand[maxp],*lp1,*lp2; //Hash Thashpoint *hash[hashsize]; //expand int nowp,A[9],arcT[9],dist[9][9],b,depth,swap[9][9]; int data,G,newdata,newG; bool find_answer; void readdata(const char *filename,int &data) { int i,v; FILE *f=fopen(filename,"r"); data=0; for (i=0;i<9;i++) { fscanf(f,"%d",&v); data=data+v*ten[i]; } fclose(f); } bool check_noanswer() { int p[9],i,b1,b2; bool vis[9]; for (i=0;i<9;i++) p[i]=arcT[src/ten[i]%10]; for (b1=0; src/ten[b1]%10!=0;b1++); for (b2=0;dest/ten[b2]%10!=0;b2++); int countP=0; memset(vis,false,sizeof(vis)); for (i=0;i<9;i++) if (!vis[i]) { countP++; for (int k=i;!vis[k];k=p[k]) vis[k]=true; } return (countP-dist[b1][b2])%2==0; } void preprocess() { ID=0; find_answer=false; memset(hash,0,sizeof(hash)); memset(head,0,sizeof(head)); memset(expand,0,sizeof(expand)); for (int k=0;k<9;k++) arcT[dest/ten[k]%10]=k; for (int u=0;u<9;u++) for (int v=0;v<9;v++) { dist[u][v]=abs(u/3-v/3)+abs(u%3-v%3); swap[u][v]=ten[u]-ten[v]; } } void addnode() { if (newdata==dest) { printf("%d/n",depth); find_answer=true; return; } int address=newdata%hashsize; for (p=hash[address];p!=NULL;p=p->next) if (p->data==newdata) return; if (ID==maxnode) return; p=&hashM[ID]; p->data=newdata; p->next=hash[address]; hash[address]=p; q=&listM[ID]; ID++; q->data=newdata; q->d=depth; if (newG>=maxp) return; if (newG==nowp) { q->next=expand[depth]; expand[depth]=q; } else { q->next=head[newG]; head[newG]=q; } } void solve() { nowp=-1; newdata=src; newG=0; for (int k=0;k<9;k++) if (src/ten[k]%10!=0) newG+=dist[arcT[src/ten[k]%10]][k]; depth=0; addnode(); if (find_answer) return; for (int p=0;p<maxp;p++) if (head[p]!=NULL) { nowp=p; for (lp1=head[p];lp1!=NULL;lp1=lp2) { lp2=lp1->next; lp1->next=expand[lp1->d]; expand[lp1->d]=lp1; } for (int d=0;d<=p;d++) for (;expand[d]!=NULL;) { data=expand[d]->data; G=p-expand[d]->d; depth=expand[d]->d+1; expand[d]->d=-2; expand[d]=expand[d]->next; for (b=0;data/ten[b]%10!=0;b++); for (int v=0;v<C[b];v++) { int u=EP[b][v]; int c=data/ten[u]%10; newdata=data+swap[b][u]*c; c=arcT[c]; newG=depth+G-dist[c][u]+dist[c][b]; addnode(); if (find_answer) return; } } } printf("-1/n"); } int main() { readdata("start.txt",src); readdata("goal.txt",dest); preprocess(); if (check_noanswer()) printf("-1/n"); else solve(); return 0; }
IMACS的代码
#include <stdio.h> #include <string.h> struct Node { char d[9]; char p; }; int p11,p12,p21,p22; int step1,step2; Node q1[181500],q2[181500]; Node start,target; char flag[362880]={0}; int v_target,v_start; int ans; int power[]={1,1,2,6,24,120,720,5040,40320}; int movpn[9]={2,3,2,3,4,3,2,3,2}; int movp[9][4]={{1,3},{2,0,4},{5,1},{4,0,6},{5,1,3,7},{8,2,4},{7,3},{8,4,6},{5,7}}; void readdata(Node &p) { int x; for(int i=0;i<9;i++) { scanf("%d",&x); p.d[i]=x; if(x==0) p.p=i; } } void read() { freopen("start.txt","r",stdin); readdata(start); fclose(stdin); freopen("goal.txt","r",stdin); readdata(target); fclose(stdin); } int hash(Node &p) { int ret=0; int i,j; for(i=1;i<9;i++) for(j=0;j<i;j++) if(p.d[i]>p.d[j]) ret+=power[i]; return ret; } int hash2(Node &p) { int ret=0; int i,j; for(i=1;i<9;i++) for(j=0;j<i;j++) if(p.d[i]>p.d[j]) ret++; return ret&1; } void movesp(Node &p,Node &q) { int x; q=p; while(q.p!=8) { x=movp[q.p][0]; q.d[q.p]=q.d[x]; q.d[x]=0,q.p=x; } } bool nosol() { Node s1,t1; movesp(start,s1); movesp(target,t1); return hash2(s1)!=hash2(t1); } bool work1() { int p,sp,x,v,i,tmp; step1++; tmp=p12; for(p=p12;p>=p11;p--) { sp=q1[p].p; for(i=0;i<movpn[sp];i++) { x=movp[sp][i]; p12++; q1[p12]=q1[p]; q1[p12].d[sp]=q1[p].d[x]; q1[p12].d[x]=0,q1[p12].p=x; v=hash(q1[p12]); if(flag[v]==0) { flag[v]=1; } else if(flag[v]>0) p12--; else if(flag[v]<0) { ans=step1+step2; return 1; } } } p11=tmp+1; return 0; } bool work2() { int p,sp,x,v,i,tmp; step2++; tmp=p22; for(p=p22;p>=p21;p--) { sp=q2[p].p; for(i=0;i<movpn[sp];i++) { x=movp[sp][i]; p22++; q2[p22]=q2[p]; q2[p22].d[sp]=q2[p].d[x]; q2[p22].d[x]=0,q2[p22].p=x; v=hash(q2[p22]); if(flag[v]==0) { flag[v]=-1; } else if(flag[v]<0) p22--; else if(flag[v]>0) { ans=step1+step2; return 1; } } } p21=tmp+1; return 0; } int solve() { v_start=hash(start); v_target=hash(target); if(v_start==v_target) return 0; if(nosol()) return -1; p11=p12=0,q1[0]=start,flag[v_start]=1,step1=0; p21=p22=0,q2[0]=target,flag[v_target]=-1,step2=0; while(p11<=p12 || p21<=p22) { if(p11<=p12) if(work1()) return ans; if(p21<=p22) if(work2()) return ans; } return -1; } int main() { read(); printf("%d/n",solve()); return 0; } //#include <iostream> #include <cstdio> #include <memory> using namespace std; struct map { int Gap; int Board[9]; }; const int max_len = 20000; const int hash_size = 59999; int SLen, DLen, SCursor, DCursor, SStep, DStep; int SQ[max_len], DQ[max_len]; char SGaps[max_len], DGaps[max_len]; char Hash[hash_size]; int HashKeys[hash_size]; map SMap, DMap; int SKey, DKey; inline char& hash(int V) { int Key = V * 4 % hash_size; V++; while (HashKeys[Key] != 0 && HashKeys[Key] != V) { Key++; if (Key == hash_size) Key = 0; } HashKeys[Key] = V; return Hash[Key]; } inline int convert(int P[]) { int I; int Key = 0; for (I = 8; I >= 0; I--) if (P[I] != 0) { Key <<= 3; Key |= P[I] - 1; } return Key; } bool search(int& FCursor, int& FLen, int Q[], char Gaps[], int Dir) { int Cursor = FCursor; int Len = FLen; int Limit = Len; while (Cursor < Limit) { int Key = Q[Cursor]; char Gap = Gaps[Cursor]; int Delta = Gap + Gap + Gap; if (Gap < 6) { int Key2 = Key & ~(511 << Delta) | ((Key & (63 << Delta)) << 3) | ((Key & (7 << (Delta + 6))) >> 6); char Gap2 = Gap + 3; char& H = hash(Key2 * 9 + Gap2); if ((H & Dir) != Dir) if ((H |= Dir) == 3) return true; else { Q[Len] = Key2; Gaps[Len++] = Gap2; } } if (Gap >= 3) { int Key2 = Key & ~(511 << (Delta - 9)) | ((Key & (63 << (Delta - 6))) >> 3) | ((Key & (7 << (Delta - 9))) << 6); char Gap2 = Gap - 3; char& H = hash(Key2 * 9 + Gap2); if ((H & Dir) != Dir) if ((H |= Dir) == 3) return true; else { Q[Len] = Key2; Gaps[Len++] = Gap2; } } if (Gap != 0 && Gap != 3 && Gap != 6) { int Key2 = Key; char Gap2 = Gap - 1; char& H = hash(Key2 * 9 + Gap2); if ((H & Dir) != Dir) if ((H |= Dir) == 3) return true; else { Q[Len] = Key2; Gaps[Len++] = Gap2; } } if (Gap != 2 && Gap != 5 && Gap != 8) { int Key2 = Key; char Gap2 = Gap + 1; char& H = hash(Key2 * 9 + Gap2); if ((H & Dir) != Dir) if ((H |= Dir) == 3) return true; else { Q[Len] = Key2; Gaps[Len++] = Gap2; } } Cursor++; } FCursor = Cursor; FLen = Len; return false; } void read_map(map& Map) { int I; for (I = 0; I < 9; I++) { scanf(" %d", &Map.Board[I]); if (Map.Board[I] == 0) Map.Gap = I; } } void perform() { freopen("start.txt", "r", stdin); read_map(SMap); freopen("goal.txt", "r", stdin); read_map(DMap); SKey = convert(SMap.Board); DKey = convert(DMap.Board); if (SMap.Gap == DMap.Gap && SKey == DKey) { printf("0/n"); return; } hash(SKey * 9 + SMap.Gap) |= 1; hash(DKey * 9 + DMap.Gap) |= 2; SLen = SCursor = DLen = DCursor = 0; SQ[SLen] = SKey; SGaps[SLen++] = SMap.Gap; DQ[DLen] = DKey; DGaps[DLen++] = DMap.Gap; SStep = 0; DStep = 0; while (SStep < 16 || DStep < 16) { if (SCursor < SLen && DCursor < DLen && SStep <= DStep || DCursor >= DLen && SCursor < SLen) if (search(SCursor, SLen, SQ, SGaps, 1)) { printf("%d/n", SStep + DStep + 1); return; } else SStep++; else if (DCursor < DLen) if (search(DCursor, DLen, DQ, DGaps, 2)) { printf("%d/n", SStep + DStep + 1); return; } else DStep++; else break; } printf("-1/n"); } int main() { perform(); return 0; }