【问题标题】:Java BFS - What should I change if I wanted to move diagonally?Java BFS - 如果我想对角移动,我应该改变什么?
【发布时间】:2019-06-03 12:25:55
【问题描述】:

我正在查看这段代码,但我真的不知道如何实现对角线移动。 我试过改变可能的动作,但我不知道该怎么做。 预期结果是代码尽可能寻找对角线移动的最短路径以降低总成本。 抱歉代码太长,谢谢。

{
    // (x, y) represents matrix cell coordinates
    // dist represent its minimum distance from the source
    int x, y, dist;

    Node(int x, int y, int dist) {
        this.x = x;
        this.y = y;
        this.dist = dist;
    }
};

class Main
{
    // M x N matrix
    private static final int M = 10;
    private static final int N = 10;

    // Possible movements
    private static final int row[] = { -1, 0, 0, 1 };
    private static final int col[] = { 0, -1, 1, 0 };

    // Function to check if it is possible to go to position (row, col)
    private static boolean isValid(int mat[][], boolean visited[][], int row, int col)
    {
        return (row >= 0) && (row < M) && (col >= 0) && (col < N)
                        && mat[row][col] == 1 && !visited[row][col];
    }

    // Find Shortest Possible Route
    // cell (i, j) to cell (x, y)
    private static void BFS(int mat[][], int i, int j, int x, int y)
    {
        // construct a matrix to keep track of visited cells
        boolean[][] visited = new boolean[M][N];

        // create an empty queue
        Queue<Node> q = new ArrayDeque<>();

        // mark source cell as visited and enqueue the source node
        visited[i][j] = true;
        q.add(new Node(i, j, 0));

        // stores length of longest path from source to destination
        int min_dist = Integer.MAX_VALUE;

        // run till queue is not empty
        while (!q.isEmpty())
        {
            // pop front node from queue and process it
            Node node = q.poll();

            // (i, j) represents current cell and dist stores its
            // minimum distance from the source
            i = node.x;
            j = node.y;
            int dist = node.dist;

            // if destination is found, update min_dist and stop
            if (i == x && j == y)
            {
                min_dist = dist;
                break;
            }

            // check for all 4 possible movements from current cell
            // and enqueue each valid movement
            for (int k = 0; k < 4; k++)
            {
                // check if it is possible to go to position
                // (i + row[k], j + col[k]) from current position
                if (isValid(mat, visited, i + row[k], j + col[k]))
                {
                    // mark next cell as visited and enqueue it
                    visited[i + row[k]][j + col[k]] = true;
                    q.add(new Node(i + row[k], j + col[k], dist + 1));
                }
            }
        }

        if (min_dist != Integer.MAX_VALUE) {
            System.out.print("The shortest path from source to destination " +
                                     "has length " + min_dist);
        }
        else {
            System.out.print("Destination can't be reached from given source");
        }
    }
}

【问题讨论】:

  • 您能否缩小您的问题范围,以便我们真正回答它?就目前而言,这只是一个大代码转储,我们必须花费大量时间分析和弄清楚什么是什么以及为什么。
  • 您必须将该动作添加到动作数组(rowcol),并将 k 的范围增加到新的限制

标签: java matrix breadth-first-search


【解决方案1】:

一个运动矩阵看起来像

(-1, -1) (-1,  0) (-1,  1)
( 0, -1) ( r,  c) ( 0,  1)
( 1, -1) ( 1,  0) ( 1,  1)

在哪里

r - is the current row
c - is the current column

这些值表示需要添加到当前位置才能到达那里的数量

在此基础上你可以扩展你的功能

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2023-04-01
    • 1970-01-01
    • 2014-09-18
    • 2015-01-07
    • 1970-01-01
    • 2021-05-02
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多