題目描述 : Rush Hour is a puzzle game invented by Nob Yoshigahara in the 1970s. It is now being manufactured by ThinkFun. The board is a 6 × 6 grid with grooves in the tiles to allow vehicles to slide. Cars and trucks are both one square wide, but cars are two squares long and trucks are three squares long. Vehicles can only be moved forward or backward along a straight line on the grid. The goal of the game is to get the only red car totally out through the exit of the board by moving the other vehicles out of its way. Figure 1 gives an example of Rush Hour puzzle.
Figure 1: An example of Rush Hour puzzle. We give each vehicle of a puzzle a unique id, numbered from 1 to the number of vehicles, in which the red car’s id is 1. The board information of a puzzle is represented by a 6 × 6 matrix,named board matrix. Each entry of a board matrix is the id of the vehicle placed on that groove, and the entries are filled with 0 if there exists no vehicle on those grooves. The exit of the board is located at the right end side of the 3rd row. Figure 2 shows the board matrix corresponding to the puzzle in Figure 1. Moving a piece (car or truck) by one unit (a groove) is called a step. A puzzle is easy if it can be solved (the red car totally out through the exit of the board) in no more than 10 steps. Please write a program to judge whether a puzzle is easy or not.
Figure 2: The board matrix corresponding to the puzzle in Figure 1.
輸入 The input contains 6 lines, each line indicates the content (6 integers separated by a blank) of each row of a board matrix. 輸出 Output the minimum number of steps for solving the input puzzle if the puzzle is easy, otherwise output -1. 樣例輸入 0 2 0 6 6 0 0 2 0 0 7 0 0 3 1 1 7 0 0 3 4 4 8 0 0 5 5 5 8 0 0 0 0 0 0 0 樣例輸出 6 提示 ? There are at most 10 vehicles on the board for each puzzle. ? Only the red car can be moved out of the board for each puzzle. 題目大意:車的長度由2和3,連續的非零數字代表一輛車,現在要把“1”代表的車移出去(出口固定在第三行末尾),一次移動一格,問把車移動出去的最小次數(注意移出去還要2次),若答案不存在或在10步以上,則輸出-1。 表面意思,bfs加矩陣哈希(我用了map處理)。但我保證,這是我寫過最長+最長時間的bfs搜索。。。
#pragma GCC optimize(3,"Ofast","inline")#pragma G++ optimize(3)#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<fstream>#include<algorithm>#include<cmath>#include<deque>#include<vector>#include<queue>#include<string>#include<cstring>#include<map>#include<stack>#include<set>#include<sstream>
using namespace std;typedeflonglong ll;typedefunsignedlonglong ull;typedef pair<ll,ll> pll;typedef pair<int,int> pii;typedef queue<int> q_i;typedef queue<string> q_s;typedef queue<double> q_d;typedef queue<ll> q_ll;typedef queue<char> q_c;typedef priority_queue<int> pq_i;typedef priority_queue<string> pq_s;typedef priority_queue<double> pq_d;typedef priority_queue<ll> pq_ll;typedef stack<int> s_i;typedef stack<string> s_s;typedef stack<double> s_d;typedef stack<ll> s_ll;typedef stack<char> s_c;typedef map<ll,ll> m_ll_ll;typedef map<int,ll> m_i_ll;typedef map<int,int> m_i_i;typedef map<string,ll> m_s_ll;typedef map<char,int> m_c_i;typedef map<char,ll> m_c_ll;const ll INF=0x3f3f3f3f;#define rep(i,l,r) for(ll i=l;i<=r;i++)#define per(i,l,r) for(ll i=r;i>=l;i--)#define eif else if#define N 100005#define mm(dp) memset(dp,0,sizeof(dp))#define mm1(dp) memset(dp,-1,sizeof(dp))#define mm2(dp) memset(dp,0x3f,sizeof(dp))#define IT set<int>::iterator#define fs(n) fixed<< setprecision(n)//const double e=2.71828182845;#define max(a,b) a>b?a:b#define min(a,b) a<b?a:bconstdouble pi =acos(-1.0);constint p=1e9+7;inline ll read(){ll s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();return s*w;}inlinevoidwrite(ll x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');}floatSqrtByCarmack(float number ){int i;float x2, y;constfloat threehalfs =1.5F;x2 = number *0.5F;y = number;i =*(int*)&y;i =0x5f375a86-( i >>1);y =*(float*)&i;y = y *( threehalfs -( x2 * y * y ));y = y *( threehalfs -( x2 * y * y ));y = y *( threehalfs -( x2 * y * y ));return number*y;}
ll qpow(ll a,ll b,ll mod){ll sum=1;while(b){if(b%2==1){sum=sum*a%mod;}b/=2;a=a*a%mod;}return sum;}interfen(int*a,int start,int endd,int l)//小于等于l的最大值的角標{int mid=(start+endd)/2;if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))return mid;elseif(a[mid]<=l)returnerfen(a,mid+1,endd,l);elseif(a[mid]>l){if(start!=mid)returnerfen(a,start,mid,l);elsereturn start-1;}}
ll prime[6]={2,3,5,233,331};
ll qmul(ll x, ll y, ll mod){return(x * y -(longlong)(x /(longdouble)mod * y +1e-3)*mod + mod)% mod;}
bool Miller_Rabin(ll p){if(p <2)return0;if(p !=2&& p %2==0)return0;ll s = p -1;while(!(s &1))s >>=1;for(int i =0; i <5;++i){if(p == prime[i])return1;ll t = s, m =qpow(prime[i], s, p);while(t != p -1&& m !=1&& m != p -1){m =qmul(m, m, p);t <<=1;}if(m != p -1&&!(t &1))return0;}return1;}
ll gcd(ll x,ll y){if(y==0)return x;elsereturngcd(y,x%y);}typedefstruct{int a[7][7];int nm;}STU;typedefstruct{int a[7][7];}STU1;
STU stu;
queue<STU>que;
map<string,int>mapp;intmain(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int cnt=0;int u[15];mm(u);int kk=0;rep(i,1,6){rep(j,1,6){cin>>stu.a[i][j];//stu1.a[i][j]=stu.a[i][j];u[stu.a[i][j]]++;cnt=max(cnt,stu.a[i][j]);}}int flag=0;stu.nm=0;que.push(stu);while(!que.empty()){STU stu0=que.front();kk++;// cout<<endl;// rep(i,1,6)// {// rep(j,1,6)// {// cout<<stu0.a[i][j]<<" ";// }// cout<<endl;// }// cout<<endl;que.pop();if(stu0.a[3][6]==1&&stu0.a[3][5]==1){if(u[1]==2)cout<<stu0.nm+2<<'\n';elsecout<<stu0.nm+3<<'\n';flag=1;break;}rep(k,1,cnt){if(u[k]==2){int v=0;int flagx1=0;int flagy1=0;int flagx2=0;int flagy2=0;rep(i,1,6){rep(j,1,6){if(stu0.a[i][j]==k&&v==0){flagx1=i;flagy1=j;v++;}eif(stu0.a[i][j]==k){flagx2=i;flagy2=j;v++;break;}}if(v==2)break;}//if(k==2)//cout<<flagy1<<" "<<flagy2<<endl;if(flagy1==flagy2){int xx1=min(flagx1,flagx2);int xx2=max(flagx1,flagx2);int yy=flagy1;//cout<<xx1-1<<endl;if(xx1-1>0){if(stu0.a[xx1-1][yy]==0){//cout<<xx1-1<<" "<<yy<<" "<<k<<endl;STU stu1;rep(p,1,6){rep(q,1,6){if(q==yy)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==xx1-1||p==xx1){stu1.a[p][yy]=stu0.a[p+1][yy];}eif(p==xx2){stu1.a[p][yy]=0;}elsestu1.a[p][yy]=stu0.a[p][yy];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}if(xx2+1<7){if(stu0.a[xx2+1][yy]==0){//cout<<xx2+1<<" "<<yy<<" "<<k<<endl;STU stu1;rep(p,1,6){rep(q,1,6){if(q==yy)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==xx2||p==xx2+1){stu1.a[p][yy]=stu0.a[p-1][yy];}eif(p==xx1){stu1.a[p][yy]=0;}elsestu1.a[p][yy]=stu0.a[p][yy];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}}else{int yy1=min(flagy1,flagy2);int yy2=max(flagy1,flagy2);int xx=flagx1;if(yy1-1>0){if(stu0.a[xx][yy1-1]==0){STU stu1;rep(p,1,6){rep(q,1,6){if(p==xx)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==yy1-1||p==yy1){stu1.a[xx][p]=stu0.a[xx][p+1];}eif(p==yy2){stu1.a[xx][p]=0;}elsestu1.a[xx][p]=stu0.a[xx][p];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}if(yy2+1<7){if(stu0.a[xx][yy2+1]==0){STU stu1;rep(p,1,6){rep(q,1,6){if(p==xx)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==yy2||p==yy2+1){stu1.a[xx][p]=k;}eif(p==yy1){stu1.a[xx][p]=0;}elsestu1.a[xx][p]=stu0.a[xx][p];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}}}else{int v=0;int flagx1=0;int flagy1=0;int flagx2=0;int flagy2=0;int flagx3=0;int flagy3=0;rep(i,1,6){rep(j,1,6){if(stu0.a[i][j]==k&&v==0){flagx1=i;flagy1=j;v++;}eif(stu0.a[i][j]==k&&v==1){flagx2=i;flagy2=j;v++;}eif(stu0.a[i][j]==k&&v==2){flagx3=i;flagy3=j;v++;}if(v==3)break;}if(v==3)break;}if(flagy1==flagy2){int xx1=min(flagx1,flagx2);xx1=min(xx1,flagx3);int xx2=max(flagx1,flagx2);xx2=max(xx2,flagx3);int yy=flagy1;if(xx1-1>0){if(stu0.a[xx1-1][yy]==0){//cout<<xx1-1<<" "<<yy<<" "<<k<<endl;STU stu1;rep(p,1,6){rep(q,1,6){if(q==yy)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==xx1-1||p==xx1||p==xx1+1){stu1.a[p][yy]=stu0.a[p+1][yy];}eif(p==xx2){stu1.a[p][yy]=0;}elsestu1.a[p][yy]=stu0.a[p][yy];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}if(xx2+1<7){if(stu0.a[xx2+1][yy]==0){//cout<<xx2+1<<" "<<yy<<" "<<k<<endl;STU stu1;rep(p,1,6){rep(q,1,6){if(q==yy)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==xx2||p==xx2+1||p==xx2-1){stu1.a[p][yy]=k;}eif(p==xx1){stu1.a[p][yy]=0;}elsestu1.a[p][yy]=stu0.a[p][yy];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}}else{int yy1=min(flagy1,flagy2);yy1=min(yy1,flagy3);int yy2=max(flagy1,flagy2);yy2=max(yy2,flagy3);int xx=flagx1;if(yy1-1>0){if(stu0.a[xx][yy1-1]==0){STU stu1;rep(p,1,6){rep(q,1,6){if(p==xx)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==yy1-1||p==yy1||p==yy1+1){stu1.a[xx][p]=k;}eif(p==yy2){stu1.a[xx][p]=0;}elsestu1.a[xx][p]=stu0.a[xx][p];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}if(yy2+1<7){if(stu0.a[xx][yy2+1]==0){STU stu1;rep(p,1,6){rep(q,1,6){if(p==xx)continue;elsestu1.a[p][q]=stu0.a[p][q];}}rep(p,1,6){if(p==yy2||p==yy2+1||p==yy2-1){stu1.a[xx][p]=k;}eif(p==yy1){stu1.a[xx][p]=0;}elsestu1.a[xx][p]=stu0.a[xx][p];}stu1.nm=stu0.nm+1;string str="";rep(i,1,6){rep(j,1,6){char ch=(stu1.a[i][j]-'0');str=str+ch;}}if(mapp[str]==0){mapp[str]=1;if(u[1]==2){if(stu1.nm<=8)que.push(stu1);}else{if(stu1.nm<=7)que.push(stu1);}}}}}}}}if(flag==0)cout<<-1<<'\n';return0;}