首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用DFS解决8益智游戏

使用DFS解决8益智游戏
EN

Stack Overflow用户
提问于 2013-10-28 19:54:01
回答 1查看 8.8K关注 0票数 3

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

代码语言:javascript
运行
复制
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);
    }
}

}
EN

回答 1

Stack Overflow用户

发布于 2013-10-28 20:09:47

DFS不是解决8个难题的最佳方法,但是,

有了这些代码,您应该只需更改main方法nextState并添加一个goDepth方法就可以将其实现为DFS。

我建议分析这段代码,然后尝试在纸上可视化它是如何工作的。在您确切地了解它是如何工作的之后,您可以在其中实现DFS算法,这是没有问题的。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19634147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档