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

梦里梦外; 2022-09-25 05:18 233阅读 0赞

E - E

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 510B

Description

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:

Center

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, …, d**k a cycle if and only if it meets the following condition:

  1. These k dots are different: if i ≠ j then d**i is different from d**j.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1: d**i and d**i + 1 are adjacent. Also, d**k 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

Input

  1. 3 4
  2. AAAA
  3. ABCA
  4. AAAA

Output

  1. Yes

Input

  1. 3 4
  2. AAAA
  3. ABCA
  4. AADA

Output

  1. No

Input

  1. 4 4
  2. YYYR
  3. BYBY
  4. BBBY
  5. BBBY

Output

  1. Yes

Input

  1. 7 6
  2. AAAAAB
  3. ABBBAB
  4. ABAAAB
  5. ABABBB
  6. ABAAAB
  7. ABBBAB
  8. AAAAAB

Output

  1. Yes

Input

  1. 2 13
  2. ABCDEFGHIJKLM
  3. NOPQRSTUVWXYZ

Output

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

我用深度优先搜索做的,

ac代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int N = 55;
  7. int px[4]= {1,-1,0,0};
  8. int py[4]= {0,0,1,-1};
  9. char map[N][N];
  10. bool vis[N][N];
  11. int temp,c1,c2,n,m;
  12. bool check(int x,int y) {
  13. int flag=0;
  14. for(int k=0; k<4; k++) {
  15. int u=x+px[k];
  16. int v=y+py[k];
  17. if(u>=0&&v>=0&&u<n&&v<m&&map[u][v]==map[c1][c2]&&vis[u][v])
  18. flag++;
  19. }
  20. // printf("%d---\n",flag);
  21. if(flag>=2)
  22. return true;
  23. return false;
  24. }
  25. bool judge(int x,int y) {
  26. if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]==map[c1][c2]&&!vis[x][y])
  27. return true;
  28. return false;
  29. }
  30. void dfs(int i,int j,int cnt) {
  31. cnt++;
  32. vis[i][j]=true;
  33. if(check(i,j)&&cnt>=4)
  34. temp=1;
  35. for(int l=0; l<4; l++) {
  36. int nx=i+px[l];
  37. int ny=j+py[l];
  38. if(judge(nx,ny)) {
  39. vis[nx][ny]=true;
  40. dfs(nx,ny,cnt);
  41. }
  42. }
  43. }
  44. int main() {
  45. while(scanf("%d%d",&n,&m)!=EOF) {
  46. temp=0;
  47. for(int i=0; i<n; i++) {
  48. scanf("%s",map[i]);
  49. }
  50. memset(vis,false,sizeof(vis));
  51. for(int i=0; i<n; i++) {
  52. for(int j=0; j<m; j++) {
  53. if(!vis[i][j]) {
  54. c1=i,c2=j;
  55. dfs(i,j,0);
  56. }
  57. }
  58. }
  59. printf(temp==1?"Yes\n":"No\n");
  60. }
  61. return 0;
  62. }

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读