![]() |
VOOZH | about |
Given a chess board of dimension m * n. Find number of possible moves where knight can be moved on a chessboard from given position. If mat[i][j] = 1 then the block is filled by something else, otherwise empty. Assume that board consist of all pieces of same color, i.e., there are no blocks being attacked.
Examples:
Input : mat[][] = {{1, 0, 1, 0},
{0, 1, 1, 1},
{1, 1, 0, 1},
{0, 1, 1, 1}}
pos = (2, 2)
Output : 4
Knight can moved from (2, 2) to (0, 1), (0, 3),
(1, 0) and (3, 0).
We can observe that knight on a chessboard moves either:
The idea is to store all possible moves of knight and then count the number of valid moves. A move will be invalid if:
Implementation:
4
Time Complexity: O(1)
Auxiliary Space: O(1)