UVA 253 Cube painting(模拟+枚举)

初始:上前左右后下
模拟:
向左旋转一次(上下为轴上下保持不动)
上后前左右下
向后旋转(左右为轴左右保持不动)
前下左右上后
顺时针旋转(前后为轴前后保持不动)
左前下上后右
枚举:
做一个三重循环,把所有的旋转情况枚举一遍
  左旋转4次
    向后旋转4次
      顺时针旋转4次
        遇到目标串返回true
枚举完所有情况返回false

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

bool check(char s[]){
    char s1[7];//6个分量表示 上前左右后下
    char s2[7];
    int i,j,k;
    //cout<<s<<endl;
    for(i=0;i<6;i++) s1[i] = s[i];
    s1[i] = '\0';
    k=0;
    for(i=6;i<12;i++,k++) s2[k] = s[i];//目标串
    s2[k] = '\0';
    //cout<<s1<<endl;
    //cout<<s2<<endl;
    char a,b,c,d;
    for(i=0;i<4;i++){//左旋转 上下为轴
        //上下保持不变 上右前后左下
        a = s1[1];b= s1[2];c = s1[3];d = s1[4];
        s1[1] = c;s1[2] = a;s1[3] = d;s1[4] = b;
        //cout<<i<<":"<<s1<<endl;
        for(j=0;j<4;j++){//后旋转 左右为轴
            //左右保持不变 前下左右上后

            a = s1[0];b = s1[1];c=s1[4];d=s1[5];
            s1[0] = b;s1[1] = d;s1[4] = a;s1[5] = c;
            for(k=0;k<4;k++){//顺时针旋转 前后为轴
                //前后不变 左前下上后右
                a = s1[0];b = s1[2];c = s1[3]; d= s1[5];
                s1[0] = b;s1[2] = d; s1[3] = a; s1[5] = c;
                if(strcmp(s1,s2)==0) return true;
            }
            /*
            cout<<"\t"<<j<<":"<<s1<<endl;
            if(strcmp(s1,s2)==0) {
                //cout<<i<<" "<<j<<endl;
                return true;
            }
            */
        }
    }
    return false;
}
int main()
{
    //#define LOCAL
    #ifdef LOCAL
    freopen("in","r",stdin);
    freopen("out","w",stdout);
    #endif
    char s[13];
    bool flag;
    while(cin>>s){
        flag = check(s);
        printf("%s\n",flag?"TRUE":"FALSE");
    }
    return 0;
}

 

posted @ 2019-06-14 22:35  fanyuheng  阅读(75)  评论(0)    收藏  举报