Java 用DFS解决8字谜游戏

Java 用DFS解决8字谜游戏,java,breadth-first-search,depth-first-search,sliding-tile-puzzle,Java,Breadth First Search,Depth First Search,Sliding Tile Puzzle,我试图解决DFS的8个难题,从BFS实现的代码开始。最简单的方法是什么?我研究过的所有代码要么都在工作,要么都不完整,这让我比以前更加困惑 import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; class EightPuzzle { Queue<String> agenda = new LinkedList<String>

我试图解决DFS的8个难题,从BFS实现的代码开始。最简单的方法是什么?我研究过的所有代码要么都在工作,要么都不完整,这让我比以前更加困惑

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

class EightPuzzle {

Queue<String> agenda = new LinkedList<String>();    // Use of Queue Implemented using LinkedList for Storing All the Nodes in BFS.
Map<String,Integer> stateDepth = new HashMap<String, Integer>(); // HashMap is used to ignore repeated nodes
Map<String,String> stateHistory = new HashMap<String,String>(); // relates each position to its predecessor

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, null);                                                   // Add the Initial State

    while(!e.agenda.isEmpty()){
        String currentState = e.agenda.remove();
        e.up(currentState);                                       // Move the blank space up and add new state to queue
        e.down(currentState);                                     // Move the blank space down
        e.left(currentState);                                     // Move left
        e.right(currentState);                          // 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 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);
    }
}

/* 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 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);
    }
}

}
import java.util.HashMap;
导入java.util.LinkedList;
导入java.util.Map;
导入java.util.Queue;
第八类{
Queue agenda=new LinkedList();//使用LinkedList实现的队列存储BFS中的所有节点。
Map stateDepth=new HashMap();//HashMap用于忽略重复的节点
Map stateHistory=new HashMap();//将每个位置与其前身关联
公共静态void main(字符串参数[]){
字符串STR=“087465132”;//将板状态输入为0的空白字符串。
EightPuzzle=新的EightPuzzle();//EightPuzzle的新实例
e、 add(str,null);//添加初始状态
而(!e.agenda.isEmpty()){
字符串currentState=e.agenda.remove();
E. UP(CurrnType);/ /移动空白空间并向队列添加新的状态
E.向下(当前状态);/ /向下移动空白区域
e、 左(当前状态);//向左移动
e、 右(currentState);//向右移动并从队列中删除当前节点
}
System.out.println(“解决方案不存在”);
}
//Add方法将新字符串添加到映射和队列中
void add(字符串newState、字符串oldState){
如果(!stateDepth.containsKey(newState)){
int newValue=oldState==null?0:stateDepth.get(oldState)+1;
stateDepth.put(newState,newValue);
议程.增补(新闻状态);
stateHistory.put(newState、oldState);
}
}
*下面的每一个方法都将当前的板状态作为字符串,如果可能的话,就可以完成移动空白空间的操作。
之后,新字符串被添加到映射和队列中。如果它是目标状态,则程序终止。
*/
作废(字符串当前状态){
int a=当前状态.indexOf(“0”);
如果(a>2){
字符串nextState=currentState.substring(0,a-3)+“0”+currentState.substring(a-2,a)+currentState.charAt(a-3)+currentState.substring(a+1);
检查完成(当前状态、下一状态);
}
}
向下作废(字符串当前状态){
int a=当前状态.indexOf(“0”);

如果(aDFS不是解决8字谜的最佳方法,但是

有了这些代码,您应该能够通过只更改main方法nextState并添加goDepth方法将其实现为DFS


我建议您先分析这段代码,然后试着在纸上可视化它是如何工作的。在您完全理解了它的工作原理之后,您就可以毫无问题地在其中实现DFS算法了。

我知道这并不是我必须要用的,而且它很乏味,lisp版本的代码需要很长时间才能运行,以确定它是否工作。因此我正在尝试g这个。