Fox And Two Dots
64-bit integer IO format: %I64d Java class name: (Any)

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:
- These k dots are different: if i ≠ j then di is different from dj.
- k is at least 4.
- All dots belong to the same color.
- 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.
Sample Input
3 4
AAAA
ABCA
AAAA
Yes
3 4
AAAA
ABCA
AADA
No
4 4
YYYR
BYBY
BBBY
BBBY
Yes
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
Yes
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
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).
Source


1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 char Map[55][55];/*地图*/ 6 int MAP[55][55];/*标记走过的点*/ 7 int Len_X,Len_Y; 8 int SIGN;/*SING=1表示没有找到结果,SIGN=0表示找到环*/ 9 void DFS(int x,int y,char str,int N)/*深度搜索,str为最初字符,N表示方向*/ 10 { 11 12 if(!SIGN)return;/*如果找到一个条件,结束搜索*/ 13 if(Map[x][y]==str)/*判断如果当前字符一样*/ 14 { 15 if(MAP[x][y]==1)/*字符一样且搜索过说明构成了环*/ 16 {SIGN=0;return;} 17 else /*否则,继续进行循环*/ 18 { 19 MAP[x][y]=1; /*标记该点已经走过*/ 20 if(N!=0)DFS(x-1,y,str,1); /*防止反方向搜索*/ 21 if(N!=1)DFS(x+1,y,str,0); /*防止反方向搜索*/ 22 if(N!=2)DFS(x,y-1,str,3); /*防止反方向搜索*/ 23 if(N!=3)DFS(x,y+1,str,2); /*防止反方向搜索*/ 24 } 25 } 26 } 27 28 int main() 29 { 30 int N,M; 31 int i,j; 32 char str; 33 while(scanf("%d%d",&Len_X,&Len_Y)!=EOF) 34 { 35 memset(Map,'*',sizeof(Map));/*地图初始化,建围墙*/ 36 memset(MAP,-1,sizeof(MAP)); /*标记数组初始化,建围墙*/ 37 for(i=1;i<=Len_X;i++) 38 { 39 scanf(" %s",Map[i]+1);/*输入地图*/ 40 Map[i][Len_Y+1]='*';/*处理最后一个字符\n*/ 41 } 42 for(i=1,SIGN=1;i<=Len_X&&SIGN;i++) 43 { 44 for(j=1;j<=Len_Y&&SIGN;j++) 45 { 46 if(MAP[i][j]==-1)/*如果该数组没有被标记过,从该点进行查找*/ 47 { 48 MAP[i][j]=1; /*标记该点已经使用过*/ 49 DFS(i+1,j,Map[i][j],0);/*向下搜索*/ 50 DFS(i-1,j,Map[i][j],1);/*向上搜索*/ 51 DFS(i,j+1,Map[i][j],2);/*向右搜索*/ 52 DFS(i,j-1,Map[i][j],3);/*向左搜索*/ 53 } 54 } 55 } 56 if(!SIGN)printf("Yes\n"); 57 else printf("No\n"); 58 } 59 return 0; 60 }
修改:2015.5.8


1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 using namespace std; 5 int Len_X,Len_Y,SIGN; 6 char Map[100][100]; 7 int Map_S[100][100]; 8 void DFS(int x,int y,char Str,int N)/*N用来控制方向*/ 9 { 10 if(SIGN==1)return ; 11 if(Map_S[x][y]==1)/*如果该点走过,且和原本相同则说明构成环*/ 12 { 13 if(Map[x][y]==Str)SIGN=1; 14 return ; 15 } 16 if(Map[x][y]==Str)/**/ 17 { 18 Map_S[x][y]=1;/*标记该点走过*/ 19 if(N!=1)DFS(x+1,y,Str,0);/*防止方向搜索*/ 20 if(N!=0)DFS(x-1,y,Str,1);/*防止方向搜索*/ 21 if(N!=3)DFS(x,y+1,Str,2);/*防止方向搜索*/ 22 if(N!=2)DFS(x,y-1,Str,3);/*防止方向搜索*/ 23 } 24 return ; 25 } 26 27 int main() 28 { 29 int Begin_x,Begin_y,i,j,k,A,B; 30 while(scanf("%d%d",&Len_X,&Len_Y)!=EOF) 31 { 32 for(i=0;i<=Len_X+1;i++) 33 { 34 for(j=0;j<=Len_Y+1;j++) 35 { 36 if(i==0||j==0||i==Len_X+1||j==Len_Y+1) 37 {Map_S[i][j]=1;Map[i][j]='*';} 38 else 39 { 40 scanf(" %c",&Map[i][j]); 41 Map_S[i][j]=0; 42 } 43 44 } 45 } 46 47 for(i=1,SIGN=0;i<=Len_X;i++) 48 { 49 for(j=1;j<=Len_Y;j++) 50 { 51 for(k=0;k<4;k++) /*判断四个方向*/ 52 { 53 if(Map_S[i][j]==0)/*判断改点是否已经走过*/ 54 DFS(i,j,Map[i][j],k); 55 } 56 } 57 } 58 if(SIGN)printf("Yes\n"); 59 else printf("No\n"); 60 } 61 return 0; 62 }