https://www.codingame.com/training/easy/longest-road
Send your feedback or ask for help here!
Created by @Jumpmaster,validated by @David_Augusto_Villa,@Bavoen and @About.
If you have any issues, feel free to ping them.
https://www.codingame.com/training/easy/longest-road
Send your feedback or ask for help here!
Created by @Jumpmaster,validated by @David_Augusto_Villa,@Bavoen and @About.
If you have any issues, feel free to ping them.
Unless Iâm mistaken, Loops and Branches and its validator are incorrect. I count 17 aâs and a couple more bâs than claimed, all connected.
I havenât solved this yet, but my understanding is that you need to calculate the longest possible path, so for the last test the âA 13â seems to be correct with this path:
.....
A#a#.
..aa#
#.###
a....
(Note: capital A is part of the path but not counted for the length.)
PS: maybe there is a simple approach that I have overlooked, but this puzzle seems to me harder than a typical âeasyâ category, even if the small test cases allow suboptimal algorithms.
PS2: But you were one of the approvers, werenât you?
There are no problems with testcases/validators.
A chain should have no branch and end if it leads to an already encountered path.
Iâm ok with the easy ranking but I wouldnât mind a medium ranking either, it lies somewhere in between.
Because itâs a piece of cake if youâre familiar with cycle detection but if youâre not, figuring it out by yourself is not that easy.
Check to see that youâre not allowing diagonal moves along a path. (I had the same count on my first run, fwiw. My fingers just typed in the ânearest neighbor testâ from some past problem on their own, I guess. :^)
I approved before the cycle case was added. The language has been clarified to resolve this misunderstanding. Iâll rework the problem at some point, but I do wonder about the difficulty now.
Itâs way harder with the cycle case.
Allowing cycles steps into a difficult territory that the test cases do not cover.
A fully legitimate example:
9
a#aaaa#aa
a#aaaa#aa
a#aaaa#aa
a##aa###a
aaaaa#aAa
a###aaa#a
aaaa#aa#a
aaaa#aa#a
aaaa#aa#a
max length is 50
How aboutâŚ
10
Aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
max length is 99
Free to try it.
I pass your first custom test case, but timeout at your second one.
Because of the cycles, I start DFS from every cell and that is way too much for this kind of map.
Maybe, if map size would have been limited to 8*8, then the current path could have been encoded into a single int64, leading to some speedup. But the branch factor for a full âaâ map is still extremely high.
Most likely, there are much better applicable algorithms than mine, but the given âeasyâ category led me to brute-force⌠I code with a hammer, not my fingers.
I can pass the first one in Python locally in ~6 sec, the second one is ofc out of reach.
But I donât think those validators you propose really fit the description, itâs supposed to be roads so you can expect '#'s to properly separate them.
So even one 2 x 2 submatrix of 'aâs (letâs call that a clearing) would feel weird here. You would never see that in a classical maze / maze with cycles.
And you can see that not a single testcase has such clearings.
So not a real problem for me as it is, but yes it would have been if there was at least one testcase with a 2 x 2 clearing (cause you could legitimately wonder for bigger clearings in validators).
Trying to solve it by dfs but have to adjust the standard algorithm to go through every possible combination of directions priorities. The code becomes a monster quickly.
You can just run a regular DFS from each alphabetic char.
If you call the DFS recursively like this:
the search wonât explose (unless you try it on the kind of validator java_coffe_cup mentionned) and youâll just have to update your max length at each deadend.
I first had the same doubt as you. I double checked the puzzle description.
If a player has at least 5 consecutive (NON-REPEATING) roads then they can be awarded the âlongest roadâ victory points.
The roads cannot be repeated. They must be travelled from the start point to the end without backtracking.
So there is nothing to ban a road like this to be validâŚ
It cannot happen just because the author code cannot solve it.
Instead of pushing solution target to the extreme, why the statement and game rule not adjust itself to limit some âtoo difficultâ possibilities?
OK, this looked like a monster a week ago, but I solved it with simple recursion and cycle detection, which felt like brute forcing but I guess is actually DFS. Canât be easy with a high level concept like that, but isnât too challenging either. Medium difficulty seems about right to me.
Ugh, the tag for existing puzzles is DFS. I like that someone decided this should be âDepth first searchâ except now I have both skills.
Updated the difficulty to medium since that seemed to be the general consensus. Thanks for all the discussion everyone! This was my first puzzle submission, and hearing all your feedback has been super helpful. Hope you all enjoyed it.
Hi !
I only get a 83% score because of the validator 4 which fail.
But I canât understand why since my solution isnât âhard codedâ.
I used a loop which first detect every players (A, B, âŚ) before applying DFS.
Thanks
The message about hardcoded solution is just a general information.
If your code fails one of the validators it means that it is not handling one of the edge cases correctly.
You can experiment with Custom test cases to try and figure it out (you must enable Expert mode in settings to see it).
Try something like this (not the actual validator, but similar):
5
aA##c
#a##c
b###C
bBb#c
##bcc
Expected output is
C 5
Thanks !
I did it
I had the same issue and couldnât find the issue until I saw the new testcase in here since I was using a > instead of >= in one place.
An extra testcase with the shortest longest road (5) would be great!