Given a grid with 2x2 pillars spaced apart, construct the unique Hamiltonian circuit through all free cells defined by a fixed local rule.
Hard8ImplementationSimulationGraphGeometryNo attempts yetTime limit2sMemory limit512 MBByteasar is the administrative manager of a large warehouse. Expecting a severe winter, he has decided to install underfloor heating in the warehouse.
The warehouse plan is a rectangle of even dimensions n × m divided into unit squares. Most of the unit squares are warehouse space, but some are occupied by massive pillars that give the structure additional support. Each pillar occupies a 2 × 2 square of four unit squares on the plan. The pillars are not arranged densely: the centres of any two of them are at least 6 units apart in the Euclidean metric. Also, the centre of each pillar is at least 3 units away from each of the outer walls of the warehouse.
Heating is done by one heating pipe installed under the floor. The pipe has to pass through the centres of all unit squares except those occupied by pillars. Each section of the pipe must run parallel to one of the walls, and the pipe may turn only at the centres of unit squares. The pipe must begin and end at the same place. At that point the cold water is discharged outside and hot water is fed into the pipe.
Byteasar has asked you to plan the route of the pipe in the warehouse. To help you, he has introduced a rectangular coordinate system on the plan: the x coordinates lie in the interval [0, n] and the y coordinates in the interval [0, m]. The centres of all unit squares have coordinates of the form k + 1/2 for k ∈ ℕ.
The first line contains three integers n, m and f (1 ≤ n, m ≤ 1,000, n and m are even): the dimensions of the warehouse and the number of pillars. Each of the next f lines contains two integers xi and yi (0 ≤ xi ≤ n, 0 ≤ yi ≤ m), the coordinates of the centre of the i-th pillar.
In the first line print the word TAK (yes) if underfloor heating that meets Byteasar's requirements can be installed, and NIE (no) otherwise. If the answer is TAK, the second line has to describe the route of the pipe as a string of nm - 4f letters. The pipe begins at the point with coordinates (1/2, 1/2). A move by the vector [0, 1] is written as G, by [0, -1] as D, by [1, 0] as P, and by [-1, 0] as L.
Several routes may be valid, so only the one route fixed by the rule below is accepted. Call the unit square with centre (x + 1/2, y + 1/2) cell (x, y), where 0 ≤ x < n and 0 ≤ y < m. A pillar with centre (a, b) occupies the cells (a-1, b-1), (a, b-1), (a-1, b) and (a, b). A cell not occupied by a pillar is free.
The selected edges form one closed route that visits every free cell exactly once. Print that route starting from cell (0, 0) in the direction whose first move is P.
The sample output corresponds to the following figure.
