CF510B.Fox And Two Dots

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:

Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots _d_1, _d_2, ..., d__k a cycle if and only if it meets the following condition:

  1. These k dots are different: if i ≠ j then d__i is different from d__j.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1: d__i and d__i + 1 are adjacent. Also, d__k and _d_1 should also be adjacent. Cells x and y are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

狐狸雪儿正在玩一款名为“两点”的手机益智游戏。基础关卡在一个 n×mn \times m 的方格棋盘上进行,如下图所示:

每个格子中包含一个带有某种颜色的点。我们使用不同的大写拉丁字母来表示不同的颜色。

该游戏的关键在于找出一个由同色点构成的环。例如,图中四个蓝色点构成一个环。形式化地,我们称点序列 d1,d2,…,dkd_1, d_2, \dots, d_k 为一个环,当且仅当它满足以下条件:

  1. 这 kk 个点互不相同:若 i≠ji \ne j,则 di≠djd_i \ne d_j;
  2. k≥4k \ge 4;
  3. 所有点颜色相同;
  4. 对所有 1≤i≤k−11 \le i \le k-1,did_i 与 di+1d_{i+1} 相邻;且 dkd_k 与 d1d_1 也相邻。若两个格子 xx 与 yy 共享一条边,则称它们相邻。

请判断棋盘上是否存在这样的环。

输入格式

The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

第一行包含两个整数 nn 和 mm(2≤n,m≤502 \leq n, m \leq 50):棋盘的行数和列数。

接下来有 nn 行,每行包含一个长度为 mm 的字符串,表示该行中各点的颜色。每个字符均为大写拉丁字母。

输出格式

Output "Yes" if there exists a cycle, and "No" otherwise.

如果存在环,则输出“Yes”,否则输出“No”。

输入输出样例

  • 输入#1

    3 4
    AAAA
    ABCA
    AAAA

    输出#1

    Yes
  • 输入#2

    3 4
    AAAA
    ABCA
    AADA

    输出#2

    No
  • 输入#3

    4 4
    YYYR
    BYBY
    BBBY
    BBBY

    输出#3

    Yes
  • 输入#4

    7 6
    AAAAAB
    ABBBAB
    ABAAAB
    ABABBB
    ABAAAB
    ABBBAB
    AAAAAB

    输出#4

    Yes
  • 输入#5

    2 13
    ABCDEFGHIJKLM
    NOPQRSTUVWXYZ

    输出#5

    No

说明/提示

In first sample test all 'A' form a cycle.

In second sample there is no such cycle.

The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).

在第一个样例测试中,所有 'A' 构成一个环。

在第二个样例中,不存在这样的环。

第三个样例显示在上方图片中('Y' = 黄色,'B' = 蓝色,'R' = 红色)。

输入解题思路,AI测评打分。不知道怎么写?

首页