Trekking
SRM 345 · 2007-04-21 · by rusolis
Problem Statement
You are planning a small trek over at the local mountain range. It will involve walking for several days and spending the nights in a tent. The mountains are not very friendly (they're steep and rocky), and therefore many locations are not suitable for setting up a camp. You will be given a
You have several alternative plans to follow, given in the
Given trail and plans return the minimum number of nights that must be spent in the mountains, according to one of the valid plans. If all plans are invalid, return -1
Constraints
- trail will contain between 2 and 50 characters, inclusive.
- trail will contain only the characters '.' and '^'.
- plans will contain between 2 and 50 elements, inclusive.
- Each element of plans will contain the same number of characters as trail.
- Each element of plans will contain only the characters 'w' and 'C'.
"^^....^^^..."
{"CwwCwwCwwCww",
"wwwCwCwwwCww",
"wwwwCwwwwCww"}
Returns: 2
The first plan is not valid because it involves camping in the first visited location, which is not suitable for camping. The other two plans are valid, but the third involves only two camps, so it's the best one.
"^^^^"
{"wwww",
"wwwC"
}
Returns: 0
"^^.^^^^"
{"wwCwwwC",
"wwwCwww",
"wCwwwCw"}
Returns: -1
"^^^^....^.^.^."
{"wwwwCwwwwCwCwC",
"wwwwCwwCwCwwwC",
"wwwCwwwCwwwCww",
"wwwwwCwwwCwwwC"}
Returns: 3
".............."
{"CwCwCwCwCwCwCw",
"CwwCwwCwwCwwCw"}
Returns: 5
Submissions are judged against all 66 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Trekking with a public method int findCamps(string trail, vector<string> plans) · 66 test cases · 2 s / 256 MB per case