1. 程式人生 > >HDU 6202 cube cube cube (2017瀋陽網賽

HDU 6202 cube cube cube (2017瀋陽網賽

題意:

給你一個八面八軸的魔方, 問你是否 3步內還原。

思路:

真的太噁心的一個題目。。。。

其實理清了 很 簡單, 雖然寫起來很麻煩。

看題目中的圖片描述的話, 底面是可以轉的, 且有八個面, 首先就有8種旋轉底面的操作。

圖中還介紹了旋轉中間軸, 中間軸的話 是 兩個面確定一箇中間軸, 一共8面, 因此有四個中間軸。

因此 有8 + 4 = 12 種旋轉操作, 加上逆時針, 一共24種旋轉操作。

逆時針也挺好轉, 旋轉底面的操作的逆時針 就是 順時針轉兩次。

中間軸同理, 直接寫也挺好寫。

然後模擬就行了。

但是一直WA, 本以為是旋轉出了問題(確實有問題), 但錯誤根本還在於 dfs 寫錯了。

之前一直沒注意到dfs 這個問題, 今天也通過這個題 長見識了。。。

 那就是dfs裡 開臨時陣列不能太大 太多。

因為是回溯嘛, 所以要記錄沒轉之前是啥, 但是一開就是一個72的int 陣列, 直接出不來答案。。。。。(那也應該返回TLE 把。。。)

所以為了避免重複開陣列, 直接不開好了, 轉過去, 在逆時針轉回來好了, 別記錄了。。。

貼幾幅圖紀念一下吧,。。T_T


隨便參考一下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int a[100];

bool check(){
    for (int i = 1; i <= 8; ++i){
        int st = (i-1)*9 + 1;
        for (int j = st; j <= st + 9 - 1; ++j){
            if (a[j] != a[st]) return false;
        }
    }
    return true;
}
int a2[100] ;
void rotate1(int b[], int c[]){
    int t = a[ b[6] ];
    a[b[6] ] = a[b[0] ];
    a[b[0] ] = a[b[12] ];
    a[b[12] ] = t;
    int t1 = a[b[1] ];
    int t2 = a[b[2] ];
    int t3 = a[b[3] ];
    int t4 = a[b[4] ];
    int t5 = a[b[5] ];


    for (int i = 1; i <= 5; ++i){
        a[b[i] ] = a[b[ i + 12] ];
    }
    for (int i = 13; i < 18; ++i){
        a[b[i] ] = a[b[i - 6] ];
    }
    a[b[7] ] = t1;
    a[b[8] ] = t2;
    a[b[9] ] = t3;
    a[b[10] ] = t4;
    a[b[11] ] = t5;



    for (int i = 1; i <= 72; ++i){
        a2[i] = a[i];
    }

    a[c[0] ] = a2[c[4] ];
    a[c[1] ] = a2[c[6] ];
    a[c[2] ] = a2[c[5] ];
    a[c[3] ] = a2[c[1] ];
    a[c[4] ] = a2[c[8] ];
    a[c[5] ] = a2[c[7] ];
    a[c[6] ] = a2[c[3] ];
    a[c[7] ] = a2[c[2] ];
    a[c[8] ] = a2[c[0] ];

}


void rotate2(int d[], int way){
    if (way == 0){
        int t1 = a[d[15] ];
        int t2 = a[d[16] ];
        int t3 = a[d[17] ];
        for (int i = 15; i >= 3; i -= 3){
            for (int j = i; j < i + 3; ++j){
                a[d[j] ] = a[d[j-3] ];
            }
        }
        a[d[0] ] = t1;
        a[d[1] ] = t2;
        a[d[2] ] = t3;
    }
    else {
        int t1 = a[d[0] ];
        int t2 = a[d[1] ];
        int t3 = a[d[2] ];
        for (int i = 0; i <= 12; i += 3){
            for (int j = i; j < i + 3; ++j){
                a[d[j] ] = a[d[j+3] ];
            }
        }
        a[d[15] ] = t1;
        a[d[16] ] = t2;
        a[d[17] ] = t3;
    }
}

///================================
/// 1:

///0  6  12
///23 64 9
///         0   1   2   3   4   5   6   7   8   9   10  11  12 13  14  15  16  17
int b1[] = {23, 63, 62, 58, 57, 55, 64, 37, 39, 38, 42, 41, 9, 14, 15, 16, 17, 18};
int c1[] = {50, 52, 51, 47, 54, 53, 49, 48, 46};

int b2[] = {19, 10, 12, 11, 15, 14, 54, 41, 42, 43, 44, 45, 68, 36, 35, 31, 30, 28};
int c2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

int b3[] = {1, 28, 30, 29, 33, 32, 72, 59, 60, 61, 62, 63, 50, 18, 17, 13, 12, 10};
int c3[] = {19, 20, 21, 22, 23, 24, 25, 26, 27};

int b4[] = {28, 19, 21, 20, 24, 23, 63, 50, 51, 52, 53, 54, 41, 9, 8, 4, 3, 1};
int c4[] = {10, 11, 12, 13, 14, 15, 16, 17, 18};


//////////aaaa

int b5[] = {37, 46, 48, 47, 51, 50, 18, 23, 24, 25, 26, 27, 32, 72, 71, 67, 66, 64};
int c5[] = {55, 56, 57, 58, 59, 60, 61, 62, 63};

int b6[] = {45, 68, 69, 70, 71, 72, 59, 27, 26, 22, 21, 19, 10, 1, 3, 2, 6, 5};
int c6[] = {36, 31, 35, 34, 28, 30, 29, 33, 32};

int b7[] = {36, 5, 6, 7, 8, 9, 14, 54, 53, 49, 48, 46, 55, 64, 66, 65, 69, 68};
int c7[] = {45, 40, 44, 43, 37, 39, 38, 42, 41};

int b8[] = {5, 45, 44, 40, 39, 37, 46, 55, 57, 56, 60, 59, 27, 32, 33, 34, 35, 36};
int c8[] = {68, 70, 69, 65, 72, 71, 67, 66, 64};

///=======================
///******** 0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17
int d1[] = {13, 17, 16, 52, 53, 49, 38, 39, 40, 65, 69, 70, 34, 33, 29, 22, 21, 20};
int d2[] = {13, 12, 11, 4, 8, 7, 43, 44, 40, 65, 66, 67, 56, 60, 61, 25, 24, 20};

int d3[] = {58, 62, 61, 25, 26, 22, 29, 30, 31, 2, 6, 7, 43, 42, 38, 49, 48, 47};
int d4[] = {2, 3, 4, 11, 15, 16, 52, 51, 47, 58, 57, 56, 67, 71, 70, 34, 35, 31};


void r1(){ rotate2(d1, 0);}
void r2(){rotate2(d1, 1);}
void r3(){rotate2(d2, 0);}
void r4(){rotate2(d2, 1);}

void r21(){rotate2(d3, 0);}
void r22(){rotate2(d3, 1);}
void r23(){rotate2(d4, 0);}
void r24(){rotate2(d4, 1);}


void r5(){rotate1(b1, c1);}
void r6(){rotate1(b1, c1);rotate1(b1, c1);}
void r7(){rotate1(b2, c2);}
void r8(){rotate1(b2, c2);rotate1(b2, c2);}
void r9(){rotate1(b3, c3);}
void r10(){rotate1(b3, c3);rotate1(b3, c3);}
void r11(){rotate1(b4, c4);}
void r12(){rotate1(b4, c4);rotate1(b4, c4);}

void r13(){rotate1(b5, c5);}
void r14(){rotate1(b5, c5); rotate1(b5, c5);}

void r15(){rotate1(b6, c6);}
void r16(){rotate1(b6, c6); rotate1(b6, c6);}

void r17(){rotate1(b7, c7);}
void r18(){rotate1(b7, c7); rotate1(b7, c7);}

void r19(){rotate1(b8, c8);}
void r20(){rotate1(b8, c8); rotate1(b8, c8);}
///=========================================



bool dfs(int d){
    if (d > 3) return false;
    if (check()) return true;

    ///*******************
    r1();
    if (dfs(d+1)) return true;
    r2();
    ///*******************


    ///*******************
    r2();
    if (dfs(d+1)) return true;
    r1();
    ///*******************

    ///*******************
    r3();
    if (dfs(d+1)) return true;
    r4();
    ///*******************
    ///*******************
    r4();
    if (dfs(d+1)) return true;
    r3();
    ///*******************
    ///*******************
    r5();
    if (dfs(d+1)) return true;
    r6();
    ///*******************
    ///*******************
    r6();
    if (dfs(d+1)) return true;
    r5();
    ///*******************
    ///*******************
    r7();
    if (dfs(d+1)) return true;
    r8();
    ///*******************
    ///*******************
    r8();
    if (dfs(d+1)) return true;
    r7();
    ///*******************
    ///*******************
    r9();
    if (dfs(d+1)) return true;
    r10();
    ///*******************
    ///*******************
    r10();
    if (dfs(d+1)) return true;
    r9();
    ///*******************
    ///*******************
    r11();
    if (dfs(d+1)) return true;
    r12();
    ///*******************
    ///*******************
    r12();
    if (dfs(d+1)) return true;
    r11();
    ///*******************
    ///*******************
    r13();
    if (dfs(d+1)) return true;
    r14();
    ///*******************

    ///*******************
    r14();
    if (dfs(d+1)) return true;
    r13();
    ///*******************
    ///*******************
    r15();
    if (dfs(d+1)) return true;
    r16();
    ///*******************
    ///*******************
    r16();
    if (dfs(d+1)) return true;
    r15();
    ///*******************
    ///*******************
    r17();
    if (dfs(d+1)) return true;
    r18();
    ///*******************
    ///*******************
    r18();
    if (dfs(d+1)) return true;
    r17();
    ///*******************
    ///*******************
    r19();
    if (dfs(d+1)) return true;
    r20();
    ///*******************
    ///*******************
    r20();
    if (dfs(d+1)) return true;
    r19();

    ///*******************
    ///*******************
    r21();
    if (dfs(d+1)) return true;
    r22();
    ///*******************
    ///*******************
    r22();
    if (dfs(d+1)) return true;
    r21();
    ///*******************
    ///*******************
    r23();
    if (dfs(d+1)) return true;
    r24();
    ///*******************

    ///*******************
    r24();
    if (dfs(d+1)) return true;
    r23();
    ///*******************

    return false;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int cnt = 0;
        for (int i = 0; i < 8; ++i){
            for (int j = 0; j < 9; ++j){
                int x;
                scanf("%d",&x);
                a[++cnt] = x;
            }
        }

        int ans = dfs(0);
        if (ans){
            puts("YES");
        }
        else puts("NO");
    }

    return 0;
}

cube cube cube

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 9313    Accepted Submission(s): 127


Problem Description Rubick has a strange cube. Can you restore Rubik's cube in 3 steps (including 3)?
Rubik's cube is shown in the following picture:



The plane expansion of the Rubik's cube is shown in the following figure, each number represents the color corresponding to each cube.



The following picture explains how to rotate this strange cube. If you still feel confused, you can refer to
http://www.bilibili.com/video/av8452301/?from=search&seid=11750270100959783079 .



Input The first line contains an integer T (T10), the number of test cases.
Each test case consists of 72 integers which correspond to the colors of each location of the Rubik's Cube. Each number represents one color, it's guaranteed that there are exactly 8 colors and each color appears 9 times.

Output For each test case, if you can restore the Rubik's cube in 3 steps, output "YES", else output "NO". (both without quote)

Sample Input 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8
Sample Output YES
Source
Recommend liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201