Soccer Stadium
Time limit3.5sMemory limit1024 MB
Given an N by N grid with trees, find the largest set of empty cells where any two cells connect within two straight row or column moves inside the set.
- Level
Hard9 of 10
- Topics
- Matrix, Dynamic programming, Stack
- Solved
- No attempts yet
Problem
Nagyerdő is a square-shaped forest located in the city of Debrecen, which can be modeled as an grid of cells. The rows are numbered from to from north to south, and the columns are numbered from to from west to east. The cell at row and column is called cell .
In the forest, each cell is either empty or contains a tree. At least one cell in the forest is empty.
DVSC, the sports club of the city, plans to build a new soccer stadium in the forest. A stadium of size (where ) is a set of distinct empty cells such that for every pair , at least one of and holds.
A ball is moved around the cells of the stadium. A straight kick is either of the following two actions:
- Move the ball from cell to cell (, ), where the stadium contains all cells between cell and in row . Formally, if then the stadium contains cell for each with , and if then the stadium contains cell for each with .
- Move the ball from cell to cell (, ), where the stadium contains all cells between cell and in column . Formally, if then the stadium contains cell for each with , and if then the stadium contains cell for each with .
A stadium is regular if the ball can be moved from any cell in the stadium to any other cell in the stadium using at most straight kicks. Any stadium of size is regular.

In the example figure, cells with trees are darkened and cells in the stadium are striped, for a forest of size with trees at and . The stadium on the left is regular. The stadium in the middle is not regular, because at least straight kicks are needed to move the ball from cell to . The stadium on the right is also not regular, because the ball cannot be moved from cell to using straight kicks.
The club wants to build a regular stadium that is as large as possible. Find the maximum such that a regular stadium of size exists in the forest.
Constraints
- for each and with and .
- There is at least one empty cell in the forest. In other words, for some and .