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

题目大意  :判断图中是否有些相同字母组成的环,如果有的话直接出书YES,没有输出NO

思路 :  图中的每个点都有可能构成循环,所以要逐一遍历,如果该点可以构成循环的环 那么起点是该点,,终点也是该点,环至少是4个点构成,所以如果有环的话再次回到起点时步数一定大于等于四。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,end_i,end_j;
char aa;
char arr[][];
int mark[][]={};
int d[][]={{,},{-,},{,},{,-}};
int flag=;
void dfs(int x,int y,int step)
{
if(flag==) return;
if(step>=&&x==end_i&&y==end_j)如果回到了起点且步数大于等于4 则找到环了。
{
flag=;
return;
}
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]==aa){
if(dx==end_i&&y==end_j&&step<)//起点就是终点所以起点没有被标记,当进入到第二个点时,可能会回到起点,所以要对步数进行判断,看是否大于等于3
continue;
mark[dx][dy]=;
dfs(dx,dy,step+);
}
}
}
int main()
{
cin>>n>>m;//n行m列
for(int i=;i<n;i++) scanf("%s",&arr[i]);//存图 for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
memset(mark,,sizeof(mark));
end_i=i;
end_j=j;//起点与终点
aa=arr[i][j];//搜索的字符
dfs(i,j,);
if(flag==)
break;
}
if(flag==) break;
}
if(flag==) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return ;
}

D - Fox And Two Dots DFS的更多相关文章

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

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  2. CF Fox And Two Dots (DFS)

    Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. CodeForces - 510B Fox And Two Dots (bfs或dfs)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. 17-比赛2 F - Fox And Two Dots (dfs)

    Fox And Two Dots CodeForces - 510B ================================================================= ...

  5. B. Fox And Two Dots

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Fox And Two Dots

    B - Fox And Two Dots Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  7. CF510B Fox And Two Dots(搜索图形环)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

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

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  9. Codeforces 510B Fox And Two Dots 【DFS】

    好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...

随机推荐

  1. KMP 算法简单解释

    ​ 讲KMP算法,离不开BF,实际上,KMP就是BF升级版,主要流程和BF一样 ​ 不同是在匹配失败时能利用子串的特征减少回溯,利用根据子串特征生成的Next数组来减少 <( ̄︶ ̄)↗[GO!] ...

  2. DNS提供的服务

    DNS提供的服务 DNS是:1.一个由分层的DNS服务器实现的分布式数据库:2. 一个使主机能够查询分布式数据库的应用协议.DNS协议运行在UDP上,使用53号端口. 与http,FTP,SMTP协议 ...

  3. Codeforces Round #567 (Div. 2) B. Split a Number

    Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  4. javaAPI操作ES分组聚合

    连接es的客户端使用的 TransportClient SearchRequestBuilder requestBuilder = transportClient.prepareSearch(indi ...

  5. Mysql失败,异常pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9D\\x90\\xBF;......

    问题描述: 插入Mysql时失败了,python代码报如下异常: pymysql.err.InternalError: (1366, "Incorrect string value: '\\ ...

  6. 面试刷题22:CAS和AQS是什么?

    java并发包提供的同步工具和线程池,底层是基于什么原理来设计和实现的呢?这个非常重要. 我是李福春,我在准备面试,今天的题目是: CAS和AQS是什么? 答:CAS是一系列的操作集合,获取当前值进行 ...

  7. 如何设置mysql远程访问

    如何设置mysql远程访问 Mysql默认是不可以通过远程机器访问的,通过下面的配置可以开启远程访问 在MySQL Server端: 执行mysql 命令进入mysql 命令模式, mysql> ...

  8. 洛谷5026 Lycanthropy 差分套差分

    题目链接 https://www.luogu.com.cn/problem/P5026 题意 在一个长度为m的序列中,每次给一个下标x,和一个权值v,然后从x-v*3到x-v*2单调递增,从x-v*2 ...

  9. Springboot 事务注解--- @Transactional

    spring boot @Transactional事物处理    spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...

  10. 利用sqlmap进行Access和Mysql注入

    sqlmap将检测结果保存到C:\Users\Administrator.sqlmap\output (windows) linux:(/root/.sqlmap/output) Access注入 1 ...