You are not logged in. Please login at www.codechef.com to post your questions!

×

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

asked 18 Oct '13, 21:10

suhasinturi's gravatar image

0★suhasinturi
0112
accept rate: 0%

edited 18 Oct '13, 21:18

what do u want..??

(18 Oct '13, 21:16) kunal3614★

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.

link

answered 18 Oct '13, 21:29

kcahdog's gravatar image

3★kcahdog
10.0k2854129
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) suhasinturi0★

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.

link

answered 18 Oct '13, 23:48

kcahdog's gravatar image

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) suhasinturi0★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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