簡體   English   中英

從立方塔遞歸確定所有解決方案

[英]Recursive determination of all solutions from a cube tower

我必須解決一個任務,以確定Java中的多維數據集塔中的所有解決方案。

任務:
四個具有彩色表面(紅色,藍色,綠色,黃色)的立方體相互堆疊並旋轉,因此四種顏色都不會在牆壁的每一側出現兩次。 我必須開發一個系統,該系統將遞歸確定多維數據集塔的所有解決方案。 (與名為“即時精神錯亂”的謎題進行比較:https: //en.wikipedia.org/wiki/Instant_Insanity

迭代地解決這個難題相對容易,但是在我看來,遞歸方法非常困難。

多維數據集類:

private String[] colour;

public Cube() {
    this.colour = new String[6];
    for(int i = 0; i < colour.length; i++) {
        colour[i] = colourRead();
    }
}

我還獲得了兩個用於水平和垂直旋轉多維數據集的函數,一個用於顏色的get函數和一個用於讀取多維數據集顏色的函數。

立方塔級:

private Cube[] cube;
private ArrayList<String> solutions;
private int solutionCounter = 1;

public CubeTower() {

    this.cube= new Cube[4];
    this.cube[0] = new Cube();
    this.cube[1] = new Cube();
    this.cube[2] = new Cube();
    this.cube[3] = new Cube();
    this.solutions = new ArrayList<>();
}

如果立方體塔的一側有重復的顏色,我還獲得了一個立方體的get函數和一個要測試的函數。

我不知道如何編寫函數來遞歸確定所有解決方案。 也許有人對如何解決這個難題提出了建議。 謝謝你的幫助!

這是遞歸方法的偽代碼。

function findAllSolutions(cubeTower):
    if cubeTower is None:
        cubeTower = []
    solutions = []
    if length(cubeTower) < 4:
        for each cube in possible cube rotations:
            cubeTower.push(cube)
            solutions.appendAll(findAllSolutions(cubeTower))      
            cubeTower.pop(cube)
    else:
        if (isValidSolution(cubeTower)):
            solutions.push(copy of cubeTower)
    return solutions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM