Torus
Time limit1sMemory limit256 MB
Print 1 for each test case followed by the prescribed vertex order that visits every vertex of the toroidal lattice exactly once.
- Level
Easy2 of 10
- Topics
- Implementation
- Solved
- No attempts yet
Problem
A regular polygon has all sides of the same length and all interior angles equal. A regular tiling of the Euclidean plane covers the whole plane with congruent regular polygons that do not overlap and meet vertex to vertex. Only three regular polygons admit a regular tiling: the square, the equilateral triangle and the regular hexagon. A lattice is an infinite graph whose drawing on the plane is a regular tiling, so there are the square lattice, the triangular lattice and the hexagonal lattice.
Such a lattice turns into a graph with finitely many vertices and edges that keeps the symmetry of the infinite one. The finite graph can be drawn on the surface of a torus with no edge crossings, and the regions bounded by its shortest cycles have similar shapes and sizes. A graph obtained this way is a toroidal lattice, defined as follows.
Definition 1. Let and be integers with and . The vertex set of the toroidal square lattice is . Two vertices and are joined by an edge when one of the following holds.
- and
- and
Definition 2. Let and be integers with , and even. The toroidal triangular lattice is the toroidal square lattice together with the following edges, for all and .
- If is even, join and , where and .
- If is odd, join and , where and .
Definition 3. Let and be integers with , and both even. The vertex set of the toroidal hexagonal lattice is . Two vertices and are joined by an edge when one of the following holds.
- and
- and and
To help a project that builds a library of lattice graph algorithms, write a program that finds a cycle visiting every vertex of a given toroidal lattice exactly once. A cycle is a sequence of distinct vertices such that and are adjacent for every , and and are adjacent as well. The toroidal square lattice is simple, so only the toroidal triangular and toroidal hexagonal lattices appear in the input.
Input
The first line contains the number of test cases , a positive integer. Each of the next lines contains three integers , and separated by spaces, with , and . The graph of that test case is the toroidal triangular lattice if , and the toroidal hexagonal lattice if .
The given lattice always satisfies its definition: is even when , and both and are even and at least 4 when .
Output
Print the results for the test cases in the given order. For each test case, the first line contains an integer that tells whether a cycle exists: 1 if it does, -1 if it does not. Only when the first line is 1, it is followed by lines listing the vertices of the cycle in order. Print vertex as (i,j), with no space or tab inside a line.
Several cycles can exist, so only the cycle built by the following rule is accepted. Below, is the remainder in the range from to .
For , print in this order.
- For in this order: print if is even, and if is odd.
For , for in this order, print these vertices.
- for
- for
This rule produces a cycle for every input that satisfies the constraints, so the first line of each test case is always 1.