OldBridges
SRM 556 · 2012-06-05 · by wata
Problem Statement
You are given a
Alice wants to make an round trips between the islands a1 and a2. That is, she wants to make an consecutive trips, where each trip starts at a1, gets somehow to a2, and after some more steps ends back at a1. At the same time, Bob wants to make bn round trips between the islands b1 and b2. Each round trip may use any sequence of consecutive bridges. Different round trips may, but don't have to, use the same sequence of bridges. Of course, each old bridge may only be used at most twice. (I.e., it may be used twice by Alice, or twice by Bob, or once by each of them, or just once, or not at all.)
You are given the
Constraints
- bridges will contain between 4 and 50 elements, inclusive.
- Each element of bridges will contain exactly N characters, where N is the number of elements of bridges.
- Each character in each element of bridges will be 'O', 'N', or 'X'.
- For each i, the i-th character of the i-th element of bridges will be 'X'.
- For each i and j, the i-th character of the j-th element of bridges will be equal to the j-th character of the i-th element of bridges.
- The country will be connected, i.e., there will exist a path consisting of one or more bridges between any pair of islands.
- a1, a2, b1, and b2 will be between 0 and N-1, inclusive.
- a1, a2, b1, and b2 will be pairwise distinct.
- an and bn will be between 1 and 50, inclusive.
{"XOXX","OXOX","XOXO","XXOX"}
0
1
1
2
3
1
Returns: "Yes"
Alice can travel from the island 0 to the island 1 and go back to the island 0, and Bob can travel from the island 2 to the island 3 and go back to the island 2.
{"XOXX","OXOX","XOXO","XXOX"}
0
2
1
1
3
1
Returns: "No"
In order to make a round trip between the island 0 and the island 2, Alice must use the old bridge between the island 1 and the island 2 twice. So Bob cannot travel from the island 1 to the island 3.
{"XOXO","OXOX","XOXO","OXOX"}
0
2
1
1
3
1
Returns: "Yes"
One possible pair of tours is 0->1->2->3->0 for Alice and 1->2->3->0->1 for Bob.
{"XNXO","NXOX","XOXO","OXOX"}
0
2
1
1
3
2
Returns: "No"
{"XOXOO","OXOXO","XOXOO","OXOXO","OOOOX"}
0
2
2
1
3
2
Returns: "Yes"
Submissions are judged against all 122 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class OldBridges with a public method string isPossible(vector<string> bridges, int a1, int a2, int an, int b1, int b2, int bn) · 122 test cases · 2 s / 256 MB per case