×

# knights attack

 0 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

 0 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. answered 18 Oct '13, 21:29 3★kcahdog 10.0k●28●54●129 accept rate: 14% also 1 thing that can be done is to check the knights below in the grid...and not all 8 positions...:) (18 Oct '13, 21:34) kunal3614★ 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.. (18 Oct '13, 21:58)

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.

3★kcahdog
10.0k2854129
accept rate: 14%

# 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; }

}

(19 Oct '13, 09:47)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×150
×20
×2

question asked: 18 Oct '13, 21:10

question was seen: 1,216 times

last updated: 19 Oct '13, 09:47