This is not necessarily a graph problem, but it can be solved using graphs. I was doing some practice on past google kickstart and found this problem.
Now, in the analysis section of this problem, one of the solutions is creating the graph where each node is a diagonal and each cell represents edge which connects them. I am not able to visualize this analogy and thus can’t able to proceed. It seems to be a really nice trick. Can you help me to understand this solution?
It would be nice if you can mention such problems which can be solved using such analogy.
==============================SOLUTION APPROACH ======================
I have been going over many articles for this problem. There are two approaches that can solve this given problem.
In order to arrive at this, it required huge pattern recognition and mathematics visually intuition. The Solution lies in that fact that “You are guaranteed that it is possible to make all the squares black.”
It basically meaning, there is a mathematical relationship that exist over diagonals that while flipping never ended up revolving each other. (thus infinite loop if we simply do greedy - like when we got “W” simply rotate one of the diagonal and check for other “W”)
If we closely work over many examples then we wil find that it (this relationship among diagonals) depends over each other through each grid cell. (of course, one grid cell can be part of two diagonals, ). Example:
Now, If its “W” that meaning we have to flip one diagonal only (out of the two) to make it black. While, If its “B” already meaning, we wether never flip both diagonal or flip both diagonal(to retain its colour). In order to make number of flips minimum, we have to start from the diagnal which must have the highest impact.
That will be the middle diagonal and sub middle diagonal. We will then have 4 choices wether we flip one or both or nothing and look for White cells, in both these diagonal. If there is white, then we flip another diagonal which is perpendicular to current (these two longest diagonal). Again, there will be solution exist by doing this because as given in problem, You are guaranteed that it is possible to make all the squares black. In other words, intuitively things will get unfold once, you automate this technique as it will never ended up revolving and its will result in least as we are starting from diagnal which is having the highest impact(meaning containing highest number of grid cells hence the intersection point).
NOTE: I never think, that I can able to come up with this approach while competing in real time. That’s, why i need to learn this 2nd technique to encounter such type of question in future.
In this, you have to find a pattern. If you closely look at the examples given you will see, (as discussed in 1st approach above ) that for each cell two diagonal will be connected using this cell. And the fate of this cell(Colour of this cell) depends upon the flipping status of these two diagonals.
So the pattern here is:
If Current cell is “black”- we either flip both or none
If Current cell is “white”- we flip one of the two
Look at this in another way. If Cell is “black”, we want to behave these two diagonals in same way while if cell is “white” we want to behave these two diagonals in different way.
Any pattern yet?
Actually this is something similar to what we do in bipartite graphs. In bipartite graph, if current node is coloured lets say, “Colour 1” then all of its neighbour should have to be in different colour. Only the difference here, is that here the diagonal which is acting here as node while these grid cell is acting as edges. If this grid cell is “white” (meaning edge) then it means we want to have two of its endpoing to have different colour while if Cell is “black” (meaning that edge) we wants two endpoint to behave in similar fashion.
So, We create a graph in which nodes are the diagonals, and edges are the grid cell. A diagonal is connected with others by these cells. So total edges in this graph will be n*n. And we will do dfs staring from any random node(or diagonal). While dfs, we will look at the edge colour. if its “black” then we will colour node (other endpoint in consideration) in same colour, lets say “Colour 1” while if edge or cell is white then we will colour other endpoint diagonal or node with different colour lets say “colour 2”.
We will also make a count of total number of colour 1 and colour 2 that we did. And which ever is minium we will add that to the result.
One implementation problem, that I encouter is how to translate this diagonal in node. That can look into the property as below.
00 01 02
10 11 12
20 21 22
Take above diagonal, some of the indices are always 2. So if its below left to top right diagonal, then its indices sum is equal.
00 01 02
10 11 12
20 21 22
While above diagonals, can be distinguies by the difference in the indices, as it will be same.
For Images, and Code.