Problem 574624 · medium · Level 05 Advanced Algorithms & Graphs

Count Solid Tile Squares

dynamic programming · grid DP · counting

A mosaic floor tiles marks each tile with 1 (intact) or 0 (cracked). Count how many square groups of tiles (1x1, 2x2, 3x3, ...) consist only of intact tiles. Two squares are different if they cover different sets of tiles.

Examples

Input:  tiles = [[1,1,1],
                 [1,1,0],
                 [1,1,1]]
Output: 10
Explanation: eight 1x1 squares plus two 2x2 squares (top-left and bottom-left).

Input:  tiles = [[0]]
Output: 0

Constraints

  • 1 <= rows, cols <= 100
  • tiles[i][j] is 0 or 1
  • Target complexity: O(rows * cols).

Goals

  • Reuse the 'largest square ending here' state to count all squares at once
  • Notice that a cell with state s is the corner of exactly s squares
Starting Python…