试图找到2D阵列中最近的可用点

时间:2016-04-27 08:47:03

标签: java

我正在努力找到最接近的座位。但是,我确实尝试了一些东西,但是我找不到它,到目前为止,经过一些格式化后我回到了我的明星位置。任何建议。!

public void nextFree(String seats) {
        int colA = 0;
        int rowA = Integer.parseInt(seats.substring(1, 2));
        int newColB = 0;
        int newRowB = 0;

        if (seats.equalsIgnoreCase("a")) {
            colA = 1;
        } else if (seats.equalsIgnoreCase("b")) {
            colA = 2;
        } else if (seats.equalsIgnoreCase("c")) {
            colA = 3;
        } else {
            colA = 4;
        }

        for (int i = 0; i < this.table.length; i++) {
            for (int k = 0; k < this.table.length; k++) {
                if (table[i][k] == "XX") {
                    newRowB = i;
                    newColB = k;
                }
            }
            System.out.print("The seat " + colA + rowA + " is not available! The next availale is " + newColB + newRowB);
        }

    }

3 个答案:

答案 0 :(得分:1)

你的主要问题在于:

for (int i = 0; i < this.table.length; i++) {
  for (int k = 0; k < this.table.length; k++)

看看这两个循环是如何走同一距离的?这肯定不是正确的。试试这个:

for (int i = 0; i < table.length; i++) {
  for (int k = 0; k < table[i].length; k++)

现在循环每行(table.length),然后遍历该行中的每一列(table[i].length)。

同样table[i][k] == "XX"应该是table[i][k].equals("XX")

答案 1 :(得分:0)

public void nextFree(String seats) {
    int colA = 0;
    int rowA = Integer.parseInt(seats.substring(1, 2));
    int newColB = 0;
    int newRowB = 0;
    int distance = -1;
    //Would recommend changing this to a switch statement, might lake it clearer
    if (seats.equalsIgnoreCase("a")) {
        colA = 1;
    } else if (seats.equalsIgnoreCase("b")) {
        colA = 2;
    } else if (seats.equalsIgnoreCase("c")) {
        colA = 3;
    } else {
        colA = 4;
    }

    for (int i = 0; i < table.length; i++) {
        for (int k = 0; k < table[i].length; k++){
            //Calculating current distance away from our chair
            //This assumes i is the column and k is the row, you may need to swap those values around
            int curDistance = Math.abs(i-colA) + Math.abs(k-rowA);
            //If the seat is free and the distance hasn't been set or the current distance is less than the previous distance
            if (table[i][k].equals("--") && (distance == -1 || distance > curDistance) {
                //Found a closer seat
                newRowB = k;
                newColB = i;
                distance = curDistance;
            }
        }
    }
    //Moved this out the for loop, it shouldn't have been inside
    System.out.print("The seat " + colA + rowA + " is not available! The next availale is " + newColB + newRowB);
}

使用曼哈顿距离详细here

此代码并不假设您关心行,如果座位位于当前座位后面并且可用,则可以认为它比同一行中的座位更近

与主席相比,曼哈顿距离给出以下结果:

            Row   Seat  Distance
Master       1     1       0
Next to      1     2       1
Behind       2     1       1
In front     0     1       1
Diagonal     2     2       2

证明它对我有用:

Taking seats

Results

答案 2 :(得分:-1)

您尝试从两个字符的字符串中确定rowA。

然后尝试从单字符字符串中确定colA。你可能应该在这里使用String.startsWith()。