LoopyPath
TCO 2014 Finals · 2014-03-26 · by lg5293
Problem Statement
You have a rectangle that is divided into a grid of unit squares.
The corners and sides of those squares will be called grid points and grid lines, respectively.
Each square contains either a 0 or a 1.
You are given these numbers in a
You want to take a walk along some grid lines. The walk must satisfy the following properties:
- You must both start and end the walk in the top left grid point.
- In each step, you have to walk along a single grid line (i.e., move by 1 up, down, left, or right).
- Your walk must visit each grid point at least once.
- Your walk must satisfy the additional property described in the next paragraph.
Consider any walk that satisfies the first three properties. Imagine that a boy is standing in the middle of a given square and a girl takes the entire walk once (both starting and ending in the top left grid point). While the girl walks, the boy stays in place but rotates so that he always faces the girl's current position. Clearly, when the girl ends the walk, the net result for the boy will be some number of full turns. For each square in the grid, the number written in the square must correspond to the net number of clockwise turns the boy would make if he stood in that square. (In other words, if the square contains a 0, the boy standing in that square must rotate by the same total amount clockwise and counterclockwise, and if the square contains a 1, the boy's total clockwise rotation must be 360 degrees more than the boy's total counterclockwise rotation.)
You are given the
Notes
- The walk may visit each grid point, including the top left grid point, arbitrarily many times.
Constraints
- grid will contain between 1 and 50 elements, inclusive.
- Each element of grid will contain between 1 and 50 characters, inclusive.
- Each element of grid will contain the same number of characters.
- Each character in each element of grid will be either '0' or '1'.
{"111"}
Returns: 8
The optimal walk is to go around the boundary of the grid clockwise. This gives us a path of length 8.
{"10",
"11"}
Returns: 10
We need to visit every grid point. Here is one example of an optimal walk. This walk has length 10.
{"111",
"111",
"111"}
Returns: 20
{"1110000",
"0000111"}
Returns: 34
{
"1110001110001111111000111",
"1110001110001111111000111",
"1110001110000011100000111",
"1111111110000011100000111",
"1111111110000011100000111",
"1110001110000011100000000",
"1110001110001111111000111",
"1110001110001111111000111"
}
Returns: 364
Submissions are judged against all 103 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class LoopyPath with a public method int findMinimumCycle(vector<string> grid) · 103 test cases · 2 s / 256 MB per case