Eight Digital problems.
Expand BFS + conto. Kanto is used to determine the weight. If you search directly, the query times out. Preprocessing is required.
I used a struct to store a state.
Struct lx {int can; // the current status of the Kanto expand int pcan; // the previous status of the Kanto expand int K; // the direction of movement };
Save all the 181442 statuses. Sort and then perform binary search. Iteration searches for the previous State until the initial 0.
At the beginning of writing, it is estimated that there is a problem with Kanto inverse expansion, which is always wa. After rewriting, it will be inexplicably 1A.
G ++ 171 Ms
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FORi(n) for(int i=0;i<n;i++)#define FORj(n) for(int j=0;j<n;j++)#define FORk(n) for(int k=0;k<n;k++)#define debug puts("==fuck==")#define acfun std::ios::sync_with_stdio(false)#define SIZE 1000+10using namespace std;struct lx{ int can; int pcan; int k;};int g[4][4]={{1,2,3},{4,5,6},{7,8,9}};int fac[]={1,1,2,6,24,120,720,5040,40320};int xx[]={0,0,-1,1};int yy[]={1,-1,0,0};lx q[200000]; //max = 181441bool vis[500000]; //max = 362879int n;int cantor(){ int tmp[10],top=0,num=0; FORi(3) FORj(3) tmp[top++]=g[i][j]; FORi(top) { int t=0; for(int j=i+1;j<top;j++) if(tmp[j]<tmp[i])t++; num+=fac[top-i-1]*t; } return num;}int uncantor(int num){ int tmp[10],top=0; bool v[10]; CLR(v,0); for(int i=1;i<=9;i++) { int t=num/fac[9-i]; num-=t*fac[9-i]; int j=1,k=0; for(;k<=t;j++) if(!v[j])k++; j--; v[j]=1; tmp[i-1]=j; } int k=0,site; FORi(3) FORj(3) { g[i][j]=tmp[k++]; if(g[i][j]==9)site=i*3+j; } return site;}void bfs(){ int h=0,e=0; q[h].can=cantor(); q[h].pcan=-1; q[h++].k=-1; CLR(vis,0); vis[0]=1; while(e
HDU 1043 eight