puzzle 8 java- pomoc w wytłumaczeniu kodu

0

Znalazłem w internecie kod chciałbym żeby mogł ktoś łopatologicznie wytłumaczyć
Wykorzystywana jest mapa jest zapisane skojarzenie - przechowujące dwa argumenty klucz i wartość e.add(str, null). Nie mogę rozkminać metod void jak działają na currentState itp.

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

class EightPuzzle {

    Queue<String> agenda = new LinkedList<String>();    
    Map<String,Integer> stateDepth = new HashMap<String, Integer>(); 
    Map<String,String> stateHistory = new HashMap<String,String>(); 

    public static void main(String args[]){

        String str="087465132";                                 

        EightPuzzle e = new EightPuzzle();            
        e.add(str, null);                                                

        while(!e.agenda.isEmpty()){
            String currentState = e.agenda.remove();
            e.up(currentState);                                       
            e.down(currentState);                                    
            e.left(currentState);                                     
            e.right(currentState);                          
        }

        System.out.println("Solution doesn't exist");
    }


    void add(String newState, String oldState){
        if(!stateDepth.containsKey(newState)){
            int newValue = oldState == null ? 0 : stateDepth.get(oldState) + 1;
            stateDepth.put(newState, newValue);
            agenda.add(newState);
            stateHistory.put(newState, oldState);
        }
    }

    void up(String currentState){
        int a = currentState.indexOf("0");
        if(a>2){
            String nextState = currentState.substring(0,a-3)+"0"+currentState.substring(a-2,a)+currentState.charAt(a-3)+currentState.substring(a+1);
            checkCompletion(currentState, nextState);
        }
    }

    void down(String currentState){
        int a = currentState.indexOf("0");
        if(a<6){
            String nextState = currentState.substring(0,a)+currentState.substring(a+3,a+4)+currentState.substring(a+1,a+3)+"0"+currentState.substring(a+4);
            checkCompletion(currentState, nextState);
        }
    }
    void left(String currentState){
        int a = currentState.indexOf("0");
        if(a!=0 && a!=3 && a!=6){
            String nextState = currentState.substring(0,a-1)+"0"+currentState.charAt(a-1)+currentState.substring(a+1);
            checkCompletion(currentState, nextState);
        }
    }
    void right(String currentState){
        int a = currentState.indexOf("0");
        if(a!=2 && a!=5 && a!=8){
            String nextState = currentState.substring(0,a)+currentState.charAt(a+1)+"0"+currentState.substring(a+2);
            checkCompletion(currentState, nextState);
        }
    }

    private void checkCompletion(String oldState, String newState) {
        add(newState, oldState);
        if(newState.equals("123456780")) {
            System.out.println("Solution Exists at Level "+stateDepth.get(newState)+" of the tree");
            String traceState = newState;
            while (traceState != null) {
                System.out.println(traceState + " at " + stateDepth.get(traceState));
                traceState = stateHistory.get(traceState);
            }
            System.exit(0);
        }
    }

}
0

metoda add ustawia w agenda aktualny stan. Jednocześnie currentState jest pobierany z agenda jako ostatni wpis. tu nie ma magii.

0
Koziołek napisał(a):

metoda add ustawia w agenda aktualny stan. Jednocześnie currentState jest pobierany z agenda jako ostatni wpis. tu nie ma magii.

  void up(String currentState){
        int a = currentState.indexOf("0");
        if(a>2){
            String nextState = currentState.substring(0,a-3)+"0"+currentState.substring(a-2,a)+currentState.charAt(a-3)+currentState.substring(a+1);
            checkCompletion(currentState, nextState);

Możesz mi objaśnić tą metode. Rozumiem , że a - jest w miejscu 0 indeksu 'kolejki agenda' . Jak wykonuje się warunek if(a>2) ?
"pokolorowałem" kod - bogdans

0

Po kolei:
int a = currentState.indexOf("0"); do zmiennej lokalnej a zapisz pozycję pierwszego wystąpienia znaku 0 w tekście
Jeżeli a>2 to
___do zmiennej nextState przypisz podciąg znaków od 0 do a+3 plus 0 plus podciąg znaków od a-2 do a plus znak na pozycji a-3 plus znak na pozycji a+1
poza ifem
wywołaj metodę checkCompletion(currentState, nextState);

0
Koziołek napisał(a):

Po kolei:
int a = currentState.indexOf("0"); do zmiennej lokalnej a zapisz pozycję pierwszego wystąpienia znaku 0 w tekście
Jeżeli a>2 to
___do zmiennej nextState przypisz podciąg znaków od 0 do a+3 plus 0 plus podciąg znaków od a-2 do a plus znak na pozycji a-3 plus znak na pozycji a+1
poza ifem
wywołaj metodę checkCompletion(currentState, nextState);

a propoS jestem początkujący. Jak działa w ujęciu BFS tutaj kolejka i 2 mapy ? dlaczego obiekt e.add(str, null) ma str jako klucz a wartość to 0.

0

Klucz jest dzięki temu jednoznaczny co pozwala na sprawdzenie czy dany układ miał już swoją szansę.
Wartość wynosi 0 ponieważ działa mechanizm autoboxingu, który z Integerów (obiektów) robi inty (prymitywy). O ile obiekty mogą być null to już prymitywy nie i zapisywana jest wartość domyślna 0 .

0

Co robi kolejka i mapy - hashmap i map ? ostatnie pytanie...dzięki :)

0

Map > interfejs
HashMap > implementacja

Kolejka trzyma kolejne stany do wykonania.
Mapy trzymaja historię stanów i "głębokość" czyli ścieżkę dojścia do rozwiązania.

1 użytkowników online, w tym zalogowanych: 0, gości: 1