La modification de cette 8-puzzle code pour imprimer les états intermédiaires à atteindre la solution
//Largeur de la Première Utilisation de la Recherche dans la commune de Huit Puzzle Problème.
import java.util.*;
class EightPuzzle {
Queue<String> q = new LinkedList<String>(); //Use of Queue Implemented using LinkedList for Storing All the Nodes in BFS.
Map<String,Integer> map = new HashMap<String, Integer>(); //HashMap is used to ignore repeated nodes
public static void main(String args[]){
String str="087465132"; //Input the Board State as a String with 0 as the Blank Space
EightPuzzle e = new EightPuzzle(); //New Instance of the EightPuzzle
e.add(str,0); //Add the Initial State
while(e.q.peek()!=null){
e.up(e.q.peek()); //Move the blank space up and add new state to queue
e.down(e.q.peek()); //Move the blank space down
e.left(e.q.peek()); //Move left
e.right(e.q.remove()); //Move right and remove the current node from Queue
}
System.out.println("Solution doesn't exist");
}
//Add method to add the new string to the Map and Queue
void add(String str,int n){
if(!map.containsKey(str)){
map.put(str,n);
q.add(str);
}
}
/* Each of the Methods below Takes the Current State of Board as String. Then the operation to move the blank space is done if possible.
After that the new string is added to the map and queue.If it is the Goal State then the Program Terminates.
*/
void up(String str){
int a = str.indexOf("0");
if(a>2){
String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt(a-3)+str.substring(a+1);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}
}
}
void down(String str){
int a = str.indexOf("0");
if(a<6){
String s = str.substring(0,a)+str.substring(a+3,a+4)+str.substring(a+1,a+3)+"0"+str.substring(a+4);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}
}
}
void left(String str){
int a = str.indexOf("0");
if(a!=0 && a!=3 && a!=6){
String s = str.substring(0,a-1)+"0"+str.charAt(a-1)+str.substring(a+1);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}
}
}
void right(String str){
int a = str.indexOf("0");
if(a!=2 && a!=5 && a!=8){
String s = str.substring(0,a)+str.charAt(a+1)+"0"+str.substring(a+2);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}
}
}
}
Je veux modifier le code pour qu'il imprime les états intermédiaires utilisés pour atteindre la solution, au lieu de dire le niveau sur lequel la solution a été atteint.
Par exemple, étant donné ce conseil
1 4 2
3 0 5
6 7 8
(sous forme de Chaîne 142305678)
Je veux imprimer:
1 4 2
3 0 5
6 7 8
(comme la Chaîne 142305678)
1 0 2
3 4 5
6 7 8
(comme la Chaîne 102345678)
0 1 2
3 4 5
6 7 8
(comme la Chaîne 012345678)
En regardant le code, je crois que c'intermédiaire de chaînes se sont stockées par le biais de la méthode add dans la File d'attente:
void add(String str,int n){
if(!map.containsKey(str)){
map.put(str,n);
q.add(str);
}
}
Je n'ai aucune expérience de travail avec table de hachage, comment pourrais-je regarder dans les états intermédiaires qui y sont stockées?
Vous devez vous connecter pour publier un commentaire.
Voici comment j'ai résolu votre requête pour obtenir la piste reliant commencer à objectif:
stateHistory
carte d'associer chaque état à son prédécesseur.checkCompletion
méthode.add
méthode pour prendre de nouveaux et anciens états (intériorisé la profondeur de recherche à cette méthode) et pour maintenir lastateHistory
carte ainsi que l'stateDepth
carte etagenda
file d'attente.checkCompletion
méthode à pied de lastateHistory
arrière de l'objectif d'état (une fois) à l'état d'origine (celle qui n'a aucun prédécesseur).L'exécution de la modification du code produit de cette sortie:
Modification d'imprimer la séquence de avant la commande (démarrer-à-objectif, plutôt que vers l'arrière de l'objectif-à-start) est laissée en exercice de l'étudiant.
En fait, la solution semble être stockées dans la file d'attente. La carte est uniquement utilisé pour détecter plusieurs états.
Quand il est terminé, pop chaque élément de la file d'attente et l'imprimer.
Une petite amélioration à Joel Neely version: initialisation
e.stateDepth.put(null, -1);
simplifie la logique dans la méthode add,int newValue = stateDepth.get(oldState) + 1;
.Vous avez juste besoin d'ajouter une ligne de code pour imprimer les états intermédiaires.
Dans la fonction ajouter, insérer le code
L'ensemble du code ressemblera à ceci.
La sortie ressemble à ceci.sortie
Cependant, il imprime aussi les états qui ne contribuent pas à atteindre l'objectif de l'état.