Problem D: [Introduction to Wide Search] Magic Board

Description

After successfully inventing the Rubik's Cube, Mr. Rubick invented its two-dimensional version, called the Magic Board. This is a magic board with 8 grids of the same size:
1 2 3 4
8 7 6 5
We know that each square of the magic board has a color. These 8 colors are represented by the first 8 positive integers. A sequence of colors can be used to represent a state of the magic board. It is stipulated that starting from the upper left corner of the magic board, integers are taken out in a clockwise direction to form a color sequence. For the state of the magic board in the above figure, we use the sequence (1, 2, 3, 4, 5, 6, 7, 8) to represent. This is the basic state.
Here are three basic operations, which are represented by capital letters "A", "B", and "C" (the state of the magic board can be changed through these operations):
"A": swap the upper and lower two lines;
"B": The rightmost column is inserted into the leftmost;
"C": the four squares in the center of the magic board rotate clockwise.
The following is a demonstration of the basic state operation:
A:
8 7 6 5
1 2 3 4
B:
4 1 2 3
5 8 7 6
C:
1 7 2 4
8 6 3 5
For each possible state, these three Basic operations can be used.
You need to program and calculate the transition from basic state to target state with a minimum of basic operations, and output a sequence of basic operations.
[Input format] There
are
only one line for entering multiple sets of test data , including 8 integers, separated by spaces (these integers are in the range of 1-8), indicating the target state.
[Output format]
Line 1: Including an integer, indicating the length of the shortest operation sequence.
Line 2: The earliest sequence of operations in lexicographic order is represented by a character string. Except for the last line, each line outputs 60 characters.

 

 

Sample Input

 

2 6 8 4 5 7 3 1

Sample Output

7

BCABCCB 

 

Final AC code:

#include <bits / stdc ++. h>
 using  namespace std;
 // The difficulty of this question is that you need to record the state that appeared before. This question uses string to indicate // The title
 stem does not indicate that it is not found, and the input sequence is the initial sequence. Situation (After testing, all BFS () return ""
 // The title stems that only 60 characters are output per line, but there is no case that exceeds 60 characters ... 
struct Node { 
     int M [ 2 ] [ 4 ]; // M save state 
    string s; // cumulative operation 
} now, nex;
 int des [ 2 ] [ 4 ];
 char op [ 3 ] = { ' A ' , ' B ' , ' C'};
map<string, bool> mp;
void getMatrix(char c){
    int i;
    if(c == 'A'){
        for(i=0; i<4; i++) nex.M[0][i] = now.M[1][i];
        for(i=0; i<4; i++) nex.M[1][i] = now.M[0][i];
    }else if(c == 'B'){
        for(i=0; i<2; i++) nex.M[i][0] = now.M[i][3];
        for(i=1; i<4; i++) nex.M[0][i] = now.M[0][i-1];
        for(i=1; i<4; i++) nex.M[1][i] = now.M[1][i-1];
    }else{
        for(i=0; i<2; i++) nex.M[i][0]=now.M[i][0], nex.M[i][3]=now.M[i][3];
        nex.M[0][1]=now.M[1][1], nex.M[0][2]=now.M[0][1];
        nex.M[1][1]=now.M[1][2], nex.M[1][2]=now.M[0][2];
    }
}
bool Judge(Node node){
    int i, j;
    for(i=0; i<2; i++){
        for(j=0; j<4; j++) if(node.M[i][j] != des[i][j]) return false;
    }
    return true;
}
string getStr(Node node){
    int i;
    string str;
    for(i=0; i<4; i++) str += (node.M[0][i] + '0');
    for(i=0; i<4; i++) str += (node.M[1][i] + '0');
    return str;
}
string BFS(){
    string str;
    int i, len;
    now.s = "";
    for(i=0; i<4; i++) now.M[0][i] = i+1;
    now.M[1][0]=8, now.M[1][1]=7, now.M[1][2]=6, now.M[1][3]=5;
    queue<Node> q;
    if(Judge(now)) return now.s;
    q.push(now);
    mp[getStr(now)] = true;
    while(!q.empty()){
        now = q.front();
        q.pop();
        for(i=0; i<3; i++){
            nex.s = now.s + op[i];
            getMatrix(op[i]);
            if(Judge(nex)) return nex.s;
            str = getStr(nex);
            if(!mp[str]){
                q.push(nex);
                mp[str] = true;
            }
        }
    }
    return "";
}
int main(){
    int i;
    string ans;
    while(cin>>des[0][0]>>des[0][1]>>des[0][2]>>des[0][3]){ //输入第一组 
        cin>>des[1][3]>>des[1][2]>>des[1][1]>>des[1][0]; //输入第二组 
        mp.clear();
        ans = BFS();
        printf("%d\n", ans.size());
        for(i=0; i<ans.size(); i++) printf("%c", ans[i]);
        printf("\n");
    }
    return 0;
}

Summary: This question is actually the same as the previous question, the only difference is that you need to record the state of occurrence, otherwise in some cases there will be an endless loop and cause a timeout. However, I want to vomit, the description of this question is really not good, especially the output!

Guess you like

Origin www.cnblogs.com/heyour/p/12674467.html