JAVA DFS 不打印出所需的行

标签 java algorithm depth-first-search

我正在用 java 编写一个 DFS 问题来解决 n-puzzle 问题。我有以下代码:

public class Dfssolution
{
    public static int cost=0;

    public static String goal;
    public static int n=3;
    public static HashSet<String> closedlist= new HashSet<String>();
    public static boolean solved = false;
    public static String current;
    public static int spc;
    public static String temp;
    public static LinkedHashSet<String> openlist = new LinkedHashSet<String>();
    public Dfssolution()
    {    
    }
    public static void main(String args []){
        String start= "103256789";
        goal = "130256789";
        String temp;
        openlist.add(start);
        while (openlist.isEmpty() == false && solved == false){
            current= openlist.iterator().next();
            openlist.remove(current);
            cost++;
            if(current.equals(goal)){
                System.out.println(cost);
                solved=true;
                break;
            }
            else{

                closedlist.add(current);
                if(checkRight(current)==true && openlist.contains(current) == false && closedlist.contains(current) == false){
                    temp= right(current);
                    openlist.add(temp);


                }
                if(checkUp(current)==true && openlist.contains(current) == false && closedlist.contains(current) == false){
                temp= up(current);
                    openlist.add(temp);

                }
                if(checkDown(current)==true && openlist.contains(current) == false && closedlist.contains(current) == false){
                temp= down(current);
                    openlist.add(temp);

                }
                if(checkLeft(current)==true && openlist.contains(current) == false && closedlist.contains(current) == false){
                temp= left(current);
                    openlist.add(temp);

                }

            }
        }
    }

    public static String left(String s){
        String tem = s;
        spc=tem.indexOf("0");

        char [] x = tem.toCharArray();
        char a = x[spc];
        x[spc] = x[spc-1];
        x[spc-1] = a;
        tem= new String(x);
        return tem;


    }
    public static String right(String s){
        String tem = s;
        spc=tem.indexOf("0");

        char [] x = tem.toCharArray();
        char a = x[spc];
        x[spc] = x[spc+1];
        x[spc+1] = a;
        tem= new String(x);
        return tem;


    }
    public static String up(String s){
        String tem = s;
        spc=tem.indexOf("0");

        char [] x = tem.toCharArray();
        char a = x[spc];
        x[spc] = x[spc-n];
        x[spc-n] = a;
        tem= new String(x);
        return tem;


    }
    public static String down(String s){
        String tem = s;
        spc=tem.indexOf("0");

        char [] x = tem.toCharArray();
        char a = x[spc];
        x[spc] = x[spc+n];
        x[spc+n] = a;
        tem= new String(x);
        return tem;

    }
    public static boolean checkUp(String s){
        spc=s.indexOf("0");
        if(spc > n-1){
            return true;
        }
        else{
            return false;
        }
    }
    public static boolean checkDown(String s){

        spc=s.indexOf("0");
        if(spc < n*(n-1)){
            return true;
        }
        else{
            return false;
        }
    }
    public static boolean checkLeft(String s){
        spc=s.indexOf("0");
        if(spc !=0 &&(spc % n) !=0){
            return true;
        }
        else{
            return false;
        }
    }
    public static boolean checkRight(String s){
        spc=s.indexOf("0");
        if(spc !=n-1 && spc % n !=n-1){
            return true;
        }
        else{
            return false;
        }
    }
}

我正在测试它是否会返回扩展的节点数(称为“成本”)以达到代码中的目标状态。我在一个简单的问题上测试了它

103
256
789

作为开始状态和

130
256
789

作为目标状态 该程序不会打印出应为 2 的 cost(展开的节点数)。

注意 n 在 3x3 拼图问题中代表 3

最佳答案

您需要将语句 closedlist.add(current); 移动到 else block 的末尾。否则,每个条件都会检查 closedlist 是否包含 current(它包含,你只是把它放在这里),并且不会将任何内容放入 openlist

此更改解决方案分两步找到,但还有另一个错误需要解决 — 您正在全局计算 cost,而如果要查找,则应为每个分支单独计算最少的移动量。

关于JAVA DFS 不打印出所需的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896908/

相关文章:

java - 复杂的正则表达式语句

java - 如何捕获图像并通过电子邮件将其 Uri 直接发送到附件

java - 为什么Java编译成汇编两次?

c - ONP 中的段错误

javascript - 在 JavaScript 中查找两个字符串之间的差异

Java UnmarshalException on shutdown/System.exit 从 MBean 通过 JConsole

java - 动态规划与背包应用

algorithm - 我应该使用广度优先还是深度优先来搜索文件系统以查找预定数量的错误?

c++ - 使用迭代DFS而不是递归DFS的第一个DFS路径

java - 测试深度优先树