#PU740. USACO 2017 US Open Contest, Silver — Problem 3. Where's Bessie?

    ID: 1583 传统题 文件IO:where 2000ms 256MiB 尝试: 0 已通过: 0 难度: 提高 上传者: 标签>USACO赛事:open17来源:usaco.orgscoring:equal_test_cases枚举字符串

USACO 2017 US Open Contest, Silver — Problem 3. Where's Bessie?

USACO 2017 US Open Contest, Silver — Problem 3. Where's Bessie?

题目描述

Always known for being quite tech-savy, Farmer John is testing out his new automated drone-mounted cow locator camera, which supposedly can take a picture of his field and automatically figure out the location of cows. Unfortunately, the camera does not include a very good algorithm for finding cows, so FJ needs your help developing a better one. The overhead image of his farm taken by the camera is described by an N×NN \times N grid of characters, each in the range A…ZA \ldots Z, representing one of 26 possible colors. Farmer John figures the best way to define a potential cow location (PCL) is as follows: A PCL is a rectangular sub-grid (possibly the entire image) with sides parallel to the image sides, not contained within any other PCL (so no smaller subset of a PCL is also a PCL). Furthermore, a PCL must satisfy the following property: focusing on just the contents of the rectangle and ignoring the rest of the image, exactly two colors must be present, one forming a contiguous region and one forming two or more contiguous regions. For example, a rectangle with contents AAAAA ABABA AAABB would constitute a PCL, since the A's form a single contiguous region and the B's form more than one contiguous region. The interpretation is a cow of color A with spots of color B. A region is "contiguous" if you can traverse the entire region by moving repeatedly from one cell in the region to another cell in the region taking steps up, down, left, or right. Given the image returned by FJ's camera, please count the number of PCLs.

输入格式

The first line of input contains NN, the size of the grid (1≤N≤201 \leq N \leq 20). The next NN lines describe the image, each consisting of NN characters.

输出格式

Print a count of the number of PCLs in the image.

数据范围

(见题面原文)

样例

样例 1

4
ABBC
BBBC
AABB
ABBC
2
In this example, the two PCLs are the rectangles with contents
ABB
BBB
AAB
ABB
and
BC
BC
BB
BC
Problem credits: Brian Dean