Code only works fine for 2 steps and the output begins to deviate after 2 steps

问题: I have made a program to check the number of positions a king can move in K steps. I have a chessboard of size 8×8 with rows and columns marked from 1 to 8. Suppose our kin...

问题:

I have made a program to check the number of positions a king can move in K steps. I have a chessboard of size 8×8 with rows and columns marked from 1 to 8. Suppose our king is at position 1,3; he can move to 5 new places and may remain to the current position, so overall our king can move to 6 places. The validity of the new place where our king can move can be checked by the formula Square(r'-r)+Square(c'-c)<=2 where r' and c' are the positions of cells to be checked.

My code works fine for K= 1 and 2, however the results begin to deviate for 3 or more values of 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);
        }
    }
}

回答1:

Im not 100% sure, but you do know that you technically start R,C, and K at 1, while count remains at 0, right? This is because you move to the next int, before working with them.

I would adjust the code to as follow, and see if that yields better results!

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;
            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++;
                        }
                    }
                }
            R = input.nextInt();
            C = input.nextInt();
            K = input.nextInt();
            }
            System.out.println(count);
        }
    }
}

Good Luck!


回答2:

Your formula for checking the validity of the new square is incorrect; it shouldn't involve squaring. As you discovered, for K = 3, your condition becomes

(r' - r)² + (c' - c)² ≤ 2 × 3² = 18

, which can, in fact, be satisfied by making r' = r + 4 and c' = c, since 16 ≤ 18. But that implies the king moved four squares up!

Rather, you could restate your condition in each direction:

  • The king can move k steps up, but no more than the 8th row, so the topmost row the king can reach is rmax = min(r + k, 8);
  • Similarly, rmin = max(r - k, 1);
  • Similarly, cmax = min(c + k, 8);
  • Similarly, cmin = max(c - k, 1).

You can then simply compute the answer as (rmax - rmin + 1) × (cmax - cmin + 1). This makes sense intuitively because the valid area should be a rectangle spanning rows rmin to rmax and columns cmin to cmax.

  • 发表于 2019-02-22 14:32
  • 阅读 ( 266 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除