#P16838. [GKS 2021 #A] L Shaped Plots

    ID: 19165 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP数学递推2021Google Kick Start

[GKS 2021 #A] L Shaped Plots

Problem Description

Given a grid of RR rows and CC columns each cell in the grid is either 00 or 11.

A segment is a nonempty sequence of consecutive cells such that all cells are in the same row or the same column. We define the length of a segment as number of cells in the sequence.

A segment is called "good" if all the cells in the segment contain only 11s.

An "L-shape" is defined as an unordered pair of segments, which has all the following properties:

  • Each of the segments must be a "good" segment.
  • The two segments must be perpendicular to each other.
  • The segments must share one cell that is an endpoint of both segments.
  • Segments must have length at least 22.
  • The length of the longer segment is twice the length of the shorter segment.

We need to count the number of L-shapes in the grid.

Below you can find two examples of correct L-shapes,

:::align{center} :::

and three examples of invalid L-shapes.

:::align{center} :::

Note that in the shape on the left, 22 segments do not share a common endpoint. The next 22 shapes do not meet the last requirement, as in the middle shape both segments have the same length, and in the last shape the longer segment is longer than twice the length of the shorter one.

Input Format

The first line of the input contains the number of test cases, TT. TT test cases follow.

The first line of each testcase contains two integers RR and CC.

Then, RR lines follow, each line with CC integers representing the cells of the grid.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the number of L-shapes.

2
4 3
1 0 0
1 0 1
1 0 0
1 1 0
6 4
1 0 0 0
1 0 0 1
1 1 1 1
1 0 1 0
1 0 1 0
1 1 1 0
Case #1: 1
Case #2: 9

Hint

In Sample Case #11, there is 11 L-shape.

  • The 11st one is formed by using cells: (1,1),(2,1),(3,1),(4,1),(4,2)(1,1), (2,1), (3,1), (4,1), (4,2)

:::align{center} :::

In Sample Case #22, there are 99 L-shapes.

  • The 11st one is formed by using cells: $(1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (6,2), (6,3)$
  • The 22nd one is formed by using cells: (3,1),(4,1),(5,1),(6,1),(6,2)(3,1), (4,1), (5,1), (6,1), (6,2)
  • The 33rd one is formed by using cells: (6,1),(5,1),(4,1),(3,1),(3,2)(6,1), (5,1), (4,1), (3,1), (3,2)
  • The 44th one is formed by using cells: (3,3),(4,3),(5,3),(6,3),(6,2)(3,3), (4,3), (5,3), (6,3), (6,2)
  • The 55th one is formed by using cells: (6,3),(5,3),(4,3),(3,3),(3,2)(6,3), (5,3), (4,3), (3,3), (3,2)
  • The 66th one is formed by using cells: (3,1),(3,2),(3,3),(3,4),(2,4)(3,1), (3,2), (3,3), (3,4), (2,4)
  • The 77th one is formed by using cells: (3,4),(3,3),(3,2),(3,1),(2,1)(3,4), (3,3), (3,2), (3,1), (2,1)
  • The 88th one is formed by using cells: (3,4),(3,3),(3,2),(3,1),(4,1)(3,4), (3,3), (3,2), (3,1), (4,1)
  • The 99th one is formed by using cells: (6,3),(5,3),(4,3),(3,3),(3,4)(6,3), (5,3), (4,3), (3,3), (3,4)

The 11st 33 L-shapes are shown on the picture below.

:::align{center} :::

Limits

1T1001 \le T \le 100.

Grid consists of 00s and 11s only.

Test Set 11

1R401 \le R \le 40.

1C401 \le C \le 40.

Test Set 22

1R10001 \le R \le 1000 and 1C10001 \le C \le 1000 for at most 55 test cases.

For the remaining cases, 1R401 \le R \le 40 and 1C401 \le C \le 40.