gpt4 book ai didi

java - 查找二维数组相对于任意位置的对角线边缘 - Java

转载 作者:行者123 更新时间:2023-12-02 03:28:22 25 4
gpt4 key购买 nike

我有一个 6x6 的数字数组:

        int[][] multi = new int[][]{
{4, 2, 3, 2, 5, 1},
{2, 5, 5, 4, 1, 1},
{2, 4, 6, 7, 2, 4},
{2, 1, 2, 3, 4, 3},
{3, 5, 1, 4, 5, 2},
{1, 2, 1, 4, 1, 2}
};

如果我的起始位置是multi[2][3]。如何找到数组相对于该值的对角线边缘?例如,在点 multi[2][3] 处,值为 7。对角线应为点 multi[0][1]multi[0 ][5]multi[4][5]multi[5][0]。这是我的代码当前的作用:

if (LocationValue == 7) {//find possible moves
//There should be 4 potential moves

ArrayList<Point> Moves = new ArrayList<Point>();
Point DMove;

for (int i = 0; i < multi.length; i++) {
DMove = new Point(x + i, y + i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x - i, y + i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x - i, y - i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x + i, y - i);
Moves.add(new Point(DMove));
}

ArrayList<Point> AlmostFinalMoves = FindPossibleMoves(Moves); //eliminate impossible moves
ArrayList<Point> FinalMoves = FindSideMoves(AlmostFinalMoves, x, y); //Get bishop moves
System.out.println("Possible Moves: " + FinalMoves);

}//End of IF

此方法然后消除不可能的值:

    public static ArrayList<Point> FindPossibleMoves(ArrayList<Point> AllMoves) {

ArrayList<Point> FinalMoves = new ArrayList<Point>();

for (int i = 0; i < AllMoves.size(); i++) {

if (AllMoves.get(i).getX() >= 0 && AllMoves.get(i).getX() <= 5 && AllMoves.get(i).getY() >= 0 && AllMoves.get(i).getY() <= 5) {
FinalMoves.add(AllMoves.get(i));
}
}

return FinalMoves;
}

最后,此方法消除了不在数组边缘的所有移动。

    public static ArrayList<Point> FindSideMoves(ArrayList<Point> AllPossibleMoves, int xloc, int yloc) {

ArrayList<Point> AlmostFinalSideMoves = new ArrayList<Point>();
ArrayList<Point> FinalSideMoves = new ArrayList<Point>();

for (int i = 0; i < AllPossibleMoves.size(); i++) {

if (AllPossibleMoves.get(i).getX() == 0) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 5) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 1) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 2) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 3) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 4) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
}

for (int i = 0; i < AlmostFinalSideMoves.size(); i++) {//Check to see if any possible moves match the original location. If so, do not include in list
if (AlmostFinalSideMoves.get(i).getX() == xloc && AlmostFinalSideMoves.get(i).getY() == yloc) {
//Do Nothing!
} else {
FinalSideMoves.add(AlmostFinalSideMoves.get(i));
}
}

return FinalSideMoves;
}

运行此程序会导致以下结果不正确。

Possible Moves: [java.awt.Point[x=0,y=3], java.awt.Point[x=0,y=5], java.awt.Point[x=2,y=5], java.awt.Point[x=4,y=5], java.awt.Point[x=5,y=3], java.awt.Point[x=5,y=0], java.awt.Point[x=2,y=0], java.awt.Point[x=0,y=1]]

查找二维方形数组中任意随机点的对角线的最简单方法是什么?另外,关于如何简化我的代码的建议将不胜感激。

谢谢!

最佳答案

如果索引之一等于 0 或数组的长度/高度,则数字位于正方形的边缘。假设数组已经是一个正方形(您可以自己检查):

int length = grid.length;
int height = grid[0].length;

假设您有原点的 x 和 y 坐标:

List<Point> findPossibleMoves(int x, int y) {
int length = grid.length;
int height = grid[0].length;
int originalX = x;
int originalY = y;
//add (1, 1), (-1, 1), (-1, -1), (1, -1) to the original position until you reach an edge
}

但是等等,我们真的需要循环吗?如果我们直接向 x 和 y 点添加某个值以一步到达边缘会怎么样?我们如何做到这一点?

以 (2, 3) 为例(数组中的值 = 7)。为了找到 (0, 0) 角点,我们使用以下逻辑:

  • x = 2 比 y 更接近 0,因此我们得到 (x - x, y - x) = (0, 1)
  • 这适用于 y 比 x 更接近 0 的情况:(x - y, y - y) = (x - y, 0)

将此应用到(长度,高度)角:

  • (y = 3) 比 (x = 2) 更接近 (height = 5),所以我们得到 (x + (height - y), (y + (height - y)) = (4, 5)

所有 4 个角都转换为代码:

List<Point> findPossibleMoves(int x, int y) {
List<Point> pointList = new ArrayList<Point>();
int length = grid.length;
int height = grid[0].length;

pointList.add(new Point(x - Math.min(x, y), y - Math.min(x, y)));
pointList.add(new Point(x + Math.min(length - x, y), y - Math.min(length - x, y)));
pointList.add(new Point(x - Math.min(x, height - y), y + Math.min(x, height - y)));
pointList.add(new Point(x + Math.min(length - x, height - y), y + Math.min(length - x, height - y)));

return pointList;
}

可以清理到:

List<Point> findPossibleMoves(int x, int y) {
List<Point> pointList = new ArrayList<Point>();
int length = grid.length;
int height = grid[0].length;

int to00 = Math.min(x, y);
int toL0 = Math.min(length - x, y);
int to0H = Math.min(x, height - y);
int toLH = Math.min(length - x, height - y);
pointList.add(new Point(x - to00, y - to00));
pointList.add(new Point(x + toL0, y - toL0));
pointList.add(new Point(x - to0H, y + to0H));
pointList.add(new Point(x + toLH, y + toLH));

return pointList;
}

关于java - 查找二维数组相对于任意位置的对角线边缘 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38468430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com