Hdu 4531 The story of the series of the big diversion

Source: Internet
Author: User
Tags bool cas hash sprintf

Topic Link:

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

This search problem is very disgusting ... It was not written at the game.

The first problem to be solved is how to judge the condition, that is, all the same colors are connected together. I use the simplest and most rubbing method, according to the top and bottom of the order for each Small triangle label 1~36, and then establish an adjacency matrix diagram, and then BFS judge.

Then there is the main part of the violent enumeration, each with a choice of 12 state transitions, starting with DFS, but exploding stacks. Then changed into BFS, and various tle. And then it's constantly optimizing.

The status of a weight can be represented by an integer.

The initial marking of the matrix is:

1 2 3

4 5 6

7 8 9

Then its state is 123456789. A hashing method is used.

Code:

#include <cstdio> #include <cstring> #include <vector> #include <cctype> using namespace std;  
     
typedef int Int64; const int hashsize = 1000003;     Hash value dimensions, as appropriate, const int maxstate = 400000;  
     
Total number of const int maxque = 500000 required to be saved;  
int g[40][40];  
int mat[3][3];  
Char str[12][6];  
BOOL vis[40], alpha[150];  
BOOL Iscol[3], isrow[3];  
int ans;  
     
     
vector<int>g[40];    
        Template<typename type> class hash{public:void init () {rear=1;  
        Memset (Head,-1, sizeof (head));  
    memset (state,-1, sizeof (state));  
        int Insert (Type s) {int h = hash (s), u = head[h];  
        while (U!=-1) {if (state[u] = = s) returns U;//Return to location u = next[u];  
        } State[rear] = s;  
        Next[rear] = head[h];  
        HEAD[H] = rear++;  return rear-1; Returns the newly inserted position} int find (Type s) {int h = hash(s), u = head[h];  
        while (U!=-1) {if (state[u] = = s) returns U;//Return to location u = next[u];  
    return 0;  
        } private:int Hash (Type x) {int d = ((X&0X7FFFFFFF)%hashsize);  
    return D;  
    } private:int Head[hashsize], next[maxstate], rear;  
Type State[maxstate];  
};  
     
     
hash<int64>hash;  
     
inline void init ();  
    struct node{int St;  
int cnt;  
     
     
}q[maxque];  
int que[100];  
Char s[40];  
    inline bool BFs (int st) {int pos=35;  
        while (st>0) {int p = st%10;  
        for (int k=3; k>=0;--k) s[pos--] = str[p][k];  
    St/= 10;  
    } memset (alpha, 0, sizeof (Alpha));  
     
    memset (Vis, 0, sizeof (VIS));  
        for (int i=0; i<36; ++i) if (!vis[i+1]) {if (alpha[s[i)]) return false;  
        Vis[i+1] = true;  
        int front=0, rear=1;  
        Que[0] = i+1;  
 while (front < rear) {           int u = que[front++];  
                for (int j=0; j<g[u].size (); ++j) {int v = g[u][j];  
                    if (!vis[v] && s[v-1]==s[u-1]) {Vis[v] = true;  
                que[rear++] = v;  
    }} Alpha[s[i]] = true;  
return true;  
} Char buf[15];  
    inline int Rotaterow (int st, int row) {sprintf (buf, "%d", ST);  
    char tmp = buf[row*3];  
    BUF[ROW*3] = buf[row*3+1];  
    BUF[ROW*3+1] = buf[row*3+2];  
    BUF[ROW*3+2] = tmp;  
    SSCANF (buf, "%d", &st);  
Return St;  
    inline int Rotatecol (int st, int col) {sprintf (buf, "%d", ST);  
    char tmp = Buf[col];  
    Buf[col] = Buf[3+col];  
    Buf[3+col] = Buf[6+col];  
    Buf[6+col] = tmp;  
    SSCANF (buf, "%d", &st);  
Return St;  
    } inline void Solve () {q[0].st = 123456789;  
    q[0].cnt = 0;  
    int front=0, rear=1; Hash.insert (Q[0].st);  
     
        while (front < rear) {const node& q = q[front++];  
            if (BFS (q.st)) {ans = q.cnt;  
        return;  
        Int St;  
                for (int i=0; i<3; ++i) {if (!isrow[i)) {st = Rotaterow (q.st, i);  
                    if (!hash.find (ST)) {Hash.insert (ST);  
                    Q[rear].st = st;  
                q[rear++].cnt = q.cnt+1;  
                st = Rotaterow (St, i);  
                    if (!hash.find (ST)) {Hash.insert (ST);  
                    Q[rear].st = st;  
                q[rear++].cnt = q.cnt+1;  
                } if (!iscol[i]) {st = Rotatecol (q.st, i);  
                    if (!hash.find (ST)) {Hash.insert (ST);  
                    Q[rear].st = st;  
                q[rear++].cnt = q.cnt+1;  
     
       }         st = Rotatecol (St, i);  
                    if (!hash.find (ST)) {Hash.insert (ST);  
                    Q[rear].st = st;  
                q[rear++].cnt = q.cnt+1;  
    int main () {int T, Cas=1;  
    Init ();  
     
    scanf ("%d", &t);  
        while (t--) {int idx=1;  
        memset (isrow, 0, sizeof (isrow));  
        memset (iscol, 0, sizeof (ISCOL));  
                for (int i=0; i<3; ++i) {for (int j=0; j<3; ++j) {char& ch = str[idx][0];  
                ch = getchar ();  
                while (!isalpha (CH)) Ch=getchar ();  
                for (int k=1; k<5; ++k) str[idx][k] = GetChar ();  
                if (str[idx][4] = = ' 1 ') isrow[i]=iscol[j]=true;  
            MAT[I][J] = idx++;  
        } hash.init ();  
        ans = 100000000;  
Solve ();        printf ("Case #%d:%d\n", cas++, ans);  
return 0;  
    }//============================= inline void init () {memset (g, 0, sizeof (g)); G[1][3]=1, G[1][4]=1, G[5][7]=1, G[5][8]=1, G[9][11]=1, G[9][12]=1, G[13][2]=1, G[13][15]=1, g[13][16 ]=1, G[17][6]=1, G[17][19]=1, G[17][20]=1, G[21][10]=1, G[21][23]=1, G[21][24]=1, G[25][14]=1, g[25][   
     
    27]=1, G[25][28]=1, G[29][18]=1, G[29][31]=1, G[29][32]=1, G[33][22]=1, G[33][35]=1, G[33][36]=1, G[2][3]=1, G[2][4]=1, G[2][13]=1, G[6][7]=1, G[6][8]=1, G[6][17]=1, G[10][11]=1, G[10][12]=1, g[10][21]=1 , G[14][15]=1, G[14][16]=1, G[14][25]=1, G[18][19]=1, G[18][20]=1, G[18][29]=1, G[22][23]=1, g[22][24   
     
    ]=1, G[22][33]=1, G[26][27]=1, G[26][28]=1, G[30][31]=1, G[30][32]=1, G[34][35]=1, G[34][36]=1, G[3][1]=1, G[3][2]=1, G[15][13]=1, G[15][14]=1, G[27] [25]=1, G[27][26]=1, G[7][6]=1, G[7][5]=1, G[7][4]=1, G[19][18]=1, G[19][17]=1, G[19][16]=1, g[31][30   
    ]=1, G[31][29]=1, G[31][28]=1, G[11][10]=1, G[11][9]=1, G[11][8]=1, G[23][22]=1, G[23][21]=1, G[23][20]=1, G[35][34]=1, G[35][33]=1, G[35][32]=1, G[4][2]=1, G[4][1]=1, G[4][7]=1, G[16][14]=1, G[16][13]=1, g[16 ][19]=1, G[28][26]=1, G[28][25]=1, G[28][31]=1, G[8][6]=1, G[8][5]=1, G[8][11]=1, G[20][18]=1, g[20][ 17]=1, G[20][23]=1, G[32][30]=1, G[32][29]=1, G[32][35]=1, G[12][10]=1, G[12][9]=1, G[24][22]=1, g[24  
     
    ][21]=1, G[36][34]=1, g[36][33]=1;  
        for (int i=1; i<=36; ++i) {g[i].clear ();  
        for (int j=1; j<=36; ++j) if (g[i][j)) {g[i].push_back (j); }  
    }  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.