P2372 yyy2015c01 Challenge: Compute the Perimeter

Background

yyy2015c01 quickly solved the problem and was praised by the neighbors. She happily went home, gave the candy to her mother, ate a delicious lunch, and took a sweet nap, feeling that life was wonderful. In the afternoon, her father came home and, upon hearing that yyy2015c01 had helped the teacher and neighbors with difficult problems, planned to take her to the amusement park to play her favorite dance machine as a reward. Hearing this, yyy2015c01 jumped for joy.

Description

The amusement park was crowded, and every dance machine was surrounded by people. What to do? With so many people, it would take a long time to play. yyy2015c01 looked around and suddenly found a dance machine with no one in front of it. “Haha, I found one with no queue, hurry up...”. But when she got there, she realized this new machine played differently: many `X` marks appeared at random positions on the floor grid. When you step on a cell, you must first compute the perimeter of the target it belongs to according to the rules, then input the correct perimeter into the machine. The final winner can receive a free game coupon from the amusement park. yyy2015c01 was tempted. Kids, can you help yyy2015c01 win the coupon? Game rules: There are many targets on the new dance machine’s rectangular grid. A dot `.` means an empty place; an uppercase `X` means part of a target. Each `X` stands for one full grid-square region, including its boundary and the target itself. An `X` is adjacent to any `X` in any of the 8 directions (sharing an edge or a corner). Any two adjacent `X` squares have overlapping boundaries or corners, so their square regions are considered adjacent. A target is a connected component formed by a chain of adjacent `X` squares under 8-direction adjacency. yyy2015c01 always steps on an `X`, selecting the target that contains that `X`. Record the coordinates where she steps. Row and column indices start from the top-left corner, numbered from $ 1 $. A useful statistic is the target’s perimeter. Assume each `X` square has side length $ 1 $. The target will not contain any completely enclosed holes. # Description

Input Format

There are $ M+1 $ lines in total. The first line contains four positive integers $ M, N, X, Y $ (separated by spaces), meaning the grid has $ M $ rows and $ N $ columns, and the stepped-on cell is at row $ X $, column $ Y $. The next $ M $ lines consist of characters `.` and `X`.

Output Format

Output a single line with the perimeter of the selected target.

Explanation/Hint

$ 1 \leq M \leq 20 $, $ 1 \leq N \leq 20 $, $ 1 \leq X \leq M $, $ 1 \leq Y \leq N $. Translated by ChatGPT 5