Editorial for COCI '21 Contest 5 #2 Dijamant


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Notice that if somewhere in the table there is a diamond shape whose edge is made out of #, then its interior is also a diamond shape, but its edge is from . instead of #. Therefore, it is enough to iterate over the table and using BFS or DFS, find the maximal connected regions made from . and for each such region, check if it has a diamond shape.

Let's now look at all cells (i,j) for which i+j=k, for some fixed value of k. It's easy to see that those cells will form a diagonal in the table going in the direction from bottom-left to top-right. Also, smaller values of k correspond to diagonals closer to the top-left corner of the table, while larger values of k correspond to diagonals closer to the bottom-right corner of the table. In a similar way, the set of cells for which ij=k forms a diagonal, but in the other direction. This means that if we are given fixed numbers a, b, c and d such that ab and cd, then the set of all cells (i,j) for which ai+jb and cijd actually forms a tilted rectangle. In case ba=dc, it is actually a tilted square, i.e. a diamond.

Therefore, to check if some region is a diamond or not, we keep track of the following: the number of visited cells (cnt), and the minimum and maximum diagonal in both directions (a=min(i+j), b=max(i+j), c=min(ij), d=max(ij)). What remains is to check whether ba=dc and to see if the number of visited cells matches the number of cells that should be in the diamond of this size (ba). In doing so, we need to calculate the number of . in a diamond of size n, which turns out to be n2+(n1)2.


Comments

There are no comments at the moment.