如何实现广度优先搜索树结构

时间:2018-03-31 16:07:42

标签: java recursion graph tree

这是我要解决的问题:

https://dmoj.ca/problem/dmopc14c1p5

问题提到它需要解决广度优先搜索问题。但是,我不知道如何为这个特定问题实现这样的搜索方法。我需要递归吗?我在Stackoverflow上看过其他类似的问题,但没有一个能帮助我理解这个问题。

在线解决方案都为搜索方法创建了单独的类,但由于我将解决方案提交给在线评分者,我认为这样的解决方案不会起作用。

我现在所拥有的只是收集问题输入的代码:

public static void main(String[] args) 
    {
        Scanner kbd = new Scanner(System.in);

        int y = kbd.nextInt(); //number of rows
        int x = kbd.nextInt(); //number of columns
        int initX = kbd.nextInt(); //initial starting position
        int initY = kbd.nextInt();
        int endX = kbd.nextInt(); //ending position (main office)
        int endY = kbd.nextInt();

        String [] maze = new String [y];

        //Collect all strings
        for (int i = 0; i < maze.length; i++) 
        {
            maze[i] = kbd.next();
        }

        char [] [] symbols = new char [y][x];

        //Split the strings into chars and insert into symbols 2D array
        //Letter O represents blank space and X represents wall
        for (int i = 0; i < maze.length; i++)
        {
            char [] temp = maze[i].toCharArray();
            for (int j = 0; j < symbols[0].length; j++)
            {
                symbols[i][j] = temp[j];
            }
        }

        //Collect teleporting device coordinates
        int tel = kbd.nextInt();
        int [][] telLocat = new int [tel][2];

        for (int i = 0; i < tel; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                telLocat[i][j] = kbd.nextInt();
            }
        }
}

0 个答案:

没有答案