【问题标题】:Write a matrix that returns the position of the surrounding positions写一个矩阵,返回周围位置的位置
【发布时间】:2015-09-22 14:25:57
【问题描述】:

谁能指出我的写作方向。我从概念上理解这个问题,但不知道如何实现。

使用以下字段编写一个类 MatrixNeighbors:

int rows
int columns
int[][] matrix

以下构造函数:

MatrixNeighbors(int rows, int columns) : sets both the rows and columns fields, then creates a new matrix of size [rows]x[columns].

还有以下方法:

String neighbors(int row, int column) : returns a String containing all the cell neighbors of the cell at row, column, starting directly above the specified cell and moving clockwise. If the cell is along any edge, be sure to omit the non-existent neighbors. See below for formatting.
Strings will be formatted as such: “<above cell row>,<above cell column>;<next cell clockwise row>,<next cell clockwise column>;…<last cell row>,<last cell column>;”.

通过返回字符串“cell不存在”来处理不存在的单元格,即存在于矩阵边界之外的单元格。

考虑下面的图表和代码:

(0,0) (1,0) (2,0)

(0,1) (1,1) (2,1)

(0,2) (1,2) (2,2)

MatrixNeighbors m = new MatrixNeighbors(3, 3);
System.out.println(m.neighbors(0, 0)); //prints "0,1;1,1;1,0;"
System.out.println(m.neighbors(2, 2)); //prints "1,2;2,1;1,1;"
System.out.println(m.neighbors(1, 1)); //prints "0,1;0,2;1,2;2,2;2,1;2,0;1,0;0,0;"
System.out.println(m.neighbors(3, 0)); //prints "cell does not exist"

我有什么:

public class MatrixNeighbors {
   int rows = 0;
    int columns = 0;
    int [][] matrix;

    MatrixNeighbors(int row, int column) {
        for (int i = 0; i < MatrixNeighbors.length; i++)
            for (int j = 0; j < MatrixNeighbors[i].length; j++)
                if (i <= row && j <= column) 
                    return (row - 1, column);

    }

我相信这是现在返回点的位置在该点上方的一行,但我不知道如何确保它仅在该点位于矩阵内时才返回它,然后如何将该点返回到新地点的右边。

【问题讨论】:

    标签: java arrays matrix 2d


    【解决方案1】:

    您可以只使用数学来确定周围的人

    • 检查给定点是否在 matix 内
    • 检查顶部邻居是否存在 -> 添加到字符串
    • 检查右邻是否存在 -> 添加到字符串
    • 打印字符串

    没有快速功能。我知道

    【讨论】:

      【解决方案2】:

      您应该能够通过使用 try-catch 块来检查位置是否有效。例如下面的方法使用 try-catch 来验证给方法的位置,然后检查位置是否存在。 在所有情况下,try 块都会尝试为该位置设置一个值,如果它抛出异常,则意味着它不存在,因此它不会添加到字符串中。这将最终向您显示有效职位。这是我想到的最好的方法,但是我很确定一定有一个更简单的方法使用循环,但这取决于你的调查。

      public String neighbors(int row, int column)
      {
          String ret = "";
          try
          {
               matrix[row][column] = 0; // To check if the row exists we just set a dummy value.
               try { matrix[row][column - 1] = 0; ret += row + "," + (column-1) + ";"; }
               catch (Exception e) {} 
               try { matrix[row - 1][column - 1] = 0; ret += (row-1) + "," + (column-1) + ";"; }
               catch (Exception e) {} 
               try { matrix[row - 1][column] = 0; ret += (row-1) + "," + column + ";"; }
               catch (Exception e) {} 
               try { matrix[row - 1][column + 1] = 0; ret += (row-1) + "," + (column+1) + ";"; }
               catch (Exception e) {} 
               try { matrix[row][column + 1] = 0; ret += row + "," + (column+1) + ";"; }
               catch (Exception e) {} 
               try { matrix[row + 1][column + 1] = 0; ret += (row+1) + "," + (column+1) + ";"; }
               catch (Exception e) {} 
               try { matrix[row + 1][column] = 0; ret += (row+1) + "," + column + ";"; }
               catch (Exception e) {} 
               try { matrix[row + 1][column - 1] = 0; ret += (row+1) + "," + (column-1) + ";"; }
               catch (Exception e) {} 
          }
          catch (Exception e)
          {
              ret += "Cell does not exist."; // Woho, exception caused due to position non-existing in matrix.
          }
          finally
          {
              return ret;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-08
        • 1970-01-01
        相关资源
        最近更新 更多