# 你有4个涂色的立方体。每个立方体的每一面涂有一种颜色。编写一个程序找出所有符合 要求的排列

fengxiuping
• 330

Cube Front Back Left Right Top Bottom
1 R B G Y B Y
2 R G G Y B B
3 Y B R G Y R
4 Y G B R R R

4 个回答
✓ 已被采纳

``````# *-* coding: utf-8 -*-
data = '''
1 R B G Y B Y
2 R G G Y B B
3 Y B R G Y R
4 Y G B R R R
'''

def gen_order(row): # 对每个立方体生成24种顺序
for i in range(6): # 一共6个面
for j in range(4): # 顺进针旋转4次
yield row # 生成一个排列
row = [row[0], row[1], row[5], row[4], row[2], row[3]] # rotate
if i < 4: # 向上旋转
row = [row[5], row[4], row[2], row[3], row[0], row[1]]
elif i == 4: # 向右转
row = [row[2], row[3], row[1], row[0], row[4], row[5]]
else: # 再向右转2次
row = [row[1], row[0], row[3], row[2], row[4], row[5]]

data = [d.split()[1:] for d in data.split('\n') if d]

def illegal(a, b):
for i in range(len(a)):
if a[i] == b[i]:
return True
return False

result = []
for i0 in gen_order(data[0]):
for i1 in gen_order(data[1]):
if illegal(i0[:4], i1[:4]): # 前4个面要各不相同
continue
for i2 in gen_order(data[2]):
if illegal(i0[:4], i2[:4]) or illegal(i1[:4], i2[:4]):
continue
for i3 in gen_order(data[3]):
if illegal(i0[:4], i3[:4]) or illegal(i1[:4], i3[:4])\
or illegal(i2[:4], i3[:4]):
continue
result.append((i0, i1, i2, i3)) # 到这里的都是好样的

print 'total', len(result)
a,b,c,d = result[0]
print 'plan1 is'
print a
print b
print c
print d
``````

``````total 18
plan1 is
['R', 'B', 'Y', 'B', 'G', 'Y']
['G', 'R', 'G', 'Y', 'B', 'B']
['B', 'Y', 'R', 'G', 'R', 'Y']
['Y', 'G', 'B', 'R', 'R', 'R']
``````
moya
• 2

``````<?php
\$a = [
'front'  => 'r',
'back'   => 'b',
'left'   => 'g',
'right'  => 'y',
'top'    => 'b',
'bottom' => 'y',
];
\$b = [
'front'  => 'r',
'back'   => 'g',
'left'   => 'g',
'right'  => 'y',
'top'    => 'b',
'bottom' => 'b',
];
\$c = [
'front'  => 'y',
'back'   => 'b',
'left'   => 'r',
'right'  => 'g',
'top'    => 'y',
'bottom' => 'r',
];
\$d = [
'front'  => 'y',
'back'   => 'g',
'left'   => 'b',
'right'  => 'r',
'top'    => 'r',
'bottom' => 'r',
];
\$pos = [];
foreach(['r', 'g', 'b', 'y'] as \$v)
{
\$pos[\$v] = [];
foreach(['a' => \$a, 'b' => \$b, 'c' => \$c, 'd' => \$d] as \$k1 => \$v1)
{
\$i = array_keys(\$v1, \$v);
if(is_array(\$i))
{
foreach(\$i as \$v2)
{
\$pos[\$v]["{\$k1}_{\$v2}"] = \$v2;
}
}
else
{
\$pos[\$v]["{\$k1}_{\$i}"] = \$i;
}
}
}
echo "<pre>";
print_r(\$pos);
echo "</pre>";
``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进