zoukankan      html  css  js  c++  java
  • 东东转魔方(模拟)

    题目描述:

    东东有一个二阶魔方,即2×2×2的一个立方体组。立方体由八个角组成。

    魔方的每一块都用三维坐标(h, k, l)标记,其中h, k, l∈{0,1}。六个面的每一个都有四个小面,每个小面都有一个正整数。

    对于每一步,东东可以选择一个特定的面,并把此面顺时针或逆时针转90度。

    请你判断,是否东东可以在一个步骤还原这个魔方(每个面没有异色)。

    输入:

    输入的第一行包含一个整数N(N≤30),这是测试用例的数量。

    对于每个测试用例, 第 1~4 个数描述魔方的顶面,这是常见的2×2面,由(0,0,1),(0,1,1),(1,0,1),(1,1,1)标记。四个整数对应于上述部分。

    第 5~8 个数描述前面,即(1,0,1),(1,1,1),(1,0,0),(1,1,0)的公共面。四个整数 与上述各部分相对应。

    第 9~12 个数描述底面,即(1,0,0),(1,1,0),(0,0,0),(0,1,0)的公共面。四个整数与上述各部分相对应。

    第 13~16 个数描述背面,即(0,0,0),(0,1,0),(0,0,1),(0,1),(0,1,1)的公共面。四个整数与上述各部分相对应。

    第 17~20 个数描述左面,即(0,0,0),(0,0,1),(1,0,0),(1,0,1)的公共面。给出四个整数与上述各部分相对应。

    第 21~24 个数描述了右面,即(0,1,1),(0,1,0),(1,1,1),(1,1,0)的公共面。给出四个整数与上述各部分相对应。

    换句话说,每个测试用例包含24个整数a、b、c到x。你可以展开表面以获得平面图

    如下所示。

    + - + - + - + - + - + - +
    | q | r | a | b | u | v |
    + - + - + - + - + - + - +
    | s | t | c | d | w | x |
    + - + - + - + - + - + - +
    | e | f |
    + - + - +
    | g | h |
    + - + - +
    | i | j |
    + - + - +
    | k | l |
    + - + - +
    | m | n |
    + - + - +
    | o | p |
    + - + - +

    输出:

    对于每个测试用例,魔方如果可以至多 "只转一步" 恢复,输出YES,则输出NO。

    思路:

    • 因为只能转90°,所以可能有12种情况;
    • 进一步思考,前面顺时针转和后面逆时针转得到的状态是相同的,所以只需考虑6种情况
    • 再进一步,一个面顺时针转一次,相当于逆时针转3次,所以只需实现3种旋转即可表示6种情况
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 int a[100][100];
     7 int *u,*d,*f,*b,*l,*r;
     8 //int u[100],d[100],f[100],b[100],r[100],l[100];
     9 void upShift()
    10 {
    11     int t1=f[1],t2=f[2];
    12     f[1]=l[2];f[2]=l[4];
    13     l[2]=b[4];l[4]=b[3];
    14     b[4]=r[3];b[3]=r[1];
    15     r[3]=t1;r[1]=t2;
    16 }
    17 void leftShift()
    18 {
    19     int t1=u[1],t2=u[3];
    20     u[1]=b[1];u[3]=b[3];
    21     b[1]=d[1];b[3]=d[3];
    22     d[1]=f[1];d[3]=f[3];
    23     f[1]=t1;f[3]=t2;
    24  
    25 }
    26 void frontShift()
    27 {
    28     int t1=u[1],t2=u[2];
    29     u[1]=l[1];u[2]=l[2];
    30     l[1]=d[4];l[2]=d[3];
    31     d[4]=r[1];d[3]=r[2];
    32     r[1]=t1;r[2]=t2;
    33 }
    34 bool Judge()
    35 {
    36     for(int i=1;i<=6;i++)
    37         if(count(a[i]+1,a[i]+4+1,a[i][1])!=4) return false;
    38      
    39     return true;
    40 }
    41 void initial()
    42 {
    43     u=a[1];f=a[2];d=a[3];b=a[4];   
    44     l=a[5];r=a[6];
    45     for(int i=1;i<=6;i++)
    46         for(int j=1;j<=4;j++)
    47             cin>>a[i][j];
    48 }
    49 int main()
    50 {
    51     int N; cin>>N;
    52     while(N--)
    53     {
    54         initial();
    55         if(Judge()) { cout<<"YES"<<endl; continue; }
    56          
    57         upShift();
    58         if(Judge()) { cout<<"YES"<<endl; continue; }
    59         upShift();upShift();
    60         if(Judge()) { cout<<"YES"<<endl; continue; }
    61         upShift();
    62          
    63         leftShift();
    64         if(Judge()) { cout<<"YES"<<endl; continue; }
    65         leftShift();leftShift();
    66         if(Judge()) { cout<<"YES"<<endl; continue; }
    67         leftShift();
    68          
    69         frontShift();
    70         if(Judge()) { cout<<"YES"<<endl; continue; }
    71         frontShift();frontShift();
    72         if(Judge()) { cout<<"YES"<<endl; continue; }
    73         frontShift();
    74         cout<<"NO"<<endl;
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    nil / Nil / NULL / NSNull
    When should I use nil and NULL in Objective-C?
    stream_context_create解析
    Linux如何通过命令查看日志文件的某几行(中间几行或最后几行)
    MySQL索引失效的几种场景
    Python网络爬虫精要
    深入Redis持久化
    SVN和Git 介绍,区别,优缺点以及适用范围
    php提示Notice: Undefined index解决方法
    在线更换背景网站(白色背景换为蓝色背景证件照)
  • 原文地址:https://www.cnblogs.com/liuzhuan-xingyun/p/13048978.html
Copyright © 2011-2022 走看看