Fox And Two Dots

B - Fox And Two Dots

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

Submit Status

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:

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.

Sample Input

Input

3 4AAAAABCAAAAA

Output

Yes

Input

3 4AAAAABCAAADA

Output

No

Input

4 4YYYRBYBYBBBYBBBY

Output

Yes

Input

7 6AAAAABABBBABABAAABABABBBABAAABABBBABAAAAAB

Output

Yes

Input

2 13ABCDEFGHIJKLMNOPQRSTUVWXYZ

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

欧拉回路,用bfs搞了一通,发现好乱,学习大神DFS,听房神说还有很简单的方法,明天继续看一下。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 char maze[55][55];
 8 int vis[55][55];
 9 int n,m;
10 int flag = 0;
11 int to[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
12 bool check(int x,int y){
13     if(x<0||x>=n||y<0||y>=m) return false;
14     //if(vis[x][y]) return false;
15     return true;
16 }
17 void dfs(int x,int y,int prex,int prey){
18     if(!check(x,y)) return;
19     vis[x][y] = 1;
20     int postx,posty;
21     for(int i = 0; i<4; i++){
22         postx = x + to[i][0];
23         posty = y + to[i][1];
24         if(check(postx,posty)&&maze[postx][posty] == maze[x][y]&&(postx!=prex||posty!=prey)){
25             if(vis[postx][posty]){
26                 flag = 1;
27                 return;
28             }
29             dfs(postx,posty,x,y);
30         }
31     }
32 }
33 void input(){
34
35     scanf("%d%d",&n,&m);
36     for(int i = 0; i<n; i++) scanf("%s",maze[i]);
37     for(int i = 0; i<n; i++){
38         for(int j = 0; j<m; j++){
39             if(!vis[i][j]){
40                 dfs(i,j,-1,-1);
41             }
42         }
43     }
44     if(flag) printf("Yes\n");
45     else printf("No\n");
46 }
47 int main()
48 {
49     input();
50     return 0;
51 }

卷珠帘

时间: 2024-08-05 17:05:49

Fox And Two Dots的相关文章

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

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m ce

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

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m ce

CodeForces 510 B. Fox And Two Dots(DFS 啊)

题目链接:http://codeforces.com/problemset/problem/510/B 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 diff

CF 510b Fox And Two Dots

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 differen

Codeforces 510B Fox And Two Dots 【DFS】

好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <bits/stdc++.h> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (

Fox And Two Dots codeforces 510B(DFS)

http://codeforces.com/problemset/problem/510/B 题意:问你能否用相同的字母构成一个环. 分析:每一个都直接从它自身开始,看看到最后是否还能回到它本身.(注意:需要最少4个点) #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algori

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

题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易知道要用到深搜.暴力搜索--- 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 cons

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

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

#290 (div.2) B. Fox And Two Dots

1.题目描述:点击打开链接 2.解题思路:本题利用DFS来解决.本题要求判断一个图中是否存在相同颜色的圈.显然需要利用DFS来寻找.那么该如何寻找呢?题目中已经告诉了我们如何判断一个圈.那么只用根据题意描述来写DFS即可.从没有搜索过的结点开始,每次都找与它相邻的且颜色相同的结点来扩展,此时为了防止重复扩展,需要在DFS参数列表中加上前驱结点.这样以来,一旦发现某一个结点曾经已经标记过,说明找到了一个圈.直接输出Yes并退出循环. 3.代码: #define _CRT_SECURE_NO_WAR