knights attack

Appu is trying to learn chess . He took a chessboard , placed all the knights he had in his hand on the chessboard . Please help Appu find out whether any 2 knights attack each other or not.

Input:

n - size of the chess board n x n array with 0 for empty positions and 1 for knight position.
Output

YES if there is an attack NO else
Constraints

0<n<100

Sample Input
4

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Sample Output
NO

I would suggest 2 things:-

  1. make a function inrange(x,y,n) which checks if the points x,y are valid points on the grid or not.

2)make a function check(x,y) which checks if the point are occupied or not. if occupied print ‘Yes’.

call the function “check” for all the 8 poistions a knight can attack from a position (x,y) eg (x+1,y+2),(x+2,y+1),etc if it is a valid position.
eg:- if you are at x,y then do:- if( inrange(x+2,y+1,n)): check(x+2,y+1).

If you are expecting full code as answer then im very sorry to disappoint you . You should try building your own code on this logic.If you want help in debugging, then you are more than welcome.

I’m starting to think you want us to do your homework for you which i hope is not the case.inrange will condition like :

x>=0 and x<n and …

and next time you want help on this question, please show us the effort you have put in (code, function, algo, etc) and then expect an answer in return.

what do u want…??

also 1 thing that can be done is to check the knights below in the grid…and not all 8 positions…:slight_smile:

how do we make a function inrange(x,y,n) which checks if the points x,y are valid points on the grid or not.plz reply as soon as possible…thank you…

#include<stdio.h>
int main()
{
int I,n;
scanf("%d",&n);//no.of rows and columns in a square chess board
if(8 conditions)
printf(“true”)
else
printf(“no”);
return 0;
}

}