Feel free to send your feedback or ask some help here!

# Shadows of the Knight - Episode 2 - Puzzle discussion

**bilut**#3

Doing binary search for each coordinate individally works most of the time (75%). If you want 100% you will have to work on both ranges at once AFAIK.

Also - first jump Iâ€™m making is just a mirror of a starting postion. That way Iâ€™m cutting possible area in most efficient way.

**Adam798**#4

I first thought that it was inneficient to search each coordinate individually, but I changed my mind. Cuting the zone in half is as good if the cut is horizontal, vertical or diagonal. Most importantly, its way easier to keep track of the remaining zone if the cuts are horizontal and vertical. So you should concentrate on the one dimensional problem (â€śTowerâ€ť test) and then just repeat for the second dimension. The though part is to determine where to jump to minimize the area remaining (in the worst case) after a few jumps , you canâ€™t always cut in half, and you shouldnâ€™t each time you canâ€¦

I really enjoyed this puzzle !

**Chingmann**#5

UVA 10084 is a puzzle on the same idea but from a different perspective. Somehow reading that puzzle helped me to progress in this puzzle

The following review provides additional hints:

**olinox14**#6

Umpff, no matter what I try, I donâ€™t manage to past the last test!

I always am like 5 rounds away of success: what am I missing?

I basically move from centroid to centroid, and I update the zone to explore each time depending on the result of the detectorâ€¦ Is there some trick to improve the algo?

I tried to move to the furthest remaining point instead of to the centroid, to search x y individuallyâ€¦ nothing work.

Thanks by advance!

**player_one**#7

My code uses a hodgepodge of approaches. I start out with mirroring my position around the centroid, with special code to handle OOB condition. If that returns a ratio of WARM:COLD areas that is beyond some threshold, then I move to the farthest point on the polygon from my current point and then switch back to centroid again. If I get below a certain threshold of rooms, then I shove them all in a collection and search for a point that gets me closest to 50% split. That last one is just me being lazy with floating point errors, though. It shouldnâ€™t be necessary.

- danBhentschel

**olinox14**#8

Ok thanks! I managed this by mirroring the centroids by 50% (sorry if this is not clear), good idea!

**Naugthall**#9

How can I check my code?

Everytime I try a test case it shows Iâ€™m sending nothing but doesnâ€™t show any error code, I supouse I have any mistake in my code but as long as I canâ€™t check it step by step Iâ€™m not sure where is it crashing

**bilut**#11

Then maybe the pick order - which side to cut first is important in this case. Idono, wasnâ€™t able to make it with binary search on x and y separately.

**TataMaso**#12

OK, as someone who is way better in maths than in programming I found this one pretty interesting: once youâ€™ve found the algorithm it should not be very hard to code it. In order to help those who might not be comfortable with the maths, the key here is to understand that the area of research (â€śARâ€ť) is a CONVEX POLYGON (polygon where the angles on the inside are below 180Â°). One of its very useful property here is that each line crossing a convex polygon by its centroid splits it in two equal areas. So my solution process the centroid and then calculate the next position so that: dist{current batman pos, centroid} = dist{next batman pos, centroid}. That means that the next batman pos is not necessarily the â€śmirrorâ€ť position; but it is on a circleâ€¦ (be careful thought, if the whole AR is on the bisection of the previous and the next Batman pos, it wonâ€™t be very usefulâ€¦ )

Finally do not forget that the convex polygon means that for each floor, there are only one left bound and one right bound that delimitate the AR on this floor (except if the whole floor is not in the AR) . So use the bounds and dichotomy to process each floor if you want to pass the last test.

With this solution (optimal mathematically I guess (?), as the AR is always split in 2) I managed to have 5 turns left on the final test. The most difficult part for me was the â€śtowerâ€ť, as there cannot be any circle: youâ€™ll have to use the best approximationâ€¦ (the area is not split in 2, but youâ€™ll have to find the optimal split).

Good luck, i hope youâ€™ll have as much fun as me on this one

**lleeppoo**#13

why for test 7 when i gave 498 498 after (x0,y0)=(501,501) the game will respond with COLDER not WARMER (is there a problem in calculating the distance between) ?/

**CyberLemonade**#16

Basically, its an implementation of binary search.

however, to get the centre, you need to mirror the current coordinate on the centre for the next coordinate

it is possible to do one coordinate first, and then the next

**CyberLemonade**#17

Okay I just completed this one (my very first very hard puzzle!!).

Thanks a lot codingame, for this wonderful puzzle

**Aklad**#18

I started this puzzle just after completion medium-level Episode 1. As this task is harder, I expected it in the hard category, havnâ€™t checked it in advance. When I finished Episode 2, I was surprised this puzzle is in very hard category. I do not believe it! IMHO it would be â€śveryâ€ť if there were a test, that cannot be passed by solving each coordinate individually. Working with centroids is much harder (I canâ€™t), than keeping track of min and max for an axis. Cutting the zone in half horizontally etc is OK while jumps are not limited, but with OOB problems there should be more effective way if jumps are not limited to rectangles. I have no example right now, but I do believe it can be found.

**miklla**#19

As more mathematician than programmer I used kinda similar but different approach. I store all possible bomb positions in horizontal intervals for each y. In big loop I Monte-Carlo next move and check areas corresponding to all three possible results. Lets call them n0, n1, n2. I minimaze â€śmax(n0, n1, n2) - min(n0, n1, n2)â€ť to choose the best next move. A bit tricky part was to abort MC. I could break cycle with timecheck but due to laziness I placed counter until C / (down_y - up_y + 1) since complexy of calculation of n0, n1, n2 is O(down_y - up_y + 1), where down_y is y coordinate of the lowest possible bomb position and up_y stands for the highest. And one exception was when you have no possible move with more than one nonzero n0, n1, n2 (in Tower it happens for example). In that case I output a random possible bomb position.

Amazingly, but this worked (Monte-Carlo for this puzzle)!

**Geropy**#21

As mentioned above, mirroring the bisector of the search space will implement a binary search, and will get you about a 50% score. The problems you have will be when you canâ€™t perform this mirror move because it would take you off the grid.

Finding a simple, greedy alternative move in these situations allowed me to solve all but the last puzzle. Finding a more optimized alternative move is what got me 100%. This involved pencil + paper and math scribbling more than programming.

My advice would be to realize that bombs near the edge of the grid are much harder to triangulate than bombs near the centre, and you will need to account for this somehow to improve your worst-case outcome. Good luck!

**OHER**#22

Got the 100% but itâ€™s frustrating to see that a dirty horizontal then vertical search works better than an elegant solution just because of the 150ms mandatory response.