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.

#definefor_each(q,s)for(typeof(s.begin())q=s.begin();q!=s.end();q++)structMagicBoard{

boolvisited[100];

set<int>edges[100];

//Depth-FirstSearch:Usedtoverifythatallnodeswithnon-zerodegree

//belongtothesameconnectedcomponent.

voiddfs(intx)

{

if(!visited[x]){

visited[x]=true;

for_each(y,edges[x]){

dfs(*y);

}

}

}

stringableToUnlock(vector<string>board)

{

intw=board.size();

inth=board[0].size();

//Let'sbuildagraph:

memset(visited,0,sizeof(visited));

intn=w+h;

for(inti=0;i<w;i++){

for(intj=0;j<h;j++){

if(board[i][j]=='#'){

edges[i].insert(w+j);

edges[w+j].insert(i);

}

}

}

//Useadfstoveirfythatallnodeswithnon-zerodegreeareconnected

//meanwhile,countthenumberofoddnodesthatarecolumnsandthe

//numberofoddnodesthatarerows.

intcompos=0;

fill(visited,visited+n,false);

intodd_column=0;

intodd_row=0;

for(inti=0;i<n;i++){

if((edges[i].size()>0)&&!visited[i]){

if(++compos>1){

return"NO";

}

dfs(i);

}

if(edges[i].size()%2==1){

if(i<w){

odd_row++;

}else{

odd_column++;

}

}

}

if(odd_row+odd_column>2){

//Toomanyodd-degreenodes.

return"NO";

}elseif(odd_column+odd_row<=1){

//Canalwayspickacolumnasastart.

return"YES";

}elseif(odd_column>=1){

//Thereisoneoddcolumnwhichcanbethestart.

return"YES";

}else{

//Sorry,noluck

return"NO";

}

}};

## 2 comments :

>If there is only one odd-degree vertex

Note that this can never happen due to the hand-shake lemma.

http://en.wikipedia.org/wiki/Handshaking_lemma

Heh, I didn't know about that even though I remember taking the same conclusion one day. Thank you mysterious guest.

Post a Comment