Fox And Two Dots-dfs

Posted ljhaha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fox And Two Dots-dfs相关的知识,希望对你有一定的参考价值。

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

题意:给出n*m大小的string数组,问数组中是否存在由同一个字母构成的环,形如下图情况之一,即认为构成了环。

BB      AAA
BB ABA
AAA
(1) (2)

思路:从前到最后进行一次bfs,在每一个点向四周搜索(但是不能走回头路),每搜到一个点就用vis标记一下。

如果搜到了vis标记过的点,而且和这个点是同一种颜色,那么说明肯定打环了,说明就成立。

转载自:https://blog.csdn.net/wyg1997/article/details/52208683

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char mp[55][55];
int vis[55][55];
int dir[4][2]=-1,0,0,1,1,0,0,-1;
bool ans;
//只要不经过 上一个 已标记过的点(nx,ny),就可以证明是 Yes.
void dfs(int x,int y,int nx,int ny)

    if(vis[x][y])
        ans=true;
        return;
    
    vis[x][y]=true;
    for(int i=0;i<4;i++)
        int xx=x+dir[i][0],yy=y+dir[i][1];
        if(xx==nx&&yy==ny)
            continue;
        if(mp[xx][yy]==mp[x][y])
            dfs(xx,yy,x,y);
        
    

int main()

    while(~scanf("%d%d",&n,&m))
        for(int i=0;i<n;i++)
            scanf("%s",mp[i]);
        
        memset(vis,false,sizeof(vis));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(!vis[i][j])
                    dfs(i,j,-1,-1);//(0,0)的前一个节点为(-1,-1)
                
                if(ans)
                    break;
                
            
            if(ans)
                break;
        
        if(ans)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    
    return 0;

 

以上是关于Fox And Two Dots-dfs的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

Codeforces 388 D. Fox and Perfect Sets

A - Fox And Snake

Topcoder SRM590 Fox And City

Fox And Jumping

网络流(最大流)CodeForces 512C:Fox And Dinner