codeforces 510BFox And Two Dots 深搜dfs

Fox And Two Dots

 CodeForces - 510B 

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 d1, d2, ..., dk a cycle if and only if it meets the following condition:

  1. These k dots are different: if i ≠ j then di is different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 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.

Input

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.

Output

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

Examples

Input

3 4
AAAA
ABCA
AAAA

Output

Yes

Input

3 4
AAAA
ABCA
AADA

Output

No

Input

4 4
YYYR
BYBY
BBBY
BBBY

Output

Yes

Input

7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

Output

Yes

Input

2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ

Output

No

Note

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).

思路:

并查集写法:

       对于每个字母,我们都赋给他们一个不同的值,然后按行从左往右扫,对于扫到的元素,我们找到它本身和它向左和向上的根节点(如果这三个点的字母相同的话),找这两个的作用是一个用来与它建立一个集合,另一个就可以用它过来判环了,于是这道题就转变成了无想吐的判环问题

dfs写法:

     对于相同的字母我们进行搜索,但是要记录一下它上次走过的位置,这样只要从当前状态走到了一个走过的点,他就一定是成环的

代码:

并查集:

#include
using namespace std;
#define N 55
char ch[N][N];
int num[N][N],f[N * N],n,m;
int dx[2] = {-1,0};
int dy[2] = {0,-1};
bool check(int x,int y)
{
    if (x >= 0 && x < n && y >= 0 && y < m)
        return 1;
    return 0;
}
int find(int x)
{
    if (f[x] != x)
        f[x] = find(f[x]);
    return f[x];
}
int main()
{
    int pos = 1;
    scanf("%d %d",&n,&m);
    for (int i = 1;i <= n * m;i ++) f[i] = i;
    for (int i = 0;i < n;i ++) scanf("%s",ch[i]);
    for (int i = 0;i < n;i ++)
        for (int j = 0;j < m;j ++)
        {
            num[i][j] = pos ++;
            for (int k = 0;k < 2;k ++)
            {
                int ex = i + dx[k],ey = j + dy[k];
                if (check(ex,ey) && ch[ex][ey] == ch[i][j])
                {
                    int u = find(num[i][j]),v = find(num[ex][ey]);
                    if (u == v)
                    {
                        puts("Yes");
                        return 0;
                    }
                    f[u] = v;
                }
            }
        }
    puts("No");
    return 0;
}

 dfs:

#include
using namespace std;
#define N 55
int n,m;
char mp[N][N];
bool vis[N][N],flag = 0;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool check(int x,int y)
{
    if (x >= 0 && x < n && y >= 0 && y < m)
        return 1;
    return 0;
}
void dfs(int x,int y,int lx,int ly,char w)
{
    vis[x][y] = 1;
    for (int i = 0;i < 4;i ++)
    {
        int ex = x + dx[i],ey = y + dy[i];
        if (ex == lx && ey == ly) continue;
        if (check(ex,ey) && mp[ex][ey] == w)
        {
            if (vis[ex][ey])
            {
                flag = 1;
                break;
            }
            dfs(ex,ey,x,y,w);
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for (int i = 0 ;i < n;i ++)
        scanf("%s",mp[i]);
    for (int i = 0;i < n;i ++)
        for (int j = 0;j < m;j ++)
        {
            if (!vis[i][j])
            {
                dfs(i,j,-1,-1,mp[i][j]);
                if (flag)
                {
                    puts("Yes");
                    return 0;
                }
            }
        }
    puts("No");
    return 0;
}

 

你可能感兴趣的:(dfs深搜,codeforces)