JAVA DFS不打印出所需的行

时间:2015-11-24 14:58:10

标签: java algorithm depth-first-search

我在java中编写了一个关于n-puzzle问题的DFS问题。我有以下代码:

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;
        }
    }
}

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

上测试了它
103
256
789

start州和

130
256
789

作为goal州 该程序不打印cost(扩展的节点数),应为2.

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

1 个答案:

答案 0 :(得分:1)

您需要将语句closedlist.add(current);移动到else块的末尾。否则,每个条件都会检查closedlist是否包含current(确实如此,您只需将其放在此处),并且openlist中不会放置任何内容。

此更改后的解决方案分为两个步骤,但还有另一个错误 - 您需要全局计算cost,而如果您想找到最小值,则应分别计算每个分支移动量。