1. 程式人生 > >藍橋杯 歷屆試題 九宮重排 (bfs+康託展開去重優化)

藍橋杯 歷屆試題 九宮重排 (bfs+康託展開去重優化)

Description

如下面第一個圖的九宮格中,放著 1~8 的數字卡片,還有一個格子空著。與空格子相鄰的格子中的卡片可以移動到空格中。經過若干次移動,可以形成第二個圖所示的局面。

我們把第一個圖的局面記為:12345678.
把第二個圖的局面記為:123.46758
顯然是按從上到下,從左到右的順序記錄數字,空格記為句點。
本題目的任務是已知九宮的初態和終態,求最少經過多少步的移動可以到達。如果無論多少步都無法到達,則輸出-1。

Input

輸入第一行包含九宮的初態,第二行包含九宮的終態。

Output

輸出最少的步數,如果不存在方案,則輸出-1。

Sample Input

樣例輸入1
12345678.
123.46758

樣例輸入2
13524678.
46758123.

Sample Output

樣例輸出1
3

樣例輸出2
22

Source

藍橋杯   分析:暴力bfs會超時
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 99999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[13]= {0
,31,28,31,30,31,30,31,31,30,31,30,31}; int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};//i的階乘 LL getval() { LL ret(0); char c; while((c=getchar())==' '||c=='\n'||c=='\r'); ret=c-'0';
while((c=getchar())!=' '&&c!='\n'&&c!='\r') ret=ret*10+c-'0'; return ret; } void out(int a) { if(a>9) out(a/10); putchar(a%10+'0'); } int kt(int a[],int n)//康託展開 { int ans=0; for(int i=1;i<=n;i++) { int c=0; for(int j=i+1;j<=n;j++) { if(a[j]<a[i]) c++; } ans+=(c*fac[n-i]); } return ans+1; } char str1[15],str2[15]; int a[4][4],b[4][4]; int sx,sy; int t[10]; int h; int w; bool vis[100000000]; struct node { int x,y,step;//x,y代表空格位置 int c[4][4];//九宮格陣列 node(int xx,int yy,int ss,int cc[][4])//初始化 { x=xx; y=yy; step=ss; for(int i=1; i<=3; i++) for(int j=1; j<=3; j++) c[i][j]=cc[i][j]; } int getkt()//得到結點陣列的康託展開值 { h=1; for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { t[h++]=c[i][j]; } } return kt(t,9); } }; void init()//初始化 { int cnt=0; for(int i=1; i<=3; i++)//得到原始九宮格 { for(int j=1; j<=3; j++) { if(str1[cnt]=='.') a[i][j]=9,sx=i,sy=j; else a[i][j]=str1[cnt]-'0'; cnt++; } } cnt=0; for(int i=1; i<=3; i++)//得到目標九宮格 { for(int j=1; j<=3; j++) { if(str2[cnt]=='.') b[i][j]=9; else b[i][j]=str2[cnt]-'0'; cnt++; } } me(vis,false);//九宮格狀態陣列 h=1; for(int i=1;i<=3;i++)//得到目標九宮格的康託展開值 { for(int j=1;j<=3;j++) { t[h++]=b[i][j]; } } w=kt(t,9); } int check(int x,int y)//邊界約束 { if(x<=3&&x>=1&&y<=3&&y>=1) return 1; return 0; } int bfs(int x,int y,int a[][4]) { queue<node> q; q.push(node(x,y,0,a)); vis[node(x,y,0,a).getkt()]=1; while(!q.empty()) { int x=q.front().x; int y=q.front().y; int step=q.front().step; int c[4][4]; for(int i=1; i<=3; i++) for(int j=1; j<=3; j++) c[i][j]=q.front().c[i][j]; q.pop(); for(int i=0; i<4; i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; int ss=step+1; int cc[4][4]; if(check(xx,yy)==0)//越界 continue; for(int i=1; i<=3; i++) for(int j=1; j<=3; j++) cc[i][j]=c[i][j]; cc[x][y]=cc[xx][yy];//移動 cc[xx][yy]=9; if(vis[node(xx,yy,ss,cc).getkt()]==0)//判斷該狀態的九宮格有沒有搜尋過 { if(node(xx,yy,ss,cc).getkt()==w)//搜尋到了目標 { return ss;//返回步數 } int temp=node(xx,yy,ss,cc).getkt(); vis[temp]=1;//標記該狀態的九宮格已經搜尋過 q.push(node(xx,yy,ss,cc)); } } } return -1; } int main() { while(~scanf("%s",str1)) { scanf("%s",str2); init(); int ans=bfs(sx,sy,a); printf("%d\n",ans); } }