[Community Puzzle] Flood fill Example

I have the same question as @Null-None. Can someone explain why there’s + at the X spot in test case 2? the 5 tower can reach at time = 6, the 2 tower can reach at time = 5. The spot is marked X below

#....##..#.#
#.#..#..5..#
...#........
......##....
.....X#..#..
1...........
............
.#.2#.......
...#..3.#.#.
....#.....#.
....#.....#.
...4...#....

Tower 3 also reaches that spot at time = 5.

(P.S. Not sure if Null-None is asking about that spot? X is (5, 4), not (6, 4).)

1 Like

For anyone new who might still be struggling with the ‘duplicate tower names’ question, here’s a different way to think about it (which might be a good addition to the puzzle instructions):

Each tower has a unique name such as “Alderaan”, “Tatooine”, etc.
However, the map will only show the first character of each tower’s name (‘A’, ‘T’, etc.).
Although the tower names are unique, the first character might not be.
Since each tower is marked at a single location on the map, repeat instances of the same character (‘A’ and ‘A’) can be known to represent different towers (“Alderaan” and “Antares”).

3 Likes

In my solution, there is a std::map<char, char> IDs (“dictionnary” in other languages ?) to give a unique incremental id to the towers, and remember which char is associated to this id.

char uniq = -1;
IDs['A'] = 'A'; // First 'A' encountred
IDs[uniq--] = 'A'; // Second 'A' encountred

When I parse the grid, chars are replaced by their unique ID.
Flood fill is computed with the unique ID.
And at the end, I use my IDs map (or dictionnary) to display the good letters.

1 Like

Ok, not sure what went wrong.
My code passes all tests, but when submitted it says I failed the second validator.
I know this validators are a bit different to the tests we see in order to prevent hardcoding the solution but I did not hardcode the solution.
My code is written in Golang and I tried doing the comparisons in bytes and strings (of length 1). Both ways all tests are passed.
I also consider propagating the ‘+’ and towers with the same name (otherwise I would not pass the tests).

I would like to know what makes my code valid on all tests but invalid on one validator once submitted.

Thanks

Tower ID can be any character different than ‘.’ and ‘#’. Probably you have some assumption about what the ID can be. I also failed with the 2nd validator when assumed that the ID can be only letter.

Technically my code should work with anything different than ‘.’ and ‘#’, but will take another look just in case.
Thanks

Are you guaranteed to have less than 256 towers?

Hello, i’ve got the same problem as @MISELLO. All my tests and validator tests passed, but not the first validator. I missed something ?

I don’t see anything special about the first validator. You may want to re-check your code. If you still can’t find any issues, please post here again.

I hoped less than 128 ^^ but if it is the only objection, you can use a map<int, char>.

I also have the issue with the second validator when all tests in the IDE work. Have you found any solution to this issue ?

Hi!
I have just submited another solution and this time it passed all tests and validators.
I still have no idea what was wrong in my previous code.
Things I did differently:

  • Store the map as a matrix of bytes instead of matrix of a defined structure.
  • Have a separated matrix of data containing the time that cell was filled and from what tower.
  • Have a queue of nodes to process, instead of trying to propagate every cell.

As I said, I don’t know what caused the my first solution to fail the second validation, but I guess that having a queue was the key in my case.

Note: It is a FIFO queue. I start adding the position of the towers in the queue and every time I want to expand I add the new position to the queue. Every node of the queue contains the coordinades x & y. The time (the towers are t=0, their neighbors are t=1 and so on) and the tower index. Then I have an array of towers where I match the tower index to their symbol (symbol is what the problem calls ID).

Hope this helps you to find the issue.

Hey,

I am curious about this. In my case, I noticed that I updated the cell values too early and it caused errors. So I split the update into:

  • find cells that change their value and store the expected changes in a dictionary
  • update the cells values

I think this is the equivalent of your implementation of tracking time. I think FIFO or LIFO during the update should not affect the solution. I am using a dictionary in python and there is no order for elements, and it works.

Thanks for your answer.

I have a list of nodes to process and propagate each node in that list. Processed nodes are then removed from it and the nodes where the propagation was successful are then added to it for the next iteration of my loop.
The iteration number of my loop acts as the distance from a source to another node.

Thus I don’t think the run time is the issue in my case, and I find it hard to find a test case where my algorithm doesn’t work. Modifying my “map” to not be a data structure should not change the behavior of the algorithm.

This sentence made me thing of something.
If you have a unique queue or list where you remove processed noded and add new nodes that need to be propagated probably the condition of the loop is while the queue/list is not empty keep going.
Said that, each iteration would corresponds to a node and not the distance from a source. I think that knowing the time or distance is important because if two sources reach the same spot at the same time (or have the same distance from source) we should print a ‘+’ there.
I solved this having a field called “t” in my node. This “t” is 0 at the tower and every time it propagates I increase it.

Not sure if this helps because you already said your code passes all tests, so you probably have already solved that even by other means.

If your code keeps failing at the validator I can only suggest to keep the idea (because it is good if it passes the tests) and try another approach as I did.

Good luck!

because this b is only one tower.
if one b start in (0,0) and another b start in (2,0), when they move (1,0) will be “+”, beacuse these b are different tower

Why a std::map? Your IDs are continuous, you can use a std::vector.

1 Like

You can do as you want, I just explained how I did to avoid problems with reused letters.

Map was easy because at the end, instead of print the grid, I print IDs[grid[y][x]] for each char, I previously filled my IDs with IDs[‘.’]=‘.’; IDs[‘#’]=‘#’; IDs[‘+’]=‘+’;

Hello everyone, here is my run on test 2

cycle 1 MAP
#....##..#.#
#.#..#..5..#
...#........
......##....
......#..#..
1...........
............
.#.2#.......
...#..3.#.#.
....#.....#.
....#.....#.
...4...#....

cycle 2 MAP
#....##.5#.#
#.#..#.555.#
...#....5...
......##....
1.....#..#..
11..........
1..2........
.#22#.3.....
...#.333#.#.
....#.3...#.
...4#.....#.
..444..#....

cycle 3 MAP
#....##55#.#
#.#..#55555#
...#...555..
1.....##5...
11....#..#..
1112........
11222.3.....
1#22#333....
..2#3333#.#.
...4#333..#.
..44#.3...#.
.44444.#....

cycle 4 MAP
#....##55#5#
#.#..#55555#
1..#..55555.
11....##55..
1112..#.5#..
11122.3.....
11222+33....
1#22#3333...
122#3333#.#.
..+4#3333.#.
.444#+33..#.
444444+#....

cycle 5 MAP
#....##55#5#
#.#..#55555#
11.#.5555555
1112..##555.
11122.#55#..
11122+335...
11222+333...
1#22#33333..
122#3333#.#.
1++4#33333#.
4444#+333.#.
444444+#....

cycle 6 MAP
#....##55#5#
#1#..#55555#
111#55555555
111225##5555
111222#55#5.
11122+3355..
11222+3333..
1#22#333333.
122#3333#3#.
1++4#33333#.
4444#+3333#.
444444+#3...

cycle 7 MAP
#1...##55#5#
#1#.5#55555#
111#55555555
111225##5555
111222#55#55
11122+33555.
11222+33333.
1#22#3333333
122#3333#3#.
1++4#33333#.
4444#+3333#.
444444+#33..

cycle 8 MAP
#11.5##55#5#
#1#55#55555#
111#55555555
111225##5555
111222#55#55
11122+335555
11222+333333
1#22#3333333
122#3333#3#3
1++4#33333#.
4444#+3333#.
444444+#333.

cycle 9 MAP
#11+5##55#5#
#1#55#55555#
111#55555555
111225##5555
111222#55#55
11122+335555
11222+333333
1#22#3333333
122#3333#3#3
1++4#33333#3
4444#+3333#.
444444+#3333

cycle 10 MAP
#11+5##55#5#
#1#55#55555#
111#55555555
111225##5555
111222#55#55
11122+335555
11222+333333
1#22#3333333
122#3333#3#3
1++4#33333#3
4444#+3333#3
444444+#3333

Its says line 5 should be 11122+#55#55, instead of 111222#55#55. I read the subject multiple times, cant find what I didnt understand. Only test 1 and 5 passed