Editorial for COCI '13 Contest 2 #2 Misa
Submitting an official solution before solving the problem yourself is a bannable offence.
We check if each seating place inside the church is available or not. If it is available, we check for the number of its non-available neighbours, to find out whether Mirko will sit there. If it is not available, yet again we need the number of its non-available neighbours in order to count them in the total number of handshakes.
How to check an item's neighbours simply? Its neighbours are the following:
For each item, we must check whether it is a part of our seating matrix (that is, if its coordinates are greater than zero and not greater than or ) and whether it is already occupied. Manually checking each condition can be time-consuming, so we use two arrays to help us:
- array ,
- array .
Using a for loop, we will iterate from to and calculate for each :
If you think about it, you will see that this way we iterate through all of our current element's neighbours. Inside the for loop we still have to check the aforementioned conditions.
The total number of handshakes during the Mass, without Mirko, is equal to the sum of calculated handshakes of all neighbouring non-available elements divided by two, because we counted each handshake twice. We still have to add the number of Mirko's handshakes, which is represented by the maximum number of neighbouring items of the current item.
Comments