SimplePath
SRM 202 · 2004-07-07 · by dgoodman
Problem Statement
We are given a
Constraints
- direction will contain between 1 and 50 characters inclusive.
- length will contain the same number of elements as direction has characters.
- Each character of direction will be an uppercase letter 'N', 'E', 'S', or 'W'.
- Each element of length will be between 1 and 10,000 inclusive.
"NWSEEN"
{5,5,3,2,5,2}
Returns: 0
The first segment goes north 5 units. The second then goes 5 west. The third segment goes 3 south. The fourth goes 2 east. The fifth segment continues east and touches (in fact intersects) the first segment. Return the zero-based index of the first segment.
"NWWS"
{10,3,7,10}
Returns: -1
This road is a 10 x 10 x 10 U-shaped simple path.
"NWES"
{2,2,1,2}
Returns: 1
Nothing touches the first segment, but the second segment touches the third segment. In fact, the third segment doubles back and covers half of the second segment. The zero-based index of the second segment is 1.
"NWSE"
{100,100,100,100}
Returns: 0
This road forms a square, so the last segment touches the first segment and we return the index of the first segment.
"NNNNNNNNNNNNNNNNNNNN"
{1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1}
Returns: -1
Submissions are judged against all 32 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SimplePath with a public method int trouble(string direction, vector<int> length) · 32 test cases · 2 s / 256 MB per case