Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 深度优先搜索问题_Java_Algorithm_Graph Algorithm - Fatal编程技术网

Java 深度优先搜索问题

Java 深度优先搜索问题,java,algorithm,graph-algorithm,Java,Algorithm,Graph Algorithm,我试图使用深度优先搜索来解决这个问题。我创建了一个基于3x3数组的节点类,并且正在使用一个大小为4^9的访问数组。然而,这显然根本不起作用(它没有打印任何内容)。我的调试尝试(见注释)表明,出于某种原因,数组似乎每次都在重置,但我不知道为什么 另外,我确信我的代码是不必要的复杂,并且有一种更好的方法来执行这样的DFS。有人能告诉我更好的方法吗 /* ID: akshajk1 LANG: JAVA TASK: clocks */ import java.util.*; import java.io

我试图使用深度优先搜索来解决这个问题。我创建了一个基于3x3数组的节点类,并且正在使用一个大小为4^9的访问数组。然而,这显然根本不起作用(它没有打印任何内容)。我的调试尝试(见注释)表明,出于某种原因,数组似乎每次都在重置,但我不知道为什么

另外,我确信我的代码是不必要的复杂,并且有一种更好的方法来执行这样的DFS。有人能告诉我更好的方法吗

/*
ID: akshajk1
LANG: JAVA
TASK: clocks
*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
class clocks {
    public static boolean[] visited;
    public static boolean done;
    public static String[] moves = {"ABDE", "ABC", "BCEF", "ADG", "BDEFH", "CFI", "DEGH", "GHI", "EFHI"};
    public static Stack<Integer> ans = new Stack<Integer>();
    public static Stack<Integer> finalans = new Stack<Integer>();
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 
        // BufferedReader bf = new BufferedReader(new FileReader("clocks.in"));
        // PrintWriter out = new PrintWriter(new FileWriter("clocks.out"));
        // int n = Integer.parseInt(bf.readLine());

        int[][] a = new int[3][3]; 
        for(int i=0; i<3; i++) {
            StringTokenizer st = new StringTokenizer(bf.readLine());
            for(int j=0; j<3; j++) a[i][j] = Integer.parseInt(st.nextToken());
        }
        visited = new boolean[4*4*4*4*4*4*4*4*4];
        Node x = new Node(a);
        dfs(x);
        for(int i : finalans) 
            out.println(i + " ");

        out.close(); System.exit(0);
    }
    public static void dfs(Node a) {
        if(done) return;
        int n = 0;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                n = 4*n+a.get(i, j);
        if(visited[n]) return;
        // for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(a.get(i,  j)); System.out.print("\n"); } System.out.print("\n");
        // System.out.println(Arrays.toString(ans.toArray()) + "\n");
        boolean gotit = true;
        for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(a.get(i, j) != 0) gotit = false;
        if(gotit) {
            for(int i : ans)
                finalans.push(i);
            done = true;
            return;
        }
        visited[n] = true;
        for(int i=0; i<moves.length; i++) {
            char[] x = moves[i].toCharArray();
            int[][] b = new int[3][3];
            for(int k=0; k<3; k++)
                for(int kk=0; kk<3; kk++)
                    b[k][kk] = a.get(k, kk);
            Node abc = new Node(b);
            for(char c : x) 
                abc = abc.turn(c);
            ans.push((i+1));
            dfs(abc);
        }           
        ans.pop();
    }
}
class Node {
    final private int[][] a;
    public Node(int[][] aaa) {
        a = new int[3][3];
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                a[i][j] = aaa[i][j];
    }
    public int get(int i, int j) { 
        return (a[i][j] % 12)/3;
    }
    public Node turn(char c) {
        int val = c - 'A';
        int[][] b = new int[3][3];
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                b[i][j] = a[i][j];
        b[val/3][val%3] += 3;
        if(b[val/3][val%3] > 12) b[val/3][val%3] %= 12;
        if(b[val/3][val%3] == 0) b[val/3][val%3] += 12;
        Node ansans = new Node(b);
        for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(ansans.get(i,  j)); System.out.print("\n"); } System.out.print("\n");

        return ansans;
    }
}
/*
ID:akshajk1
朗:爪哇
任务:时钟
*/
导入java.util.*;
导入java.io.*;
导入java.lang.*;
导入java.math.*;
班级时钟{
访问了[]个公共静态文件;
公共静态布尔完成;
公共静态字符串[]移动={“ABDE”、“ABC”、“BCEF”、“ADG”、“BDEFH”、“CFI”、“DEGH”、“GHI”、“EFHI”};
公共静态堆栈ans=新堆栈();
公共静态堆栈finalans=新堆栈();
公共静态void main(字符串[]args)引发异常{
BufferedReader bf=新的BufferedReader(新的InputStreamReader(System.in));
PrintWriter out=新的PrintWriter(新的OutputStreamWriter(System.out));
//BufferedReader bf=新的BufferedReader(新文件读取器(“clocks.in”);
//PrintWriter out=新的PrintWriter(新文件写入程序(“clocks.out”);
//int n=Integer.parseInt(bf.readLine());
int[]a=新int[3][3];

对于(int i=0;i您的复制是错误的。您将a复制到b的行。您将每个oft b的值设置为a.get(i,j),其中a.get()不返回其值,而是返回值%12除以3

好的,我发现对于前6000次搜索(即ans堆栈溢出时),算法总是有一个有效的移动选择

<>防止你考虑广度优先搜索或设置最大搜索深度。

我添加了一个最大搜索深度,算法返回,但所需的回合数会随着情况和范围的变化而变化。因此,有时需要200步移动,因此您不能将最大搜索深度设置为任何较低的值。但是,以200作为最大搜索深度。即使存在更好的解决方案


因此,我建议采用深度优先搜索作为搜索算法。

数组重置的确切位置在哪里?我不知道;这就是问题所在。在IDE中,放置断点,然后在调试模式下运行代码。这将允许您在执行行之前查看每行的变量。执行此操作并查看是否可以找到exac这是你的代码被破坏的地方。谢谢!但是我仍然有问题,请查看我编辑的帖子
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
class clocks {
    public static boolean[] visited;
    public static boolean done;
    public static String[] moves = {"ABDE", "ABC", "BCEF", "ADG", "BDEFH", "CFI", "DEGH", "GHI", "EFHI"};
    public static Stack<Integer> ans = new Stack<Integer>();
    public static Stack<Integer> finalans = new Stack<Integer>();
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 
        // BufferedReader bf = new BufferedReader(new FileReader("clocks.in"));
        // PrintWriter out = new PrintWriter(new FileWriter("clocks.out"));
        // int n = Integer.parseInt(bf.readLine());

        int[][] a = new int[3][3]; 
        for(int i=0; i<3; i++) {
            StringTokenizer st = new StringTokenizer(bf.readLine());
            for(int j=0; j<3; j++) a[i][j] = Integer.parseInt(st.nextToken());
        }
        visited = new boolean[4*4*4*4*4*4*4*4*4];
        Node x = new Node(a);
        int n = 0;
        for(int xx=0; xx<3; xx++)
            for(int j=0; j<3; j++)
                n = 4*n+x.get(xx, j);
        visited[n] = true;
        dfs(x);
        for(int i : finalans) 
            out.println(i + " ");

        out.close(); System.exit(0);
    }
    public static void dfs(Node a) {

        if(done) return;
        // for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(a.get(i,  j)); System.out.print("\n"); } System.out.print("\n");
        // System.out.println(Arrays.toString(ans.toArray()) + "\n");
        boolean gotit = true;
        for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(a.get(i, j) != 0) gotit = false;
        if(gotit) {
            for(int i : ans)
                finalans.push(i);
            done = true;
            return;
        }
        for(int i=0; i<moves.length; i++) {
            char[] x = moves[i].toCharArray();
            int[][] b = new int[3][3];
            for(int k=0; k<3; k++) {
                for(int kk=0; kk<3; kk++) {
                    b[k][kk] = a.get(k, kk)*3;
                    if(b[k][kk] == 0) b[k][kk] = 12;
                }
            }
            Node abc = new Node(b);
            for(char c : x) 
                abc = abc.turn(c);
            int n = 0;
            for(int xx=0; xx<3; xx++)
                for(int j=0; j<3; j++)
                    n = 4*n+abc.get(xx, j);
            if(!visited[n]) { 
                visited[n] = true;
                ans.push((i+1));
                dfs(abc);
                ans.pop();
            }
        }           
    }
}
class Node {
    final private int[][] a;
    public Node(int[][] aaa) {
        a = new int[3][3];
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                a[i][j] = aaa[i][j];
    }
    public int get(int i, int j) { 
        return (a[i][j] % 12)/3;
    }
    public Node turn(char c) {
        int val = c - 'A';
        int[][] b = new int[3][3];
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                b[i][j] = a[i][j];
        b[val/3][val%3] += 3;
        if(b[val/3][val%3] > 12) b[val/3][val%3] %= 12;
        if(b[val/3][val%3] == 0) b[val/3][val%3] += 12;
        Node ansans = new Node(b);
        // for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(ansans.get(i,  j)); System.out.print("\n"); } System.out.print("\n");

        return ansans;
    }
}