CF510B.Fox And Two Dots

普及/提高-

通过率:0%

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×mn×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 d1,d2,...,dkd_{1},d_{2},...,d_{k} a cycle if and only if it meets the following condition:

  1. These kk dots are different: if iji≠j then did_{i} is different from djd_{j} .
  2. kk is at least 4.
  3. All dots belong to the same color.
  4. For all 1<=i<=k11<=i<=k-1 : did_{i} and di+1d_{i+1} are adjacent. Also, dkd_{k} and d1d_{1} should also be adjacent. Cells xx and yy are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

输入格式

The first line contains two integers nn and mm ( 2<=n,m<=502<=n,m<=50 ): the number of rows and columns of the board.

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

输出格式

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

输入输出样例

  • 输入#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).

首页