user2737948
user2737948

Reputation: 339

Understanding this programming challenge logic

I'm working through this programming challenge and I'm having some difficulties understanding how the chess board is composed. From what I manage to understand is:

What I planned to do is starting a chess board where the first square is (0,0) and the last square is (3,3). I need to iterate from that first position, then do the operations to find if that square is less than Ri, if its less than mark that entrance with a counter and continue.

I did it by hand to check if the output of the first case was correct, I got a 5 reachable squares but the output got a 10. I redraw my chess board starting from (1,1) to (4,4) did the math by hand and also got a 5. The first test is (1,1,1) the attacker is in position (1,1) with a range of 1.

If I use the board starting in (0,0) the squares that can be attacked are: 2,5,6,7 and 10. If I use the board starting in (1,1) the squares are: 1,2,5,9 and 10.

Upvotes: 0

Views: 44

Answers (1)

Jeremy Kahan
Jeremy Kahan

Reputation: 3826

First, I would stick with their notation and do things 1-based. Second, people do attack the square they are on. So in the first case, the (1,1) attacker attacks (1,1), (1,2), and (2,1), the (3,1) attacks (3,1), (3,2), and (4,1) [also (2,1), but we already counted that] and (3,3) attacks itself, (2,3), (3,4) and (4,3). [also (3,2), which we already counted] That is 10 squares attacked as claimed. The next case specifies a new board which is 1 by 10 with 1 attacker. That attacker sits on (1,1) and attacks (1,1) and (1,2) so 2 squares. Then your algorithm should work (the way you describe, you would need to nest a second loop of iterations over attackers, but I would rather read an attacker at a time in a loop and then iterate over the squares).

Upvotes: 1

Related Questions