• C


     
    C. Solution for Cube
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

        During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2.

    It's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction.

    To check her answers she wants to use a program which will for some state of cube tell if it's possible to solve it using one rotation, described above.

    Cube is called solved if for each face of cube all squares on it has the same color.

    https://en.wikipedia.org/wiki/Rubik's_Cube

    Input

        In first line given a sequence of 24 integers ai (1 ≤ ai ≤ 6), where ai denotes color of i-th square. There are exactly 4 occurrences of all colors in this sequence.

    Output

        Print «YES» (without quotes) if it's possible to solve cube using one rotation and «NO» (without quotes) otherwise.

    Examples
    input
    2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4
    output
    NO
    input
    5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3
    output
    YES
    Note

        In first test case cube looks like this:

    In second test case cube looks like this:

    It's possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16.

    题意:

    给一个二阶魔方,你看看你转一圈能不能还原它。 

    解题思路:

    开25的数组存输入的颜色(i=1开始);

    开[3][8]的数组分别存三种转的圈圈;

     

    对于三种圈圈 分别转两次(向前向后) 对于每次 都判断可不可还原;

    关于判断还原:

    对于每面,反应在25的数组上就是每4个元素,判断是否相同,有一不同则还原失败;

    关于旋转:

    最直接的体现就是,对于当前那一圈的所有涉及到的元素,向前或者向后挪两格。

    附上借鉴的代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int a[25],x[3][8]={{1,3,5,7,9,11,22,24},{13,14,5,6,17,18,21,22},{1,2,18,20,12,11,15,13}};
     5 void c() ///判断是否每一面都已经转好
     6 {
     7     for(int i=1;i<7;i++)
     8         for(int j=4*(i-1)+1;j<4*i;j++)
     9             if(a[j]!=a[4*i]) return ;
    10     puts("YES");exit(0);
    11 }
    12 
    13 
    14 void r(int i)
    15 {
    16     int v=a[x[i][0]],v2=a[x[i][1]];
    17 
    18     for(int j=0;j<6;j++)
    19         a[x[i][j]]=a[x[i][j+2]];
    20     a[x[i][6]]=v;
    21     a[x[i][7]]=v2;///向前转一次;
    22 
    23 }
    24 
    25 int main()
    26 {
    27     for(int i=1;i<25;i++)
    28         scanf("%d",&a[i]);
    29     for(int i=0;i<3;i++) ///对于每一种圈圈
    30         for(int k=0;k<2;k++)
    31     {
    32         ///转一次(k=0时,向前;k=1时,向后) 判断一下 在转一次(k=0为下一次转做准备,k=1,还原)
    33         r(i);c();r(i);
    34     }
    35     puts("NO");
    36     return 0;
    37 }
    C-Solution for Cube
    まだまだだね
  • 相关阅读:
    智能指针的简单实现
    原型模式
    Linux——模拟实现一个简单的shell(带重定向)
    软件开发的一个案例:学生信息管理系统
    关于正则的一点总结
    STL——模拟实现空间配置器
    LeetCode全文解锁 √
    话说extern和static
    C++有关 const & 内敛 & 友元&静态成员那些事
    C语——宏小结
  • 原文地址:https://www.cnblogs.com/xxQ-1999/p/7803590.html
Copyright © 2020-2023  润新知