Under construction

Still being built. Things may change

Soccer Stadium

Time limit3.5sMemory limit1024 MB

Summary
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 N×NN \times N grid of cells. The rows are numbered from 00 to N−1N - 1 from north to south, and the columns are numbered from 00 to N−1N - 1 from west to east. The cell at row rr and column cc is called cell (r,c)(r, c).

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 ss (where s≥1s \ge 1) is a set of ss distinct empty cells (r0,c0),…,(rs−1,cs−1)(r_0, c_0), \ldots, (r_{s-1}, c_{s-1}) such that for every pair i<ji < j, at least one of ri≠rjr_i \ne r_j and ci≠cjc_i \ne c_j 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 (r,a)(r, a) to cell (r,b)(r, b) (0≤r,a,b<N0 \le r, a, b < N, a≠ba \ne b), where the stadium contains all cells between cell (r,a)(r, a) and (r,b)(r, b) in row rr. Formally, if a<ba < b then the stadium contains cell (r,k)(r, k) for each kk with a≤k≤ba \le k \le b, and if a>ba > b then the stadium contains cell (r,k)(r, k) for each kk with b≤k≤ab \le k \le a.
  • Move the ball from cell (a,c)(a, c) to cell (b,c)(b, c) (0≤c,a,b<N0 \le c, a, b < N, a≠ba \ne b), where the stadium contains all cells between cell (a,c)(a, c) and (b,c)(b, c) in column cc. Formally, if a<ba < b then the stadium contains cell (k,c)(k, c) for each kk with a≤k≤ba \le k \le b, and if a>ba > b then the stadium contains cell (k,c)(k, c) for each kk with b≤k≤ab \le k \le a.

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 22 straight kicks. Any stadium of size 11 is regular.

In the example figure, cells with trees are darkened and cells in the stadium are striped, for a forest of size N=5N = 5 with trees at (1,0)(1, 0) and (4,2)(4, 2). The stadium on the left is regular. The stadium in the middle is not regular, because at least 33 straight kicks are needed to move the ball from cell (4,1)(4, 1) to (4,3)(4, 3). The stadium on the right is also not regular, because the ball cannot be moved from cell (3,0)(3, 0) to (1,3)(1, 3) using straight kicks.

The club wants to build a regular stadium that is as large as possible. Find the maximum ss such that a regular stadium of size ss exists in the forest.

Constraints

  • 1≤N≤20001 \le N \le 2000
  • 0≤F[i][j]≤10 \le F[i][j] \le 1 for each ii and jj with 0≤i<N0 \le i < N and 0≤j<N0 \le j < N.
  • There is at least one empty cell in the forest. In other words, F[i][j]=0F[i][j] = 0 for some 0≤i<N0 \le i < N and 0≤j<N0 \le j < N.

Examples1

  1. Example 1

    Input
    1
    0
    
    Expected output
    1