ZTE-中兴捧月-北京线下测试赛--A题

307 阅读4分钟

魔井

题目:如下图所示一个魔井,魔井由32块格子组成,每个格子有一种颜色,一共有四种颜色,每种颜色有8块格子。而魔井的上的格子是可以移动的,他们可以沿着A、B、C、D、E、F、G、H八个方向移动,比如向A移动一步,那么方块1将移动到该列的最后,其余方块依次向上移动一格。现在要移动魔井,使它“开启”,即中间的八个方块10、11、12、16、17、21、22、23的颜色相同,如下图,我们需要移动以下步骤:D,F,就可以使中间的八个方块的颜色均为黄色。现在输入的魔井,要找出使之“开启”的最少的移动步骤,程序需要输出中间八个格子的颜色,同时输出移动的步骤。

输入:

魔井的每种颜色用数字来标识,1代表绿色,2代表红色、3代表蓝色、4代表黄色,输入为一行数字,数字从上到下,从左到右(如图中1-32的顺序)依次代表一个方块

下图可标识为:

1 2 3 1 4 1 2 1 3 4 4 4 3 2 2 4 4 1 3 1 4 4 3 2 2 2 3 3 1 3 1 2

输出:

输出第一行为中间方格的颜色,第二行输出走过的步骤,如下:

4

DF

思路:采用层序遍历(效率很低),从初始状态出发,分别可以向八个方向移动一步,然后再遍历这方格方向的下一步的移动,遍历时判断中间方格的颜色是否满足要求,满足了就中断遍历,输出结果。

代码用一个结构体来存储每个树节点,有三个成员int型数组,用来存储32个方格的颜色,int level用来标识层序,char型数组用来存储经过的步骤。用一个队列来存储要遍历的节点,当遍历一个节点时,如果该节点不满足要求,就把它的子节点全部入列

代码:

#include <stdio.h>
#include <queue>
#include <memory>
struct Node{
    char color[33];
    int level;
    char ans[100];
};

bool panduan(Node temp){
    int a[8],i=0,j=0;
    a[0]=temp.color[10];
    a[1]=temp.color[11];
    a[2]=temp.color[12];
    a[3]=temp.color[16];
    a[4]=temp.color[17];
    a[5]=temp.color[21];
    a[6]=temp.color[22];
    a[7]=temp.color[23];
    for (i=0;i<8;i++)
    {
        for (j=i;j<8;j++)
        {
            if (a[i]!=a[j])
                return false;
        }
    }
    return true;
}


void main(){
    freopen("test.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int i;
    Node first,temp,temp2;
    memset(&first,0,sizeof(first));
    memset(&temp,0,sizeof(temp));
    memset(&temp2,0,sizeof(temp2));
    for (i=1;i<33;i++)
    {
        scanf("%d",&first.color[i]);
        getchar();
    }

    first.level=0;
    std::queue<Node> q;
    if(panduan(first)){
        temp2=first;
    }
    q.push(first);
    while (!q.empty())
    {
        temp=q.front();
        q.pop();
        //A
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[1]=temp.color[3];
        temp2.color[3]=temp.color[5];
        temp2.color[5]=temp.color[10];
        temp2.color[10]=temp.color[16];
        temp2.color[16]=temp.color[21];
        temp2.color[21]=temp.color[27];
        temp2.color[27]=temp.color[29];
        temp2.color[29]=temp.color[31];
        temp2.color[31]=temp.color[1];
        temp2.ans[temp2.level]='A';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //B
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[2]=temp.color[4];
        temp2.color[4]=temp.color[6];
        temp2.color[6]=temp.color[12];
        temp2.color[12]=temp.color[17];
        temp2.color[17]=temp.color[23];
        temp2.color[23]=temp.color[28];
        temp2.color[28]=temp.color[30];
        temp2.color[30]=temp.color[32];
        temp2.color[32]=temp.color[2];
        temp2.ans[temp2.level]='B';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //C
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[7]=temp.color[15];
        temp2.color[8]=temp.color[7];
        temp2.color[9]=temp.color[8];
        temp2.color[10]=temp.color[9];
        temp2.color[11]=temp.color[10];
        temp2.color[12]=temp.color[11];
        temp2.color[13]=temp.color[12];
        temp2.color[14]=temp.color[13];
        temp2.color[15]=temp.color[14];
        temp2.ans[temp2.level]='C';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //D
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[18]=temp.color[26];
        temp2.color[19]=temp.color[18];
        temp2.color[20]=temp.color[19];
        temp2.color[21]=temp.color[20];
        temp2.color[22]=temp.color[21];
        temp2.color[23]=temp.color[22];
        temp2.color[24]=temp.color[23];
        temp2.color[25]=temp.color[24];
        temp2.color[26]=temp.color[25];
        temp2.ans[temp2.level]='D';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //E
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[2]=temp.color[32];
        temp2.color[4]=temp.color[2];
        temp2.color[6]=temp.color[4];
        temp2.color[12]=temp.color[6];
        temp2.color[17]=temp.color[12];
        temp2.color[23]=temp.color[17];
        temp2.color[28]=temp.color[23];
        temp2.color[30]=temp.color[28];
        temp2.color[32]=temp.color[30];
        temp2.ans[temp2.level]='E';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //F
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[1]=temp.color[31];
        temp2.color[3]=temp.color[1];
        temp2.color[5]=temp.color[3];
        temp2.color[10]=temp.color[5];
        temp2.color[16]=temp.color[10];
        temp2.color[21]=temp.color[16];
        temp2.color[27]=temp.color[21];
        temp2.color[29]=temp.color[27];
        temp2.color[31]=temp.color[29];
        temp2.ans[temp2.level]='F';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //G
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[18]=temp.color[19];
        temp2.color[19]=temp.color[20];
        temp2.color[20]=temp.color[21];
        temp2.color[21]=temp.color[22];
        temp2.color[22]=temp.color[23];
        temp2.color[23]=temp.color[24];
        temp2.color[24]=temp.color[25];
        temp2.color[25]=temp.color[26];
        temp2.color[26]=temp.color[18];
        temp2.ans[temp2.level]='G';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
        //H
        memcpy(&temp2,&temp,sizeof(temp));
        temp2.color[7]=temp.color[8];
        temp2.color[8]=temp.color[9];
        temp2.color[9]=temp.color[10];
        temp2.color[10]=temp.color[11];
        temp2.color[11]=temp.color[12];
        temp2.color[12]=temp.color[13];
        temp2.color[13]=temp.color[14];
        temp2.color[14]=temp.color[15];
        temp2.color[15]=temp.color[7];
        temp2.ans[temp2.level]='H';
        temp2.level+=1;
        if(panduan(temp2)){
            break;
        }
        else
            q.push(temp2);
    }
    printf("%d\n",temp2.color[10]);
    for (i=0;i<temp2.level;i++)
    {
        printf("%c",temp2.ans[i]);
    }

}