【HDOJ】3459 Rubik 2×2×2

模拟+DFS。

  1 /* 3459 */
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 
  6 #define MAXN    10
  7 #define MAXL    1005
  8 
  9 #define U0        map[0][2]
 10 #define U1        map[0][3]
 11 #define U2        map[1][3]
 12 #define U3        map[1][2]
 13 
 14 #define F0        map[2][2]
 15 #define F1        map[2][3]
 16 #define F2        map[3][3]
 17 #define F3        map[3][2]
 18 
 19 #define R0        map[2][4]
 20 #define R1        map[2][5]
 21 #define R2        map[3][5]
 22 #define R3        map[3][4]
 23 
 24 #define L0        map[2][0]
 25 #define L1        map[2][1]
 26 #define L2        map[3][1]
 27 #define L3        map[3][0]
 28 
 29 #define B0        map[2][6]        
 30 #define B1        map[2][7]
 31 #define B2        map[3][7]
 32 #define B3        map[3][6]
 33 
 34 #define D0        map[4][2]
 35 #define D1        map[4][3]
 36 #define D2        map[5][3]
 37 #define D3        map[5][2]
 38 
 39 
 40 const int n = 6;
 41 const int m = 8;
 42 int deep;
 43 char map[MAXN][MAXN];
 44 char op[MAXL];
 45 
 46 bool isValid() {
 47     bool ret;
 48     ret =\
 49         (U0==U1 && U0==U2 && U0==U3) &&\
 50         (L0==L1 && L0==L2 && L0==L3) &&\
 51         (R0==R1 && R0==R2 && R0==R3) &&\
 52         (F0==F1 && F0==F2 && F0==F3) &&\
 53         (B0==B1 && B0==B2 && B0==B3) &&\
 54         (D0==D1 && D0==D2 && D0==D3) ;
 55     return ret;    
 56 }
 57 
 58 void rotateX() {
 59     char ch, ch1, ch2;
 60     // handle right
 61     ch = R0;
 62     R0 = R1;
 63     R1 = R2;
 64     R2 = R3;
 65     R3 = ch;
 66     
 67     ch1 = F1;
 68     ch2 = F2;
 69     // up -> front
 70     F1 = U1;
 71     F2 = U2;
 72     // back -> up
 73     U1 = B3;
 74     U2 = B0;
 75     // down -> back
 76     B0 = D2;
 77     B3 = D1;
 78     // front -> down
 79     D1 = ch1;
 80     D2 = ch2;
 81 }
 82 
 83 void rotateY() {
 84     char ch, ch0, ch1;
 85     
 86     // handle up
 87     ch = U0;
 88     U0 = U1;
 89     U1 = U2;
 90     U2 = U3;
 91     U3 = ch;
 92     
 93     ch0 = F0;
 94     ch1 = F1;
 95     // left -> front
 96     F0 = L0;
 97     F1 = L1;
 98     // back -> left
 99     L0 = B0;
100     L1 = B1;
101     // right -> back
102     B0 = R0;
103     B1 = R1;
104     // front -> right
105     R0 = ch0;
106     R1 = ch1;
107 }
108 
109 void rotateZ() {
110     char ch, ch2, ch3;
111     
112     // handle front
113     ch = F0;
114     F0 = F1;
115     F1 = F2;
116     F2 = F3;
117     F3 = ch;
118     
119     ch2 = U2;
120     ch3 = U3;
121     // right -> up
122     U3 = R0;
123     U2 = R3;
124     // down -> right
125     R0 = D1;
126     R3 = D0;
127     // left -> down
128     D0 = L1;
129     D1 = L2;
130     // up -> left
131     L1 = ch2;
132     L2 = ch3;
133 }
134 
135 bool dfs(int d) {
136     if (d == deep)
137         return isValid();
138     
139     op[d] = 'X';
140     rotateX();
141     if (dfs(d+1))
142         return true;
143     rotateX();
144     rotateX();
145     rotateX();
146     
147     op[d] = 'Y';
148     rotateY();
149     if (dfs(d+1))
150         return true;
151     rotateY();
152     rotateY();
153     rotateY();
154     
155     
156     op[d] = 'Z';
157     rotateZ();
158     if (dfs(d+1))
159         return true;
160     rotateZ();
161     rotateZ();
162     rotateZ();
163     
164     return false;
165 }
166 
167 int main() {
168     int i, j, k;
169     
170     #ifndef ONLINE_JUDGE
171         freopen("data.in", "r", stdin);
172         freopen("data.out", "w", stdout);
173     #endif
174     
175     while (1) {
176         for (i=0; i<n; ++i)
177             scanf("%s", map[i]);
178         if (map[0][2] == '.')
179             break;
180         deep = 0;
181         while (1) {
182             if (dfs(0))
183                 break;
184             ++deep;
185         }
186         op[deep] = '\0';
187         puts(op);
188     }
189     
190     return 0;
191 }

 

posted on 2015-02-28 12:46  Bombe  阅读(202)  评论(0编辑  收藏  举报

导航