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

  1. 3 4
    AAAA
    ABCA
    AAAA

output

  1. Yes

input

  1. 3 4
    AAAA
    ABCA
    AADA

output

  1. No

input

  1. 4 4
    YYYR
    BYBY
    BBBY
    BBBY

output

  1. Yes

input

  1. 7 6
    AAAAAB
    ABBBAB
    ABAAAB
    ABABBB
    ABAAAB
    ABBBAB
    AAAAAB

output

  1. Yes

input

  1. 2 13
    ABCDEFGHIJKLM
    NOPQRSTUVWXYZ

output

  1. 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判断有没有回到出发的点,并保证不走回头路(所以dfs函数里定义四个变量)

  1. #include<cstdio>
  2. #include<cstring>
  3. int flag[][];
  4. char str[][];
  5. int n,m,k;
  6. int dx[]={-,,,};
  7. int dy[]={,,-,};
  8. void dfs(int x,int y,int cx,int cy)
  9. {
  10. if(flag[x][y] == )
  11. {
  12. k=;
  13. return ;
  14. }
  15. flag[x][y]=;
  16. int i,nx,ny;
  17. for(i = ; i < ; i++)
  18. {
  19.  
  20. nx=x+dx[i];
  21. ny=y+dy[i];
  22. if(str[nx][ny] == str[x][y] && (nx != cx || ny != cy))
  23. {
  24.  
  25. dfs(nx,ny,x,y);
  26.  
  27. }
  28. }
  29.  
  30. }
  31. int main()
  32. {
  33. while(scanf("%d %d",&n,&m)!=EOF)
  34. {
  35. k=;
  36. int i,j;
  37. memset(flag,,sizeof(flag));
  38. for(i = ; i <= n ; i++)
  39. {
  40. scanf("%s",str[i]+);
  41. }
  42. for(i = ; i <= n ; i++)
  43. {
  44. for(j = ; j <= m ; j++)
  45. {
  46. if(flag[i][j] == )
  47.  
  48. continue;
  49.  
  50. dfs(i,j,i,j);
  51.  
  52. if(k == )
  53.  
  54. break;
  55. }
  56. if(k == )
  57. break;
  58. }
  59. if(k == )
  60. printf("Yes\n");
  61. else
  62. printf("No\n");
  63. }
  64. }

CF 510b Fox And Two Dots的更多相关文章

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

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

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

  3. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

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

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

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

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

  7. B. Fox And Two Dots

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

  8. Fox And Two Dots

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

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

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

随机推荐

  1. net core 认证及简单集群

    net core 认证及简单集群 在Asp.net WebAPI中,认证是通过AuthenticationFilter过滤器实现的,我们通常的做法是自定义AuthenticationFilter,实现 ...

  2. javascript结合nodejs实现多文件上传

    前端文件上传功能比较依赖后端,所以第一步用nodejs实现一个供文件上传的功能接口. 因为本人对nodejs也是一知半解,所以刚开始的想法是像原始的ajax交互那样,获取上传文件的内容,然后再通过no ...

  3. Android 适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  4. SIGGRAPH 2017:深度学习与计算机图形学的碰撞

    每年由美国计算机协会(Association of Computing Machinery,简称ACM)计算机图形专业组举办的年会SIGGRAPH,是全球最负盛名的图形学和交互技术盛会.今年已经是这场 ...

  5. URAL 1057 Amount of Degrees (数位DP,入门)

    题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+2 ...

  6. 使用JS的画布制作一个瞄准镜

    <canvas width="600" height="500" id="myCanvas"></canvas> & ...

  7. CF Gym 100187D Holidays (数学,递推)

    题意:给n个元素,从n中选两个非空集合A和B.问有多少中选法? 递推: dp[n]表示元素个数为n的方案数,对于新来的一个元素,要么加入集合,要么不加入集合自成一个集合.加入集合有三种选择,A,B,E ...

  8. Android(java)学习笔记125:保存数据到SD卡 (附加:保存数据到内存)

    1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...

  9. 干净卸载 Cloudera CDH 5 beta2

    Cloudera 的官方介绍: http://www.cloudera.com/content/cloudera-content/cloudera-docs/CM4Ent/4.8.1/Cloudera ...

  10. 【转】OS X 中快速调出终端

    作者:Frank Pu链接:https://www.zhihu.com/question/20692634/answer/37152883来源:知乎著作权归作者所有,转载请联系作者获得授权. 来至 M ...