ParkPlace
SRM 788 · 2020-07-22 · by square1001
Problem Statement
There is a square-shaped park which is subdivided into N * N unit square cells.
The edges of the park are going in the north-south and east-west directions. We will assign two coordinates (row, column) to each cell in such a way that row numbers grow from the north to the south and column numbers grow from the west to the east.
You are given the
We would like to make our park more attractive to visitors by building walkways. The walkways must be built according to the following rules:
- A walkway cannot enter a tree cell.
- Each grass cell must be crossed by exactly one walkway.
- For each grass cell, the walkway must enter the cell through one edge (not through a corner) and leave the cell through some other edge.
- Walkways cannot cross the boundary of the park.
Note that the above rules imply that in any valid layout of the park the walkways will form a collection of disjoint cycles that, taken together, cover all the grass cells.
Write a program to find if it is possible to design such a collection of walkways. If it is, construct any one such layout and return its description. The way to return the answer is as follows:
-
If it's not possible to place the walkways according to the rules, return an empty
String[] . -
If there's at least one valid solution, choose any one such solution. Then, return a
String[] with length equal to the number of cycles your walkways form. Each element of the return value will describe one of these cycles. More precisely:- Each element should have the form "R C movements".
- "R" and "C" should be the coordinates (row, column) of any one cell on the walkway.
- Finally, "movements" should be a string in which each character is 'N', 'E', 'S', or 'W'. This string represents a sequence of movements that will walk along the cycle, visiting each cell once and then ending in the cell (R, C) where you started.
Note that the return value must not contain any extra characters. This includes extra spaces in "movements" and extra leading zeros in "R" and "C".
Constraints
- N will be between 1 and 30, inclusive.
- The length of array place will be exactly N.
- All lengths of strings place[0], place[1],..., place[N-1] will have length exactly N.
- place[i][j] will be either '#' or '.'.
- At least one cell will be grass. In other words, at least one place[i][j] will be '.'.
4
{ "#..#" ,
"...." ,
"...." ,
"#..#" }
Returns: {"0 1 ESESWSWNWNEN" }
In this example, the only answer is to make one big cycle through all cells of grass. The the form of cycle will be (0, 1) - (0, 2) - (1, 2) - (1, 3) - (2, 3) - (2, 2) - (3, 2) - (3, 1) - (2, 1) - (2, 0) - (1, 0) - (1, 1) - (0, 1). So, there are 12 * 2 = 24 possible return values testcase: you can pick any cell as your starting cell, and then one of two possible directions in which to walk around the cycle. You may return any of these descriptions.
4
{ "...." ,
"..#." ,
".#.." ,
"...." }
Returns: { }
In this example, it is impossible to build the walkways so that each connected part forms a cycle. Thus, you need to return an empty array of strings.
5
{ "....." ,
"....." ,
"..#.." ,
"....." ,
"....." }
Returns: {"0 0 EEEESSWNWWSWNN", "3 0 ESWN", "3 2 EESWWN" }
In this example, there are many ways to build walkways that satisfy the conditions. You can make one big cycle, but other formations are also possible. Few examples of way of building walkways are as follows. In each example, the cells marked with the same letter belong to the same cycle. AAAAA AAAAA AAABB AABBB AAAAA AAAAA AAAAA ABBBA AAABB AABBB AAAAA AAAAA AA#AA AB#BA DD#BB AA#CC BB#CC AA#AA AAAAA ABBBA DDCCC DDDCC BBBCC BBCCC AAAAA AAAAA DDCCC DDDCC BBBCC BBCCC
6
{ "......" ,
"...#.." ,
".#...." ,
"....#." ,
"......" ,
"......" }
Returns: { }
6
{ "......" ,
"....#." ,
"..#..." ,
"...#.." ,
".#...." ,
"......" }
Returns: {"0 0 EEEEESSSWNWNWWSWNN", "3 0 EESSWWNN", "4 3 EESWWN" }
16
{ "................" ,
"................" ,
".####.####.####." ,
".####.####.####." ,
".#..#.#..#.#..#." ,
".#..#.#..#.#..#." ,
".#..#.#..#.#..#." ,
".#..#.####.####." ,
"....#.####.####." ,
"....#.#..#.#..#." ,
"....#.#..#.#..#." ,
"....#.#..#.#..#." ,
"....#.####.####." ,
"....#.####.####." ,
"................" ,
"................" }
Returns: {"0 0 ESENESENEESWSSSSSSSSSSSSSESWWNWSWWWNEENENWWSWNNENWNEEENWWWNNNNNNNN", "0 7 ESWN", "0 9 EESENESENESSSSSSSSSSSSSSSWNWSWNWSWWNENNNNNNNNNNNNNWN", "4 2 ESWN", "4 7 ESSWNN", "4 12 ESSWNN", "6 2 ESWN", "9 7 ESSWNN", "9 12 ESSWNN", "10 2 ESWN", "14 7 ESWN" }
Happy SRM 788!
Submissions are judged against all 58 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ParkPlace with a public method vector<string> construct(int N, vector<string> place) · 58 test cases · 2 s / 256 MB per case