写了一个矩阵,返回周围位置的位置 [英] Write a matrix that returns the position of the surrounding positions

查看:240
本文介绍了写了一个矩阵,返回周围位置的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人点我在写方向。我在概念上理解这个问题,但不知道如何实现。

Could someone point me in the write direction. I understand the problem conceptually but don't know how to implement.

编写类MatrixNeighbors具有以下字段:

Write a class MatrixNeighbors with the following fields:

int rows
int columns
int[][] matrix

下面的构造:

the following constructor:

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>;".

拉​​手一个不存在的细胞,一个存在于矩阵边界之外,通过返回字符串单元不存在。

Handle a nonexistent cell, one that exists outside of the matrix boundaries, by returning the string "cell does not exist".

考虑下面的图和code:

Consider the following diagram and code:

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

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

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

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

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

(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);

    }

我相信这是现在正在返回现场一排上面的那一个位置,但我不知道如何确保它只返回,如果这个位置是矩阵里面,然后如何到现场返回右边的新点的。

I believe this is now returning the position of the spot one row above that one, but I don't know how to make sure it only returns it if that spot is inside the matrix and then how to return the spot to the right of the new spot.

推荐答案

您应该能够检查位置是否是使用try-catch块有效。例如下列方法使用的try-catch来验证这是考虑到的方法中的位置,然后检查该位置是否存在。 在任何情况下,try块将试图在设定值的位置,如果它抛出一个异常,这意味着它不存在,所以它不会被添加到字符串。这最终会显示您的有效阵地。这是我想到的最好的办法,但我是pretty的确定必须有使用循环更简单的方法,但它是由你来调查。

You should be able to check whether a position is valid by using a try-catch block. For example the following method uses try-catch to validate the position which is given to the method, and then checks whether the positions exist. In all cases, try block will try to set a value to the position, if it throws an exception it means it doesn't exist so it's not added to the string. This will end up showing you the valid positions. It's the best way I've thought about, however I'm pretty sure there must be an easier method using a loop, but it's up to you to investigate.

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;
    }
}

这篇关于写了一个矩阵,返回周围位置的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆