代码仅适用于 2 步,输出在 2 步后开始偏离

我编写了一个程序来检查国王可以在 K 步中移动的位置数。我有一个大小为 8×8 的棋盘,行和列从 1 到 8 标记。假设我们的国王在位置 1,3;他可以移动到 5 个新位置,并且可能会保持在当前位置,因此我们的国王总体上可以移动到 6 个位置。我们的国王可以移动的新位置的有效性可以通过公式来检查Square(r'-r)+Square(c'-c)<=2wherer'和c'are 要检查的单元格的位置。


我的代码适用于 K=1 和 2,但结果开始偏离 3 个或更多 K 值。


import java.util.Scanner;


class Chess {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int testCases;

        testCases = input.nextInt();

        while (testCases-- > 0 && testCases <= 512) {

            int R, C, K, count = 0;

            R = input.nextInt();

            C = input.nextInt();

            K = input.nextInt();

            if (R >= 1 && R <= 8 && C <= 8 && C >= 1 && K <= 8 && K >= 1) {

                for (double rowIndex = 1; rowIndex <= 8; rowIndex++) {

                    for (double columnIndex = 1; columnIndex <= 8; columnIndex++) {

                        if (Math.pow((rowIndex - R), 2) + Math.pow((columnIndex - C), 2) <= (2 * Math.pow(K, 2))) {

                            count++;

                        }

                    }

                }

            }

            System.out.println(count);

        }

    }

}


富国沪深
浏览 80回答 2
2回答

慕的地10843

我不是 100% 确定,但你知道从技术上讲,你从 1 开始 R、C 和 K,而 count 保持在 0,对吗?这是因为您在使用它们之前移动到下一个 int。我会将代码调整如下,看看是否会产生更好的结果!import java.util.Scanner;class Chess {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int testCases;&nbsp; &nbsp; &nbsp; &nbsp; testCases = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; while (testCases-- > 0 && testCases <= 512) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int R, C, K, count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (R >= 1 && R <= 8 && C <= 8 && C >= 1 && K <= 8 && K >= 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (double rowIndex = 1; rowIndex <= 8; rowIndex++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (double columnIndex = 1; columnIndex <= 8; columnIndex++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Math.pow((rowIndex - R), 2) + Math.pow((columnIndex - C), 2) <= (2 * Math.pow(K, 2))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(count);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

翻阅古今

您检查新正方形有效性的公式不正确;它不应该涉及平方。如您所见,对于K = 3,您的条件变为(r'&nbsp;-&nbsp;r)²&nbsp;+&nbsp;(c'&nbsp;-&nbsp;c)²&nbsp;≤&nbsp;2&nbsp;×&nbsp;3²&nbsp;=&nbsp;18,事实上,可以通过满足r' = r + 4和c' = c,因为 16 ≤ 18。但这意味着国王向上移动了四个方格!相反,您可以在每个方向上重申您的状况:国王可以上k台阶,但不能超过第 8排,所以国王可以到达的最上排是rmax = min(r + k, 8);同样,rmin = max(r - k, 1);同样,cmax = min(c + k, 8);同样,cmin = max(c - k, 1)。然后,您可以简单地将答案计算为(rmax - rmin + 1) × (cmax - cmin + 1)。这在直觉上是有意义的,因为有效区域应该是一个跨越行rmin到rmax和列cmin到的矩形cmax。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java