eolymp
bolt
Try our new interface for solving problems
Problems

Nuts for nuts

Nuts for nuts

So as Ryan and Larry decided that their nuts don't really taste so good, they realized that there are some nuts located in certain places of the island, and they love them! Since they're lazy, but greedy, they want to know the shortest tour that they can use to gather every single nut! Can you help them? \InputFile The first line of each test case contains the dimensions of rectangular island \textbf{x} and \textbf{y} (\textbf{x}, \textbf{y} ≤ \textbf{20}). It is followed by \textbf{x} lines of \textbf{y} characters each as a map of the area, consisting of symbols "\textbf{.}", "\textbf{#}" and "\textbf{L}". Larry and Ryan are currently located in "\textbf{L}". The nuts are represented by "\textbf{#}". The children can travel in all \textbf{8} adjacent directions in one step. There will be at most \textbf{15} places where there are nuts, and "\textbf{L}" will appear only once on the map. \OutputFile Print for each test case on a separate line the minimum number of steps to gather all the nuts starting from "\textbf{L}", and returning to "\textbf{L}".
Time limit 1 second
Memory limit 128 MiB
Input example #1
5 5
L....
#....
#....
.....
#....
8 10
L.........
..........
.......#..
..........
#....#....
.........#
..........
....#.....
Output example #1
8
23