zoukankan      html  css  js  c++  java
  • ZTE-中兴捧月-北京线下测试赛--A题

    魔井

    题目:如下图所示一个魔井,魔井由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型数组用来存储经过的步骤。用一个队列来存储要遍历的节点,当遍历一个节点时,如果该节点不满足要求,就把它的子节点全部入列

    代码:

      1 #include <stdio.h>
      2 #include <queue>
      3 #include <memory>
      4 struct Node{
      5     char color[33];
      6     int level;
      7     char ans[100];
      8 };
      9 
     10 bool panduan(Node temp){
     11     int a[8],i=0,j=0;
     12     a[0]=temp.color[10];
     13     a[1]=temp.color[11];
     14     a[2]=temp.color[12];
     15     a[3]=temp.color[16];
     16     a[4]=temp.color[17];
     17     a[5]=temp.color[21];
     18     a[6]=temp.color[22];
     19     a[7]=temp.color[23];
     20     for (i=0;i<8;i++)
     21     {
     22         for (j=i;j<8;j++)
     23         {
     24             if (a[i]!=a[j])
     25                 return false;
     26         }
     27     }
     28     return true;
     29 }
     30 
     31 
     32 void main(){
     33     freopen("test.txt","r",stdin);
     34     freopen("out.txt","w",stdout);
     35     int i;
     36     Node first,temp,temp2;
     37     memset(&first,0,sizeof(first));
     38     memset(&temp,0,sizeof(temp));
     39     memset(&temp2,0,sizeof(temp2));
     40     for (i=1;i<33;i++)
     41     {
     42         scanf("%d",&first.color[i]);
     43         getchar();
     44     }
     45 
     46     first.level=0;
     47     std::queue<Node> q;
     48     if(panduan(first)){
     49         temp2=first;
     50     }
     51     q.push(first);
     52     while (!q.empty())
     53     {
     54         temp=q.front();
     55         q.pop();
     56         //A
     57         memcpy(&temp2,&temp,sizeof(temp));
     58         temp2.color[1]=temp.color[3];
     59         temp2.color[3]=temp.color[5];
     60         temp2.color[5]=temp.color[10];
     61         temp2.color[10]=temp.color[16];
     62         temp2.color[16]=temp.color[21];
     63         temp2.color[21]=temp.color[27];
     64         temp2.color[27]=temp.color[29];
     65         temp2.color[29]=temp.color[31];
     66         temp2.color[31]=temp.color[1];
     67         temp2.ans[temp2.level]='A';
     68         temp2.level+=1;
     69         if(panduan(temp2)){
     70             break;
     71         }
     72         else
     73             q.push(temp2);
     74         //B
     75         memcpy(&temp2,&temp,sizeof(temp));
     76         temp2.color[2]=temp.color[4];
     77         temp2.color[4]=temp.color[6];
     78         temp2.color[6]=temp.color[12];
     79         temp2.color[12]=temp.color[17];
     80         temp2.color[17]=temp.color[23];
     81         temp2.color[23]=temp.color[28];
     82         temp2.color[28]=temp.color[30];
     83         temp2.color[30]=temp.color[32];
     84         temp2.color[32]=temp.color[2];
     85         temp2.ans[temp2.level]='B';
     86         temp2.level+=1;
     87         if(panduan(temp2)){
     88             break;
     89         }
     90         else
     91             q.push(temp2);
     92         //C
     93         memcpy(&temp2,&temp,sizeof(temp));
     94         temp2.color[7]=temp.color[15];
     95         temp2.color[8]=temp.color[7];
     96         temp2.color[9]=temp.color[8];
     97         temp2.color[10]=temp.color[9];
     98         temp2.color[11]=temp.color[10];
     99         temp2.color[12]=temp.color[11];
    100         temp2.color[13]=temp.color[12];
    101         temp2.color[14]=temp.color[13];
    102         temp2.color[15]=temp.color[14];
    103         temp2.ans[temp2.level]='C';
    104         temp2.level+=1;
    105         if(panduan(temp2)){
    106             break;
    107         }
    108         else
    109             q.push(temp2);
    110         //D
    111         memcpy(&temp2,&temp,sizeof(temp));
    112         temp2.color[18]=temp.color[26];
    113         temp2.color[19]=temp.color[18];
    114         temp2.color[20]=temp.color[19];
    115         temp2.color[21]=temp.color[20];
    116         temp2.color[22]=temp.color[21];
    117         temp2.color[23]=temp.color[22];
    118         temp2.color[24]=temp.color[23];
    119         temp2.color[25]=temp.color[24];
    120         temp2.color[26]=temp.color[25];
    121         temp2.ans[temp2.level]='D';
    122         temp2.level+=1;
    123         if(panduan(temp2)){
    124             break;
    125         }
    126         else
    127             q.push(temp2);
    128         //E
    129         memcpy(&temp2,&temp,sizeof(temp));
    130         temp2.color[2]=temp.color[32];
    131         temp2.color[4]=temp.color[2];
    132         temp2.color[6]=temp.color[4];
    133         temp2.color[12]=temp.color[6];
    134         temp2.color[17]=temp.color[12];
    135         temp2.color[23]=temp.color[17];
    136         temp2.color[28]=temp.color[23];
    137         temp2.color[30]=temp.color[28];
    138         temp2.color[32]=temp.color[30];
    139         temp2.ans[temp2.level]='E';
    140         temp2.level+=1;
    141         if(panduan(temp2)){
    142             break;
    143         }
    144         else
    145             q.push(temp2);
    146         //F
    147         memcpy(&temp2,&temp,sizeof(temp));
    148         temp2.color[1]=temp.color[31];
    149         temp2.color[3]=temp.color[1];
    150         temp2.color[5]=temp.color[3];
    151         temp2.color[10]=temp.color[5];
    152         temp2.color[16]=temp.color[10];
    153         temp2.color[21]=temp.color[16];
    154         temp2.color[27]=temp.color[21];
    155         temp2.color[29]=temp.color[27];
    156         temp2.color[31]=temp.color[29];
    157         temp2.ans[temp2.level]='F';
    158         temp2.level+=1;
    159         if(panduan(temp2)){
    160             break;
    161         }
    162         else
    163             q.push(temp2);
    164         //G
    165         memcpy(&temp2,&temp,sizeof(temp));
    166         temp2.color[18]=temp.color[19];
    167         temp2.color[19]=temp.color[20];
    168         temp2.color[20]=temp.color[21];
    169         temp2.color[21]=temp.color[22];
    170         temp2.color[22]=temp.color[23];
    171         temp2.color[23]=temp.color[24];
    172         temp2.color[24]=temp.color[25];
    173         temp2.color[25]=temp.color[26];
    174         temp2.color[26]=temp.color[18];
    175         temp2.ans[temp2.level]='G';
    176         temp2.level+=1;
    177         if(panduan(temp2)){
    178             break;
    179         }
    180         else
    181             q.push(temp2);
    182         //H
    183         memcpy(&temp2,&temp,sizeof(temp));
    184         temp2.color[7]=temp.color[8];
    185         temp2.color[8]=temp.color[9];
    186         temp2.color[9]=temp.color[10];
    187         temp2.color[10]=temp.color[11];
    188         temp2.color[11]=temp.color[12];
    189         temp2.color[12]=temp.color[13];
    190         temp2.color[13]=temp.color[14];
    191         temp2.color[14]=temp.color[15];
    192         temp2.color[15]=temp.color[7];
    193         temp2.ans[temp2.level]='H';
    194         temp2.level+=1;
    195         if(panduan(temp2)){
    196             break;
    197         }
    198         else
    199             q.push(temp2);
    200     }
    201     printf("%d
    ",temp2.color[10]);
    202     for (i=0;i<temp2.level;i++)
    203     {
    204         printf("%c",temp2.ans[i]);
    205     }
    206 
    207 }
  • 相关阅读:
    http headers总结
    golang跨域配置
    Kafka中topic的Partition,Kafka为什么这么快,Consumer的负载均衡及consumerGroup的概念
    kafka partition(分区)与 group
    RocketMQ从部署到应用(Golang)
    Codeforces Round #706 (Div. 2)
    关于平衡树
    具体数学 第三章 整值函数
    FFT&NTT&多项式全家桶
    省选测试15
  • 原文地址:https://www.cnblogs.com/txlbupt/p/3188774.html
Copyright © 2011-2022 走看看