ILP Part 44 — Pizza riddle simplified

This is the forty forth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve Pizza practice problem from Hash Code 2017 contest organized by Google.

Task description

Introduction

Did you know that at any given time, someone is cutting pizza somewhere around the world? The decision
about how to cut the pizza sometimes is easy, but sometimes it’s really hard: you want just the right amount
of tomatoes and mushrooms on each slice. If only there was a way to solve this problem using technology…

Problem description

Pizza
The pizza is represented as a rectangular, 2-dimensional grid of R rows and C columns. The cells within the
grid are referenced using a pair of 0-based coordinates [r, c] , denoting respectively the row and the
column of the cell.

Each cell of the pizza contains either:
– mushroom, represented in the input file as M ; or
– tomato, represented in the input file as T

Slice
A slice of pizza is a rectangular section of the pizza delimited by two rows and two columns, without holes.
The slices we want to cut out must contain at least L cells of each ingredient (that is, at least L cells of
mushroom and at least L cells of tomato) and at most H cells of any kind in total – surprising as it is, there is
such a thing as too much pizza in one slice.
The slices being cut out cannot overlap. The slices being cut do not need to cover the entire pizza.

Goal
The goal is to cut correct slices out of the pizza maximizing the total number of cells in all slices.
Input data set
The input data is provided as a data set file – a plain text file containing exclusively ASCII characters with
lines terminated with a single ‘\n’ character at the end of each line (UNIX- style line endings).

File format
The file consists of:
– one line containing the following natural numbers separated by single spaces:
— R (1 ≤ R ≤ 1000) is the number of rows,
— C (1 ≤ C ≤ 1000) is the number of columns,
— L (1 ≤ L ≤ 1000) is the minimum number of each ingredient cells in a slice,
— H (1 ≤ H ≤ 1000) is the maximum total number of cells of a slice
– R lines describing the rows of the pizza (one after another). Each of these lines contains C
characters describing the ingredients in the cells of the row (one cell after another). Each character
is either ‘M’ (for mushroom) or ‘T’ (for tomato).

Solution

For every pizza cell we create a non-negative variable representing number of assigned slice. There is at most RC slices. We also simplify problem a little bit and assume that each slice must be a rectangle with at least two rows and two columns, however, code can be easily extended to handle other situations.