Posts Total Moves For Bishop! (InterviewBit)
Post
Cancel

Total Moves For Bishop! (InterviewBit)

PROBLEM DESCRIPTION

Given the position of a Bishop (A, B) on an 8 x 8 chessboard.

Your task is to count the total number of squares that can be visited by the Bishop in one move.

The position of the Bishop is denoted using row and column number of the chessboard.

InterviewBit

SOLUTION

Bishop can only move diagonally.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {

    public int solve(int A, int B) {

        // change to 0 index
        A = A-1;
        B = B-1;

        // top left
        // limited by top rows and left columns
        int tl = Math.min(A, B);

        // top right
        // limited by top rows and right columns
        int tr = Math.min(A, 8-B-1);

        // bottom left
        // limited by bottom rows and left columns
        int bl = Math.min(B, 8-A-1);

        // bottom right
        // limited by bottom rows and right columns
        int br = Math.min(8-B-1, 8-A-1);

        // total squares possible
        return tl + tr + bl + br;

    }

}
This post is licensed under CC BY 4.0 by the author.