Editorial for Cake Cutting


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.

Author: tango

The most important thing is to realise that it is always possible to slice the cake appropriately. Once we do this, we can analyse the choices you can make when doing so.

Firstly, we outline a greedy strategy. Suppose we have a rectangular cake or slice of cake with multiple berries on it. Pick any line to cut along which would divide the cake into two rectangles with at least one berry on each side. (It's easy to see why this is possible; our subsequent arguments will also show this.) If we repeatedly apply this, we eventually get a cutting of the cake into rectangular slices each with at least one berry.

It is now useful to consider what could happen on the first cut under the greedy strategy. In particular, if we consider the leftmost and rightmost columns with a berry, any line strictly between these two can be our first cut. Similarly, any line strictly between the topmost and bottommost rows with a berry can be our first cut. (This shows a cut is possible: if there are at least two berries, they can't be all be in the same row and same column, so some cut is possible.)

Thus, for the cutting to be unique, all possible first choices must be part of the unique cutting. But this isolates every cell that lies weakly between the left- and rightmost columns and top- and bottommost rows into their own rectangles, so all of these cells must contain a berry. That is, the berries must form a solid (grid-aligned) rectangle shape.

Here is an example - the left- and rightmost columns are 2 and 5, the top- and bottommost rows are 2 and 4; every possible cut between these is made, and thus the 12 berries between these columns and rows are separated into their own slices.

It is not too hard to see that if the berries form this shape, the only possible cutting is the one suggested above: we can check that each icing cell can only be included in a rectangle with the same berry as the above configuration, because being in a rectangle with a different berry would cause two berries to end up in that rectangle.

Finally, there are many ways to test if the berries form a rectangle. A quick way as suggested by the above is to find the left- and rightmost columns with a berry, and the top- and bottommost rows with a berry, and then check if there are berries in all the cells weakly between these rows and columns. (In fact, we can just count the berries to make sure we have the right number for these particular extremal rows/columns, so the problem can actually be solved in O(\log(nm)) memory.)

Author's Note

This problem is related to the logic puzzle genre Shikaku - in particular, it asks when a Shikaku puzzle with no area clues is unique. (The insight about greedy algorithms comes from HMMT Team Round 2018/1.)

C++ Solution:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int h,w;
    cin >> h >> w;
    int berries=0,left=w,right=0,top=h,bottom=0;
    for(int r=0;r<h;r++){
        string row;
        cin >> row;
        for(int c=0;c<w;c++){
            if(row[c]=='#'){
                berries++;
                left=min(left,c);
                right=max(right,c);
                top=min(top,r);
                bottom=max(bottom,r);
            }
        }
    }
    if((bottom-top+1)*(right-left+1)==berries){
        cout << "unique cutting" << endl;
    }else{
        cout << "multiple cuttings" << endl;
    }
}

Python solution:

h,w=map(int,input().split())

berries=0
left=w
right=0
top=h
bottom=0
for r in range(h):
    row=input()
    for c in range(w):
        if row[c]=='#':
            berries+=1
            left=min(left,c)
            right=max(right,c)
            top=min(top,r)
            bottom=max(bottom,r)

if (bottom-top+1)*(right-left+1)==berries:
    print('unique cutting')
else:
    print('multiple cuttings')

Comments

There are no comments at the moment.