【问题标题】:Using 2 input coordinates of the chess board, identify whether or not they crossed each other's path (using queen moves of the chess game)使用国际象棋棋盘的 2 个输入坐标,确定它们是否越过彼此的路径(使用国际象棋游戏的皇后移动)
【发布时间】:2018-08-29 07:49:37
【问题描述】:
class Board 
{
     public static void main(String args[]) 
    {

        int i, j;
        int x1 = 0, y1 = 0;
        int x2 = 0, y2 = 0;
        int[][] board = new int[8][8];

        x1 = Integer.parseInt(args[0]); 
        y1 = Integer.parseInt(args[1]); 
        x2 = Integer.parseInt(args[2]); 
        y2 = Integer.parseInt(args[3]); 


        // initialize the board to 0's
        for (i = 0; i < 8; i++)
            for (j = 0; j < 8; j++)
                board[i][j] = 0;

        board[x1][y1] = 1;      
        board[x2][y2] = 1;      

        for (i = 0; i < 8; i++) 
        {
            for (j = 0; j < 8; j++) 
            {
                System.out.print(board[i][j]+" ");
            }
            System.out.println();
        }   

    }
}

这是我唯一能做的,就是用 0 和 1 打印电路板

板:

0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1

我的目标是编码并确定 2 个皇后(即两个 1)是否会相互交叉。

我尝试了很多方法,但其中一些不起作用。 如果你能帮助我,我真的很感激:)

P.S 还在学习编码 :)

【问题讨论】:

  • 提示:将问题分解为更小的问题。你的目标是选择两个皇后中的一个,并检查它是否可以在 8 个可能的方向上“看到”另一个。编写一个可以检查单个方向的通用函数,并使用不同的参数调用它 8 次。如果其中任何一个是阳性的,则皇后会相互交叉
  • 谢谢,我会试试的。 :)

标签: java arrays chess


【解决方案1】:

欢迎来到 StackOverflow :)

这里就是你要找的东西:

public static boolean twoQueensSeeEachOther(int x1, int y1, int x2, int y2) {
    if (x1 == x2 || y1 == y2) {
        return true;                                // One has picked another
    }
    if (x1 == x2 || y1 == y2) {
        return true;                                // Row or column
    }
    if (Math.abs(x1 - x2) == Math.abs(y1 - y2)) {
        return true;                                // Diagonal
    }
    return false;
}

两个皇后可以看到对方有这些条件:

  • 如果他们都在同一个地方,一个选择另一个

  • 如果它们共享相同的轴(x 或 y),它们会看到彼此,因为它们可以像车一样移动。如果它们的x 或y 位置相同,则满足此条件。

  • 如果他们共享相同的对角线,他们会看到对方,因为他们可以像主教一样移动。如果轴之间的差异相等,则满足此条件。示例:

    • 黑皇后在[2,5]位置,白皇后在[4,3]位置。
    • x 轴之间的差异是xDiff = abs(2 - 4) = 2。
    • y 轴之间的差异是yDiff = abs(5 - 3) = 2。
    • 两者的差异是相等的 - 他们在对角线上看到对方。

【讨论】:

  • 好的,谢谢您的帮助
【解决方案2】:
import java.util.Scanner;
class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
        int x1 = scanner.nextByte();
        int y1 = scanner.nextByte();
        int x2 = scanner.nextByte();
        int y2 = scanner.nextByte();
        boolean sameRow = y1 == y2;
        boolean sameColumn = x1 == x2;
        boolean canAttack;

        if (sameRow || sameColumn) {
            canAttack = true;
        } else {
            canAttack = Math.abs(x1 - x2) == Math.abs(y1 - y2);
        }
        System.out.println(canAttack ? "YES" : "NO");

}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多