16
2
A Hamiltonian path in a graph is a path that visits each vertex exactly once; a Hamiltonian cycle is a Hamiltonian path that is a cycle – the path forms a simple closed loop. In this challenge the graph will be a n x n
grid, where n is an even number greater than 2.
Here is an example of a Hamiltonian cycle on 12x12 rectangular grid:
+---------------+ +-----------------------+
| | | |
+-----------+ | +---+ +-----------+ |
| | | | | |
+-------+ | +---+ | +-------+ | |
| | | | | | | |
| +---+ | +---+ | +-------+ +---+
| | | | | |
| +-------+ +---+ | +---------------+
| | | |
+---+ +-------+ | | +-----------+ |
| | | | | | | |
+---+ +---+ | +---+ +---+ +---+ |
| | | | | |
+---+ +---+ | +-----------+ +---+ |
| | | | | |
+---+ +---+ | +---+ +-------+ | |
| | | | | | | |
+-------+ | +-------+ +---+ | | |
| | | | | |
+-------+ | +---------------+ +---+ |
| | | |
+-----------+ +---------------------------+
The path visits each vertex exactly once and forms a simple closed loop that do not intersect or touches itself. The grid points are not shown so that the ASCII graphic is not cluttered. There are three -
(---
) between two horizontally connected vertices or 3 spaces if the vertices are not connected. A single |
connects two vertically adjacent vertices, spaces are used otherwise. Since the grid is not visible, the +
is used only where the path takes a turn. The path will never be broken - if a segment connects two vertices, it will be ---
and never - -
.
Task:
You will be given an ASCII representation of a path and you need to check if it is a Hamiltonian cycle on a grid. Write a full program or a function that solves this problem.
Input:
ASCII representation of a path. It can be:
- A mutliline string;
- A list of strings
- A list/array of characters
or any format that is convenient for you.
You can have an optional parameter n
for the size of the grid.
You can use alternatve ASCII representation, just explain it.
Output:
- A consistent value indicating that the path is a Hamiltonian cycle on a grid
- A consistent value indicating that the path is not a Hamiltonian cycle on a grid. There can be several reasons for this: The path doesn’t visit all vertices; the path crosses/touches itself; there are two or more paths and not a single one. You don’t need to specify the reason – just return/print a consistent falsy value.
Test cases:
Truthy:
Optional `n` = 6
+-----------+ +---+
| | | |
| +-------+ | |
| | | |
| +-----------+ |
| |
+---+ +-------+ |
| | | |
+---+ +---+ | |
| | | |
+-----------+ +---+
+-------------------+
| |
| +---------------+
| |
| | +-----------+
| | | |
| | | +---+ |
| | | | | |
| +---+ | | |
| | | |
+-----------+ +---+
+---+ +-----------+
| | | |
| | | +-------+
| | | |
| | | +-------+
| | | |
| +---+ +-------+
| |
| +---+ +-------+
| | | |
+---+ +-----------+
Optional `n` = 8
+---------------------------+
| |
+---+ +-----------+ +---+
| | | |
+---+ +---+ +---+ +---+
| | | |
| +---+ | +---+ +---+
| | | | | |
+---+ | +---+ | +---+
| | | |
+---+ +-------+ +---+ |
| | | |
| +-------------------+ |
| |
+---------------------------+
+-------------------+ +---+
| | | |
+-------+ +---+ | | |
| | | | | |
+---+ | | | +---+ |
| | | | | |
| | +---+ | +---+ |
| | | | | |
| +-----------+ | | |
| | | |
+---+ +-----------+ | |
| | | |
+---+ +-----------+ | |
| | | |
+-------------------+ +---+
+---+ +-------------------+
| | | |
| | +---+ +-----------+
| | | |
| +---+ | +-----------+
| | | |
| +---+ +---+ +---+ |
| | | | | |
| +-----------+ | | |
| | | |
| +-------+ +---+ | |
| | | | | |
| | +---+ +---+ | |
| | | | | |
+---+ +-----------+ +---+
Optional `n` = 12
+---+ +-----------+ +-------------------+
| | | | | |
| | +-------+ | +---+ +-----------+
| | | | | |
| +-------+ | +-------+ +---+ +---+
| | | | | |
| +-------+ +---+ +-------+ | | |
| | | | | | | |
| +---------------+ | +---+ +---+ |
| | | |
| +-------+ +-------+ +-----------+ |
| | | | | |
| | +---+ | +-------------------+ |
| | | | | |
| | +-------+ +-----------------------+
| |
| +-------------------------------+ +---+
| | | |
+-------+ +-------+ +---+ +---+ | |
| | | | | | | |
+-------+ +---+ | | | +-------+ |
| | | | | |
+---------------+ +---+ +---------------+
+---+ +---------------------------+ +---+
| | | | | |
| | | +-------+ +-----------+ | |
| | | | | | | |
| | | +---+ | +-------+ +---+ |
| | | | | | | |
| | +---+ | | +---+ | | +---+
| | | | | | | | | |
| +-------+ | +---+ | +---+ +---+
| | | |
+---+ +---+ | +---+ +-------+ +---+
| | | | | | | |
+---+ | | +---+ +---+ +---+ +---+
| | | | | |
| +---+ +---------------+ +---+ +---+
| | | |
| +-----------+ +---+ +-------+ +---+
| | | | | |
| +-------+ | | | | +-------+ |
| | | | | | | | | |
| | +---+ | | +---+ +---+ | |
| | | | | | | |
+---+ +-------+ +---------------+ +---+
+---------------------------+ +---+ +---+
| | | | | |
| +---------------+ +---+ | +---+ |
| | | | | |
| | +---+ +---+ +-------+ +---+ |
| | | | | | | |
| +---+ | +---------------+ | | |
| | | | | |
+-------+ +---+ +-----------+ | | |
| | | | | |
+---+ | +---+ | +-----------+ | |
| | | | | | | |
| +---+ | +---+ | +-----------+ |
| | | | | |
| +-------+ +---+ | | +-------+ |
| | | | | | | |
| | +-----------+ | | | +---+ |
| | | | | | | |
| | | +-------+ | +---+ +---+ |
| | | | | | | |
| | | | +---+ +-------+ +---+ |
| | | | | | | |
+---+ +---+ +---------------+ +-------+
Falsy:
Optional `n` = 6
; Two paths
+-------------------+
| |
| +-----------+ |
| | | |
| +-----------+ |
| |
+-------+ +-------+
| |
+-------+ +-------+
| |
+-------------------+
; Two paths
+-------+ +-------+
| | | |
| +---+ +-------+
| |
| +---------------+
| |
| +-----------+ |
| | | |
| +---+ +---+ |
| | | |
+-------+ +-------+
; The path doesn't visit each vertex
+-----------+ +---+
| | | |
| | | |
| | | |
+-------+ +---+ |
| |
+---+ +---+ +---+
| | | |
| +-------+ +---+
| |
+-------------------+
; The path doesn't visit each vertex and touches itself
; (so visits some points more than once)
+-------------------+
| |
| +-----------+ |
| | | |
| | +-------+ |
| | | |
| | +-------+ |
| | | |
+---+-----------+ |
| |
+---------------+
; The path doesn't form a loop and touches itself
+---+ +---+ +---+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+---+---+ +---+---+
Optional `n` = 8
; Two paths
+-------------------+ +---+
| | | |
| +-----------+ | | |
| | | | | |
| +-------+ | +---+ |
| | | |
+-------+ | +-----------+
| |
+---+ +---+ +-----------+
| | | |
| | +---+ | +-------+
| | | | | |
| +---+ +---+ +-------+
| |
+---------------------------+
; The path doesn't visit each vertex
+---------------+ +-------+
| | | |
| +-------+ +---+ |
| | | |
| | +---+ +---+ |
| | | | | |
| | +-------+ +-------+
| |
| | +-------------------+
| | | |
| +---+ +---+ +---+ |
| | | | | |
| +---+ | | | | |
| | | | | | | |
+---+ +---+ +---+ +---+
; The path doesn't visit each vertex
+---------------+ +-------+
| | | |
| +-----------+ +---+ |
| | | |
| +---+ +-----------+ |
| | | |
+---+ | +-------+ +---+
| | | |
+---+ +-------+ | |
| | | |
| +-----------+ | |
| | | |
| +---------------+ +---+
| |
+---------------------------+
; Two paths
+-----------+ +-----------+
| | | |
| +---+ | | +-------+
| | | | | |
| | | | | +-------+
| | | | | |
+---+ | | +-------+ |
| | | |
+---+ | +-----------+ |
| | | |
| | | +-----------+ |
| | | | | |
| +---+ +-----------+ |
| |
+---------------------------+
; The path touches itself (so visits some points more than once)
+---+ +-------------------+
| | | |
| | | +---+ +-------+
| | | | | |
| +---+ | | | +---+
| | | | | |
+---+ +---+ | | | |
| | | | | |
+---+ +-------+---+---+ |
| | | |
+-------+ +---+ +---+ |
| | | |
+-------+ +-----------+ |
| |
+---------------------------+
; The path doesn't form a loop
+---------------+ +-------+
| | |
+-------+ + +-------+ |
| | | |
+-------+ +-----------+ |
| |
| +-------------------+ |
| | | |
| +---+ +-----------+ |
| | | |
| +---+ +-----------+ |
| | | |
| +---------------+ | |
| | | |
+-------------------+ +---+
Wining criteria:
The shortest solution in bytes in each language wins. I'll highly appreciate if you add explanation of the code and the algorithm you used.
Do we have to use the ASCII representation given? Or can we use an alternative ASCII representation? – Nick Kennedy – 2019-10-26T12:44:03.807
Are we allowed to take inputs drawn with only a single
-
(rather than---
) as the horizontal connector? – Jonah – 2019-10-26T12:55:27.847@Nick Kennedy Yes, you can. – Galen Ivanov – 2019-10-26T13:44:56.077
@Jonah Yes, you can. – Galen Ivanov – 2019-10-26T13:45:20.593