TreasureHunt
SRM 173 · 2003-11-25 · by Yarin
Problem Statement
Hurray! You have found a map to a hidden treasure on an island somewhere in the Carribean. It's a classic pirate treasure map, with a big X marking the spot of the treasure. It also has instructions describing how to walk to reach the X, like "north 2 paces", "east 1 pace", etc. However, the instructions lack one vital piece of information: It doesn't say where on the island you should start from! Since you suspect the treasure is buried deep and that the X on the map is only a rough estimation, you might have to dig in a lot of places before finding the treasure. So before you start to dig, you pick up your laptop computer in order to determine where on the island the treasure is most likely to be buried.
You assume that the intended start location is somewhere on the beach, and that if you follow the walking instructions, you should never have to walk across water. If several such starting positions exist, the position that will cause the treasure to be closest (Euclidean distance) to the estimated treasure position is considered most likely (see example 0). If there is a tie, select the northernmost position of these. If there is still a tie, select the westernmost position of these.
The island will be given as a
{"..OOOO..",
".OOOO...",
"OOXOOOOO",
"OOOOOOO.",
".OOOO...",
"..OOO..."}
The island will always be connected. This means that if you stand somewhere on the island, it will be possible to reach every part of the island by only walking on land in any of the four cardinal directions. Also, there will be no lakes, which means that from any water square it will be possible to go in cardinal directions on other water squares until the edge of the map is reached.
The walking instructions will be given as a
Create a class TreasureHunt containing the method findTreasure which takes a
Notes
- All land cells that are on the edge of the grid are considered to be a beach since they're adjacent to squares outside the grid, which are water (see example 2).
- The Euclidean distance between two points x1,y1 and x2,y2 is sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)).
- East is the positive x-direction and North is the negative y-direction.
Constraints
- island will contain between 1 and 50 elements, inclusive.
- Each element in island will contain between 1 and 50 characters, inclusive.
- All elements in island will contain the same number of characters.
- The characters in each element in island will be '.', 'O' or 'X'.
- Exactly one character in island will be a 'X'.
- The island will be connected.
- The island will not contain any lakes.
- instructions will contain between 1 and 50 elements, inclusive.
- Each element in instructions will be in the form "
" where direction is 'N', 'S', 'E' or 'W' and paces is an integer between 1 and 9 (without leading zeros), inclusive.
{"..OOOO..",
".OOOO...",
"OOXOOOOO",
"OOOOOOO.",
".OOOO...",
"..OOO..."}
{"W 3","S 1","E 2"}
Returns: { 3, 2 }
The treasure can't be buried where the 'X' is, because then we would have to walk on water.
{".......",
".OOOOO.",
".OOOOO.",
".OOXOO.",
".OOOOO.",
".OOOOO.",
"......."}
{"N 1"}
Returns: { 3, 4 }
Notice that you must start from the beach.
{"OOOOOOOOOOO.",
"OX..........",
"OOOOOOOOOOO."}
{"W 2"}
Returns: { 1, 0 }
{"....OO.",
"..OOXOO",
"OOOO...",
".OOOOOO",
"...OOOO",
".OOOOO.",
"..OOO.."}
{"N 1","E 1","N 4"}
Returns: { 3, 1 }
{"X"}
{"N 1","E 1","S 1","W 1"}
Returns: { }
Submissions are judged against all 56 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TreasureHunt with a public method vector<int> findTreasure(vector<string> island, vector<string> instructions) · 56 test cases · 2 s / 256 MB per case