Fox and the Dots

Source: Internet
Author: User

B-fox and both DotsTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64d &%I 64u Submit Status

Description

Fox Ciel is playing a mobile puzzle game called ". The basic levels is played on a board of size n x m cells, like this:

Each cell contains a dot this has some color. We'll use the different uppercase Latin characters to express different colors.

The key of this game was 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 an D only if it meets the following condition:

  1. These k Dots is different:if iJ and di is Different from DJ.
  2. K is at least 4.
  3. All dots belong to the same color.
  4. For all 1≤ ik -1: di and di + 1 is Adjac Ent. Also, dK and D1 should Also be adjacent. Cells x and y is called adjacent if they share an edge.

Determine if there exists a cycle on the field.

Input

The first line contains integers n and m (2≤ n, m ≤50): t He number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters, expressing colors O f dots in each line. Each character is a uppercase Latin letter.

Output

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

Sample Input

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

Hint

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

Euler circuit, with BFS made a pass, found a good mess, learn the great God Dfs, listen to Fang said there is a very simple way to continue to look at tomorrow.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 Charmaze[ -][ -];8 intvis[ -][ -];9 intn,m;Ten intFlag =0; One intto[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; A BOOLCheckintXinty) { -     if(x<0|| x>=n| | y<0|| Y&GT;=M)return false; -     //if (Vis[x][y]) return false; the     return true; - } - voidDfsintXintYintPrexintprey) { -     if(!check (x, y))return; +Vis[x][y] =1; -     intpostx,posty; +      for(inti =0; i<4; i++){ APOSTX = x + to[i][0]; atPosty = y + to[i][1]; -         if(Check (postx,posty) &&maze[postx][posty] = = maze[x][y]&& (postx!=prex| | posty!=prey)) { -             if(Vis[postx][posty]) { -Flag =1; -                 return; -             } in DFS (postx,posty,x,y); -         } to     } + } - voidinput () { the  *scanf"%d%d",&n,&m); $      for(inti =0; i<n; i++) scanf ("%s", Maze[i]);Panax Notoginseng      for(inti =0; i<n; i++){ -          for(intj =0; j<m; J + +){ the             if(!Vis[i][j]) { +DFS (i,j,-1,-1); A             } the         } +     } -     if(flag) printf ("yes\n"); $     Elseprintf"no\n"); $ } - intMain () - { the input (); -     return 0;Wuyi}
Volume bead Curtain

Fox and the Dots

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.