佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > python實(shí)現(xiàn)三階魔方還原的示例代碼

python實(shí)現(xiàn)三階魔方還原的示例代碼

熱門(mén)標(biāo)簽:語(yǔ)音系統(tǒng) 電話(huà)運(yùn)營(yíng)中心 Win7旗艦版 硅谷的囚徒呼叫中心 百度AI接口 企業(yè)做大做強(qiáng) 客戶(hù)服務(wù) 呼叫中心市場(chǎng)需求

思路

復(fù)原魔方困難問(wèn)題的分解:

​ 1、用合適的數(shù)據(jù)結(jié)構(gòu)表示出三階魔方的六個(gè)面以及每一面的顏色

​ 2、每一次不同旋轉(zhuǎn)操作的實(shí)現(xiàn)

​ 3、復(fù)原時(shí)如何判斷當(dāng)前魔方應(yīng)該使用哪一種公式

本次實(shí)驗(yàn)實(shí)現(xiàn)了前兩個(gè)操作,具體思路是:

用numpy庫(kù)中的矩陣將六個(gè)面分別表示出來(lái),按上下左右前后的順序放入列表中。再依據(jù)流行公式里的方法編寫(xiě)對(duì)每一個(gè)面進(jìn)行旋轉(zhuǎn)操作的函數(shù),調(diào)用函數(shù)實(shí)現(xiàn)魔方的旋轉(zhuǎn)。最后輸入指令可得到旋轉(zhuǎn)之后的魔方,以及執(zhí)行逆序指令后驗(yàn)證魔方還原。

預(yù)備知識(shí)

矩陣:使用numpy庫(kù)中的矩陣結(jié)構(gòu)

函數(shù)說(shuō)明:

U: 上面順時(shí)針旋轉(zhuǎn) 90°

D: 底面順時(shí)針旋轉(zhuǎn) 90°

L: 左面順時(shí)針旋轉(zhuǎn) 90°

R: 右面順時(shí)針旋轉(zhuǎn) 90°

F: 正面順時(shí)針旋轉(zhuǎn) 90°

B: 背面順時(shí)針旋轉(zhuǎn) 90°

**注:**字母前加上下劃線(xiàn) ‘_' 表示逆時(shí)針

代碼詳解

本次實(shí)驗(yàn)將【上、下、左、右、前、后】六個(gè)面用數(shù)字【0、1、2、3、4、5】表示原本每個(gè)面的顏色,并依次存入列表faces【】里(即:faces[0]中存放的是最上面的數(shù)字全為0的三階矩陣)

注:魔方視角始終固定,即在整個(gè)過(guò)程中正(左…)面始終是正(左…)面

# 創(chuàng)建六個(gè)面,放在faces列表里,順序?yàn)樯希?),下(1),左(2),右(3),前(4),后(5)
faces = [np.zeros((3, 3))]

for i in range(1, 6):
    faces.append(np.ones((3, 3)) + faces[i - 1])

每一個(gè)面的 順時(shí)針逆時(shí)針 旋轉(zhuǎn)由函數(shù) clockwise()antiClockwise() 實(shí)現(xiàn)

t = np.array([[0, 0, 1],
              [0, 1, 0],
              [1, 0, 0]])

# 該面順時(shí)針旋轉(zhuǎn) 90 度
def clockwise(face):
    face = face.transpose().dot(t)
    return face

# 該面逆時(shí)針旋轉(zhuǎn) 90 度
def antiClockwise(face):
    face = face.dot(t).transpose()
    return face

A.transpose() 方法是實(shí)現(xiàn) A 矩陣的轉(zhuǎn)置

A.dot(B) 方法是實(shí)現(xiàn) A乘以矩陣B

通過(guò)計(jì)算,上述方法可以實(shí)現(xiàn)矩陣順時(shí)針或者逆時(shí)針旋轉(zhuǎn)的效果

在這里以左面的順時(shí)針旋轉(zhuǎn) 90°為例,其它旋轉(zhuǎn)方式可以類(lèi)比

def L(FACES):
    FACES[2] = clockwise(FACES[2])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = clockwise(FACES_new[4]), clockwise(FACES_new[1]), antiClockwise(FACES_new[5]), clockwise(FACES_new[0])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    e[0], f[0], g[0], h[0] = d[0], a[0], b[0], c[0]
    FACES[4], FACES[1], FACES[5], FACES[0] = antiClockwise(e), antiClockwise(f), clockwise(g), antiClockwise(h)

1、直接調(diào)用函數(shù)將左面(第2面)順時(shí)針旋轉(zhuǎn) 90°

FACES[2] = clockwise(FACES[2])

2、這里采用深度復(fù)制,使用 cp.deepcopy() 的方法,避免直接使用等號(hào) ‘=' 導(dǎo)致不同的變量指向同一個(gè)值。這時(shí),【e、f、g、h】和【a、b、c、d】代表魔方的

【正面、底面順時(shí)針旋轉(zhuǎn)90°、背面逆時(shí)針旋轉(zhuǎn)90°、上面順時(shí)針旋轉(zhuǎn)90°】

a, b, c, d = clockwise(FACES_new[4]), clockwise(FACES_new[1]), antiClockwise(FACES_new[5]), clockwise(FACES_new[0])

旋轉(zhuǎn)的目的是:

在左面旋轉(zhuǎn)的過(guò)程中,左面會(huì)影響到其它四個(gè)面,但對(duì)其它四個(gè)面的影響是不同的。例如正面、底面和上面被影響的是第一列,而背面被影響的是第三列。我們?yōu)榱耸垢髅娼y(tǒng)一起來(lái),方便數(shù)值的改變,我們選擇將正、底、上面順時(shí)針旋轉(zhuǎn)90°,將背面逆時(shí)針旋轉(zhuǎn)90°。這時(shí),我們只需按順序交換每一面的第一行,最后再逆時(shí)針或順時(shí)針轉(zhuǎn)回來(lái)即可。

3、按順序交換:正面第一行傳遞到底面第一行

​ 上面第一行傳遞到正面第一行

​ 背面第一行傳遞到上面第一行

​ 底面第一行傳遞到背面第一行

e[0], f[0], g[0], h[0] = d[0], a[0], b[0], c[0]

最后再依次根據(jù)上述操作逆旋轉(zhuǎn)回去:

FACES[4], FACES[1], FACES[5], FACES[0] = antiClockwise(e), antiClockwise(f), clockwise(g), antiClockwise(h)

代碼

import numpy as np
import copy as cp

# 創(chuàng)建六個(gè)面,放在faces列表里,順序?yàn)樯希?),下(1),左(2),右(3),前(4),后(5)
faces = [np.zeros((3, 3))]

for i in range(1, 6):
    faces.append(np.ones((3, 3)) + faces[i - 1])

t = np.array([[0, 0, 1],
              [0, 1, 0],
              [1, 0, 0]])

# 該面順時(shí)針旋轉(zhuǎn) 90 度
def clockwise(face):
    face = face.transpose().dot(t)
    return face

# 該面逆時(shí)針旋轉(zhuǎn) 90 度
def antiClockwise(face):
    face = face.dot(t).transpose()
    return face


def U(FACES):
    FACES[0] = clockwise(FACES[0])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][0], FACES[2][0], FACES[5][0], FACES[3][0] = d[0], a[0], b[0], c[0]


def _U(FACES):
    FACES[0] = antiClockwise(FACES[0])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][0], FACES[2][0], FACES[5][0], FACES[3][0] = b[0], c[0], d[0], a[0]


def U2(FACES):
    for i in range(2):
        U(FACES)
    '''
    FACES[0] = clockwise(clockwise(FACES[0]))
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][0], FACES[2][0], FACES[5][0], FACES[3][0] = c[0], d[0], a[0], b[0]
    '''


def D(FACES):
    FACES[1] = clockwise(FACES[1])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][2], FACES[2][2], FACES[5][2], FACES[3][2] = b[2], c[2], d[2], a[2]


def _D(FACES):
    FACES[1] = antiClockwise(FACES[1])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][2], FACES[2][2], FACES[5][2], FACES[3][2] = d[2], a[2], b[2], c[2]


def D2(FACES):
    for i in range(2):
        D(FACES)
    '''
    FACES[1] = clockwise(clockwise(FACES[1]))
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[4], FACES_new[2], FACES_new[5], FACES_new[3]
    FACES[4][2], FACES[2][2], FACES[5][2], FACES[3][2] = c[2], d[2], a[2], b[2]
    '''


def L(FACES):
    FACES[2] = clockwise(FACES[2])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = clockwise(FACES_new[4]), clockwise(FACES_new[1]), antiClockwise(FACES_new[5]), clockwise(FACES_new[0])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    e[0], f[0], g[0], h[0] = d[0], a[0], b[0], c[0]
    FACES[4], FACES[1], FACES[5], FACES[0] = antiClockwise(e), antiClockwise(f), clockwise(g), antiClockwise(h)


def _L(FACES):
    FACES[2] = antiClockwise(FACES[2])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = clockwise(FACES_new[4]), clockwise(FACES_new[1]), antiClockwise(FACES_new[5]), clockwise(FACES_new[0])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    e[0], f[0], g[0], h[0] = b[0], c[0], d[0], a[0]
    FACES[4], FACES[1], FACES[5], FACES[0] = antiClockwise(e), antiClockwise(f), clockwise(g), antiClockwise(h)


def L2(FACES):
    for i in range(2):
        L(FACES)


# 上(0),下(1),左(2),右(3),前(4),后(5)
def R(FACES):
    FACES[3] = clockwise(FACES[3])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = antiClockwise(FACES_new[4]), antiClockwise(FACES_new[1]), clockwise(FACES_new[5]), antiClockwise(
        FACES_new[0])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    g[0], f[0], e[0], h[0] = d[0], c[0], b[0], a[0]
    FACES[4], FACES[1], FACES[5], FACES[0] = clockwise(e), clockwise(f), antiClockwise(g), clockwise(h)


def _R(FACES):
    FACES[3] = antiClockwise(FACES[3])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = antiClockwise(FACES_new[4]), antiClockwise(FACES_new[1]), clockwise(FACES_new[5]), antiClockwise(
        FACES_new[0])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    f[0], g[0], h[0], e[0] = a[0], b[0], c[0], d[0]
    FACES[4], FACES[1], FACES[5], FACES[0] = clockwise(e), clockwise(f), antiClockwise(g), clockwise(h)


def R2(FACES):
    for i in range(2):
        R(FACES)


def F(FACES):
    FACES[4] = clockwise(FACES[4])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = clockwise(clockwise(FACES_new[0])), FACES_new[1], antiClockwise(FACES_new[2]), clockwise(FACES_new[3])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    e[0], g[0], f[0], h[0] = c[0], b[0], d[0], a[0]
    FACES[0], FACES[1], FACES[2], FACES[3] = clockwise(clockwise(e)), f, clockwise(g), antiClockwise(h)


def _F(FACES):
    FACES[4] = antiClockwise(FACES[4])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = clockwise(clockwise(FACES_new[0])), FACES_new[1], antiClockwise(FACES_new[2]), clockwise(FACES_new[3])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    g[0], f[0], h[0], e[0] = a[0], c[0], b[0], d[0]
    FACES[0], FACES[1], FACES[2], FACES[3] = clockwise(clockwise(e)), f, clockwise(g), antiClockwise(h)


def F2(FACES):
    for _ in range(2):
        F(FACES)


# 上(0),下(1),左(2),右(3),前(4),后(5)
def B(FACES):
    FACES[5] = clockwise(FACES[5])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[0], clockwise(clockwise(FACES_new[1])), clockwise(FACES_new[2]), antiClockwise(FACES_new[3])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    g[0], f[0], h[0], e[0] = a[0], c[0], b[0], d[0]
    FACES[0], FACES[1], FACES[2], FACES[3] = e, clockwise(clockwise(f)), antiClockwise(g), clockwise(h)


def _B(FACES):
    FACES[5] = antiClockwise(FACES[5])
    FACES_new = cp.deepcopy(FACES)
    a, b, c, d = FACES_new[0], clockwise(clockwise(FACES_new[1])), clockwise(FACES_new[2]), antiClockwise(FACES_new[3])
    e, f, g, h = cp.deepcopy(a), cp.deepcopy(b), cp.deepcopy(c), cp.deepcopy(d)
    e[0], g[0], f[0], h[0] = c[0], b[0], d[0], a[0]
    FACES[0], FACES[1], FACES[2], FACES[3] = e, clockwise(clockwise(f)), antiClockwise(g), clockwise(h)


def B2(FACES):
    for i in range(2):
        B(FACES)


'''
                          |************|
                          |*U1**U2**U3*|
                          |************|
                          |*U4**U5**U6*|
                          |************|
                          |*U7**U8**U9*|
                          |************|
              ************|************|************|************|
              *L1**L2**L3*|*F1**F2**F3*|*R1**R2**R3*|*B1**B2**B3*|
              ************|************|************|************|
              *L4**L5**L6*|*F4**F5**F6*|*R4**R5**R6*|*B4**B5**B6*|
              ************|************|************|************|
              *L7**L8**L9*|*F7**F8**F9*|*R7**R8**R9*|*B7**B8**B9*|
              ************|************|************|************|
                          |************|
                          |*D1**D2**D3*|
                          |************|
                          |*D4**D5**D6*|
                          |************|
                          |*D7**D8**D9*|
                          |************|
'''


def toString(FACES):
    print()
    for i in range(3):
        print("     ", int(FACES[0][i][0]), int(FACES[0][i][1]), int(FACES[0][i][2]))
    for i in range(3):
        print(int(FACES[2][i][0]), int(FACES[2][i][1]), int(FACES[2][i][2]), end=" ")
        print(int(FACES[4][i][0]), int(FACES[4][i][1]), int(FACES[4][i][2]), end=" ")
        print(int(FACES[3][i][0]), int(FACES[3][i][1]), int(FACES[3][i][2]), end=" ")
        print(int(FACES[5][i][0]), int(FACES[5][i][1]), int(FACES[5][i][2]))
    for i in range(3):
        print("     ", int(FACES[1][i][0]), int(FACES[1][i][1]), int(FACES[1][i][2]))
    print()


def moves(FACES, lst):
    for x in lst:
        if x == 'U':
            U(faces)
        elif x == 'u':
            _U(faces)
        elif x == 'D':
            D(faces)
        elif x == 'd':
            _D(faces)
        elif x == 'L':
            L(faces)
        elif x == 'l':
            _L(faces)
        elif x == 'R':
            R(faces)
        elif x == 'r':
            _R(faces)
        elif x == 'F':
            F(faces)
        elif x == 'f':
            _F(faces)
        elif x == 'B':
            B(faces)
        elif x == 'b':
            _B(faces)


lst = input("請(qǐng)輸入步驟:")
moves(faces, lst)
print("執(zhí)行后的魔方為")
toString(faces)
reverse = ''.join(map(chr, map(lambda x: ord(x) ^ 32, lst)))[::-1]
moves(faces, reverse)
print("魔方恢復(fù)步驟:", reverse)
toString(faces)

示例

請(qǐng)輸入步驟:UBLDFRULFDRULBGBVFDRLLBFLLDSSDBVDJFRUDLRFBDLFBbdj
執(zhí)行后的魔方為

      2 5 3
      5 0 2
      5 0 5
5 2 3 1 2 1 2 4 0 4 0 0
1 2 3 1 4 5 1 3 1 4 5 2
2 5 2 4 4 3 1 0 5 3 4 4
      1 0 4
      3 1 3
      0 3 0

魔方恢復(fù)步驟: JDBbfldbfrldurfjdvbdssdllfbllrdfvbgblurdflurfdlbu

      0 0 0
      0 0 0
      0 0 0
2 2 2 4 4 4 3 3 3 5 5 5
2 2 2 4 4 4 3 3 3 5 5 5
2 2 2 4 4 4 3 3 3 5 5 5
      1 1 1
      1 1 1
      1 1 1


Process finished with exit code 0

注:大寫(xiě)為順時(shí)針,小寫(xiě)為逆時(shí)針

到此這篇關(guān)于python實(shí)現(xiàn)三階魔方還原的示例代碼的文章就介紹到這了,更多相關(guān)python 三階魔方還原內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 用Python簡(jiǎn)陋模擬n階魔方

標(biāo)簽:山西 濟(jì)南 安康 長(zhǎng)沙 海南 喀什 山西 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)現(xiàn)三階魔方還原的示例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    忻州市| 浠水县| 丹东市| 丽江市| 五常市| 望都县| 海口市| 胶州市| 辽阳市| 志丹县| 婺源县| 厦门市| 前郭尔| 孝感市| 邢台市| 广州市| 岳普湖县| 伊春市| 监利县| 剑川县| 东光县| 龙里县| 乌审旗| 友谊县| 诸暨市| 成武县| 龙岩市| 卢氏县| 大石桥市| 屯留县| 新和县| 陕西省| 姚安县| 茌平县| 五常市| 治多县| 景德镇市| 个旧市| 凤山市| 彰武县| 治县。|