Saturday, February 18, 2012

Finally solved it. It is a nice problem that is worth explaining in a post.

You have a grid/board of at most 50x50 cells. Some cells contain a diamond. You need to find if it is possible to build a sequence of cells that visits each diamond cell exactly once. With the added condition that if we number the cells in the path in the order in which they are visited, every even cells must share the row with the cell that comes after it. And every odd cell must share the column with the next cell in the sequence.

I had difficulties to have a remotely good idea to solve this problem during the match, but I eventually I learned of the trick behind the solutions. "It is often the case that, when dealing with a grid, instead of seeing each cell as a node in a graph, it is better to see each cell as an edge in a graph". This change of perspective is actually frequent in cool problems, I think I remember two occasions in which I had to learn this trick before solving a hard problem for an editorial.

If cells are edges in a graph, what do they connect? The usual idea is to make the cell at position (i,j) connect the i-th row with the j-th column. So, in fact, our graph will contain all the rows and all the columns. There is a bidirectional edge between a row and a column if and only if there is a diamond located at the cell that has that row and that column.

That idea is great for this problem, because you will notice that if you move across the new graph, you will follow the even/odd rule automatically. You can confirm this by just noticing that there are never connections between rows and rows or between columns and columns. So, two consecutive edges will always share either a column or a row but not both.

What we now need is to find if there's a path that visits each edge in a given bidirectional graph at least once. This is a known problem that Euler has solved centuries ago: The Eulerian path.

The holy wikipedia claims that a graph will have an Eulerian path, if all nodes with non-zero degree are connected and there are at most 2 nodes with odd degree.

Catch: This sounds too good to be true. Merely make a graph containing the rows and columns, and connect them using the diamond cells, then check for the parity of the degrees. This will pass example test cases but won't solve the problem. There is a condition that we need to guard for: The first pair of edges we pick must share a row (not a column).

In order to fix this, merely question yourself: Why is the rule that at most two vertices have odd degree? Of course, the reason is that the vertices with odd degree will be the ones that will be necessarily used as extremes in the Eulerian path (start or end) (If all nodes have even degree, then there is not only an Eulerian path, there is an Eulerian cycle, which means that any node can be the start or end of an Eulerian path). Please note that to follow the condition that was just mentioned, then the first node in the path must always be a column - This means that at least one of the (at most 2) odd vertices must be a column.

Some details: If there is only one odd-degree vertex, then there is no problem, there is an Eulerian cycle between all other nodes. And then we can add the odd-degree vertex either to the end or the beginning of the path. This way we can still make sure a path exists that begins with a column and is Eulerian.

When there are no odd-degree nodes, we can begin an Eulerian path in any node, so let's just pick a column.

When there are two odd-degree nodes. Then one of them has to be the beginning of the path. Make sure that one of them is a column, then we can begin in it. If both are rows, it is not possible.