Possible moves of knight
Last Updated :
20 Mar, 2023
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:
- Two moves horizontal and one move vertical
- Two moves vertical and one move horizontal
The idea is to store all possible moves of knight and then count the number of valid moves. A move will be invalid if:
- A block is already occupied by another piece.
- Move is out of the chessboard.
Implementation:
C++
#include <bits/stdc++.h>
#define n 4
#define m 4
using namespace std;
int findPossibleMoves( int mat[n][m], int p, int q)
{
int X[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int count = 0;
for ( int i = 0; i < 8; i++) {
int x = p + X[i];
int y = q + Y[i];
if (x >= 0 && y >= 0 && x < n && y < m
&& mat[x][y] == 0)
count++;
}
return count;
}
int main()
{
int mat[n][m] = { { 1, 0, 1, 0 },
{ 0, 1, 1, 1 },
{ 1, 1, 0, 1 },
{ 0, 1, 1, 1 } };
int p = 2, q = 2;
cout << findPossibleMoves(mat, p, q);
return 0;
}
|
Java
public class Main {
public static final int n = 4 ;
public static final int m = 4 ;
static int findPossibleMoves( int mat[][], int p, int q)
{
int X[] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 };
int Y[] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 };
int count = 0 ;
for ( int i = 0 ; i < 8 ; i++) {
int x = p + X[i];
int y = q + Y[i];
if (x >= 0 && y >= 0 && x < n && y < m
&& mat[x][y] == 0 )
count++;
}
return count;
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 0 , 1 , 0 },
{ 0 , 1 , 1 , 1 },
{ 1 , 1 , 0 , 1 },
{ 0 , 1 , 1 , 1 } };
int p = 2 , q = 2 ;
System.out.println(findPossibleMoves(mat, p, q));
}
}
|
Python3
n = 4 ;
m = 4 ;
def findPossibleMoves(mat, p, q):
global n, m;
X = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ];
Y = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ];
count = 0 ;
for i in range ( 8 ):
x = p + X[i];
y = q + Y[i];
if (x > = 0 and y > = 0 and x < n and
y < m and mat[x][y] = = 0 ):
count + = 1 ;
return count;
if __name__ = = '__main__' :
mat = [[ 1 , 0 , 1 , 0 ], [ 0 , 1 , 1 , 1 ],
[ 1 , 1 , 0 , 1 ], [ 0 , 1 , 1 , 1 ]];
p, q = 2 , 2 ;
print (findPossibleMoves(mat, p, q));
|
C#
using System;
class GFG
{
static int n = 4;
static int m = 4;
static int findPossibleMoves( int [,]mat,
int p, int q)
{
int []X = { 2, 1, -1, -2,
-2, -1, 1, 2 };
int []Y = { 1, 2, 2, 1,
-1, -2, -2, -1 };
int count = 0;
for ( int i = 0; i < 8; i++)
{
int x = p + X[i];
int y = q + Y[i];
if (x >= 0 && y >= 0 &&
x < n && y < m &&
mat[x, y] == 0)
count++;
}
return count;
}
static public void Main ()
{
int [,]mat = { { 1, 0, 1, 0 },
{ 0, 1, 1, 1 },
{ 1, 1, 0, 1 },
{ 0, 1, 1, 1 }};
int p = 2, q = 2;
Console.WriteLine(findPossibleMoves(mat,
p, q));
}
}
|
PHP
<?php
$n = 4;
$m = 4;
function findPossibleMoves( $mat ,
$p , $q )
{
global $n ;
global $m ;
$X = array (2, 1, -1, -2,
-2, -1, 1, 2);
$Y = array (1, 2, 2, 1,
-1, -2, -2, -1);
$count = 0;
for ( $i = 0; $i < 8; $i ++)
{
$x = $p + $X [ $i ];
$y = $q + $Y [ $i ];
if ( $x >= 0 && $y >= 0 &&
$x < $n && $y < $m &&
$mat [ $x ][ $y ] == 0)
$count ++;
}
return $count ;
}
$mat = array ( array (1, 0, 1, 0),
array (0, 1, 1, 1),
array (1, 1, 0, 1),
array (0, 1, 1, 1));
$p = 2; $q = 2;
echo findPossibleMoves( $mat ,
$p , $q );
?>
|
Javascript
<script>
let n = 4;
let m = 4;
function findPossibleMoves(mat, p, q)
{
let X = [ 2, 1, -1, -2, -2, -1, 1, 2 ];
let Y = [ 1, 2, 2, 1, -1, -2, -2, -1 ];
let count = 0;
for (let i = 0; i < 8; i++) {
let x = p + X[i];
let y = q + Y[i];
if (x >= 0 && y >= 0 && x < n && y < m && mat[x][y] == 0)
count++;
}
return count;
}
let mat = [ [ 1, 0, 1, 0 ],
[ 0, 1, 1, 1 ],
[ 1, 1, 0, 1 ],
[ 0, 1, 1, 1 ] ];
let p = 2, q = 2;
document.write(findPossibleMoves(mat, p, q));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...