【问题标题】:Locker App Method储物柜应用方法
【发布时间】:2016-06-20 15:44:07
【问题描述】:

这是我正在构建的一个储物柜应用程序,可确保放置储物柜,以便城市中的客户始终与城市中的储物柜保持很短的距离。考虑到这一点,我需要一种方法来模拟储物柜的位置和与储物柜的距离。

为此,我提供以下内容:

  1. 一个正整数范围 1-9,表示您所在城市的街区长度
  2. 一个正整数范围 1-9,表示您所在城市的街区宽度
  3. 一个数组,包含代表储物柜位置的所有 X 坐标,每个 X 坐标范围为 1-9。
  4. 一个数组,包含代表储物柜位置的所有 Y 坐标,每个 Y 坐标范围为 1-9。

这里的工作是构建城市的 2D 网格。网格的每个元素都应该是一个正整数,用于指定壁橱储物柜的块数。两个块之间的距离是它们水平和垂直距离的总和(因此,对角线方向的移动被认为是距离 2)。将您的网格返回为整数的二维数组,其中第一个索引对应于 X 维度,第二个索引对应于 Y 方向。

示例 #1:

给定:

  1. 3
  2. 5
  3. [1]
  4. [1]

返回:

012
123
234
345
456

示例 #2:

给定:

  1. 5
  2. 7
  3. [2,4]
  4. [3,7]

返回:

32345
21234
10123
21234
32323
43212
32101

这是我被要求使用的方法:

静态 int[][] getLockerDistanceGrid(int cityLength, int cityWidth, int[] lockerXCoordinates, int[] lockerYCoordinates) { }

我该怎么做?任何帮助将不胜感激!

【问题讨论】:

  • 这是一个非常广泛的问题......至少告诉我们你做了什么。
  • 一个好的开始是遍历所有位置(两个堆叠的 for 循环可以很好地解决问题),然后在每个位置,通过比较你的位置和位置来确定最近的储物柜在哪里在列表中给出(这应该可以通过一些非常简单的算术来实现)。
  • 我还没有编写任何代码。我不知道如何开始。
  • 您可以通过声明输出表所需的数组来启动该方法:array = new int[cityWidth][]; for(int i=0; i < cityWidth; ++i ) { array[i] = new int[cityLength]; } 或者您可以将其编码为交换宽度和长度。

标签: java arrays


【解决方案1】:

我用稍微修改的 BFS 解决了这个问题

private static class Node {
    public Node (int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int x, y;
}

static int[][] getLockerDistanceGridBfs(int cityLength, int cityWidth, int[] lockerXCoordinates, int[] lockerYCoordinates) {
    if (lockerXCoordinates == null || lockerYCoordinates == null || lockerXCoordinates.length != lockerYCoordinates.length)
        return null;

    int[][] city = new int[cityWidth][cityLength];
    for (int i = 0; i < city[0].length; i++)
        for (int j = 0; j < city.length; j++)
            city[j][i] = Integer.MAX_VALUE;

    Queue<Node> nodesToDo = new LinkedList<>();
    for (int i = 0; i < lockerXCoordinates.length; i++) {
        city[lockerXCoordinates[i] - 1][lockerYCoordinates[i] - 1] = 0;
        nodesToDo.add(new Node(lockerXCoordinates[i] - 1, lockerYCoordinates[i] - 1));
    }

    while(!nodesToDo.isEmpty()) {
        Node v = nodesToDo.poll();
        int newDist = city[v.x][v.y] + 1;
        List<Node> neighbours = getNeighbours(cityLength, cityWidth, v.x, v.y);
        for(Node node : neighbours) {
            if(newDist < city[node.x][node.y]) {
                city[node.x][node.y] = newDist;
                nodesToDo.add(node);
            }
        }
    }

    return city;
}

private static List<Node> getNeighbours(int cityLength, int cityWidth, int x, int y) {
    List<Node> result = new ArrayList<>(4);
    if(x - 1 >= 0) {
        result.add(new Node(x - 1, y));
    }
    if(y - 1 >= 0) {
        result.add(new Node(x, y - 1));
    }
    if(x + 1 < cityWidth) {
        result.add(new Node(x + 1, y));
    }
    if(y + 1 < cityLength) {
        result.add(new Node(x, y + 1));
    }

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多