【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 }
时间: 2024-11-06 15:53:16

【HDOJ】3459 Rubik 2×2×2的相关文章

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (

【HDOJ】2795 Billboard

线段树.注意h范围(小于等于n). 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXN 200005 5 #define lson l, mid, rt<<1 6 #define rson mid+1, r, rt<<1|1 7 #define mymax(x, y) (x>y) ? x:y 8 9 int nums[MAXN<<2]; 10 int h, w; 11 12