HDU 3567 Eight II play table, Cantor unfold, bfs,g++ submit can be over C + + not too difficult: 3

Source: Internet
Author: User
Tags recode

http://acm.hdu.edu.cn/showproblem.php?pid=3567

Compared to eight, it seems that the target State has been changed from certain to indeterminate, but Cantor expansion + Manhattan for H value of a * and ida* are not, but also difficult to control the dictionary order

In other words, although there are many starting states, but which one is 1, which one is 2 is not the most important, the most important is the corresponding to the target State, so you can re-encode the starting state to "12345678" this form (regardless of x), then the target State also corresponds to the past, when considering X, We can assume that there are only 9 starting states, each of which is ' X ' in each position

Using BFS, you can get the shortest and smallest dictionary-order transfer method for each starting state to the corresponding target State, and if you record a string for each state, you will need to make a note of it, so only the actions needed to record the previous state to that state transition are required.

Finally, for each target state, reverse the state transfer sequence, then reverse the output to

PS: Processing 3.6e6 states at a time, if not queue or loop array will be MLE, if not g++ commits and submitted in C + + will be tle

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cctype >using namespace Std;const int base[9]={1,1,2,6,24,120,720,5040,40320};const int dx[4]={1,0,0,-1};/transfer direction,        Conforms to the dictionary order minimum const int DY[4]={0,-1,1,0};CONST int maxh=362880;//The number of States corresponding to each start state int des;//is used to store the target State hash value and determine if no transfer struct is required pnt{        int maz[3][3];        int ind;//marks the starting state of the number int x, y;        int hashcode;        PNT () {x=y=hashcode=-1;                                } PNT (char * str) {for (int i=0;i<9;i++) {if (str[i]== ' X ') {                                maz[i/3][i%3]=9;                                X=I/3;                                y=i%3;                        Ind=i;                } else maz[i/3][i%3]=str[i]-' 0 ';                }} int GetHashCode () {int ans=0;                        for (int i=0;i<9;i++) {int cnt=0; For(int j=0;j<i;j++)                                {if (maz[j/3][j%3]>maz[i/3][i%3]) {cnt++;                }} ans+=base[i]*cnt;        } return Hashcode=ind*maxh+ans; }};bool in (int tx,int ty) {return tx>=0&&tx<3&&ty>=0&&ty<3;} The int vis[maxh*10+1];//is used to store whether a state has been traversed and stored by what operation gets the state int pre[maxh*10+1];//is used to store a state transferred by what state to get queue<pnt> que;char ans [maxh*10+1];//Store answer in reverse order Char buff[300];//store input PNT s,e;int ind[10],indlen;//used to recode bool input () {if (scanf ("%s", Buff)!=1) r        Eturn false;                        indlen=0;//recode for (int i=0;i<9;i++) {if (buff[i]>= ' 0 ' &&buff[i]<= ' 9 ') {                        S.maz[i/3][i%3]=++indlen;                ind[buff[i]-' 0 ']=indlen;                        } else{s.maz[i/3][i%3]=9;                     S.X=I/3;   s.y=i%3;                S.ind=i;        }} if (scanf ("%s", buff)!=1) return false; for (int i=0;i<9;i++) {if (buff[i]>= ' 0 ' &&buff[i]<= ' 9 ') {e.maz[i/3][i%                3]=ind[buff[i]-' 0 '];                        } else{e.maz[i/3][i%3]=9;                        E.X=I/3;                e.y=i%3;        }} e.ind=s.ind;//starting state is the same des=e.gethashcode (); return true;}                void BFs () {while (!que.empty ()) {PNT tp=que.front (); Que.pop ();                int x=tp.x,y=tp.y;                        for (int i=0;i<4;i++) {int tx=x+dx[i],ty=y+dy[i];                                if (in (Tx,ty)) {PNT nw=tp;                                Swap (nw.maz[tx][ty],nw.maz[x][y]);                                Nw.x=tx,nw.y=ty;                          if (Vis[nw.gethashcode ()]==-1) {              Vis[nw.hashcode]=i;                                        Pre[nw.hashcode]=tp.gethashcode ();                                Que.push (NW);        }}}}}void init () {memset (vis,-1,sizeof (VIS));        memset (pre,-1,sizeof (pre));        PNT s1=pnt ("12345678X");//9 initial state added sequence Vis[s1.gethashcode ()]=-2;        Que.push (S1);        PNT s2=pnt ("1234567x8");        Vis[s2.gethashcode ()]=-2;        Que.push (S2);        PNT s3=pnt ("123456x78");        Vis[s3.gethashcode ()]=-2;        Que.push (S3);        PNT s4=pnt ("12345x678");        Vis[s4.gethashcode ()]=-2;        Que.push (S4);        PNT s5=pnt ("1234x5678");        Vis[s5.gethashcode ()]=-2;        Que.push (S5);        PNT s6=pnt ("123x45678");        Vis[s6.gethashcode ()]=-2;        Que.push (S6);        PNT s7=pnt ("12x345678");        Vis[s7.gethashcode ()]=-2;        Que.push (S7);        PNT s8=pnt ("1x2345678"); Vis[s8.gethashcode ()]=-2;        Que.push (S8);        PNT s9=pnt ("X12345678");        Vis[s9.gethashcode ()]=-2;        Que.push (S9); BFS ();}        int Heap[maxh],sz;int Getans (PNT e) {//reverse traverse sz=0;        int Last=e.gethashcode ();                while (pre[last]>=0) {heap[sz++]=vis[last];        Last=pre[last]; } return sz;}                        void print () {for (int. i=sz-1;i>=0;i--) {switch (Heap[i]) {case 0:                Putchar (' d ');                Case 1:putchar (' l ');                Case 2:putchar (' R ');                Case 3:putchar (' U '); }} putchar (' \ n ');}        int main () {int T;        scanf ("%d", &t);        Init ();                for (int ti=1;ti<=t;ti++) {input ();                S.hashcode=s.gethashcode ();       if (s.hashcode==des) {printf ("Case%d:0\n", TI);                 Puts ("");                Continue                } vis[s.hashcode]=-2;                int Step=getans (e);                printf ("Case%d:%d\n", ti,step);        Print (); } return 0;}

  

HDU 3567 Eight II play table, Cantor unfold, bfs,g++ submit can be over C + + not too difficult: 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.