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

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

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char maze[][];
int vis[][];
int n,m;
int flag = ;
int to[][] = {{,},{-,},{,},{,-}};
bool check(int x,int y){
if(x<||x>=n||y<||y>=m) return false;
//if(vis[x][y]) return false;
return true;
}
void dfs(int x,int y,int prex,int prey){
if(!check(x,y)) return;
vis[x][y] = ;
int postx,posty;
for(int i = ; i<; i++){
postx = x + to[i][];
posty = y + to[i][];
if(check(postx,posty)&&maze[postx][posty] == maze[x][y]&&(postx!=prex||posty!=prey)){
if(vis[postx][posty]){
flag = ;
return;
}
dfs(postx,posty,x,y);
}
}
}
void input(){ scanf("%d%d",&n,&m);
for(int i = ; i<n; i++) scanf("%s",maze[i]);
for(int i = ; i<n; i++){
for(int j = ; j<m; j++){
if(!vis[i][j]){
dfs(i,j,-,-);
}
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
int main()
{
input();
return ;
}

卷珠帘

Fox And Two Dots的更多相关文章

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

  2. B. Fox And Two Dots

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

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

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

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

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

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

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

  7. CF 510b Fox And Two Dots

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  8. D - Fox And Two Dots DFS

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

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

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

随机推荐

  1. Adobe Flash CC 2014 下载及破解

    来源 :http://prodesigntools.com/adobe-cc-2014-direct-download-links.html 地址:http://trials3.adobe.com/A ...

  2. MVC 项目中又一新方法实现依懒注入 (AutoFac)

    详情请查看:http://docs.autofac.org/en/latest/integration/mvc.html#quick-start

  3. Ansible3:ansible.cfg配置说明【转】

    Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码.是否开启sudo认证.actio ...

  4. JSP基本语法--实例演练

    基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...

  5. iptables查看、添加、删除规则

    1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...

  6. zookeeper集群实例

    zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...

  7. pci 相关资料

    1.http://www.cnblogs.com/image-eye/archive/2012/02/15/2352699.html

  8. sinaBlog中小知识总结

    1.上拉刷新的时候记得移除老的数据(同样应用于其他地方) if (vc.currentPage == 1) { //上拉加载 记得移除新的 [vc.totalArr removeAllObjects] ...

  9. Android OpenGL ES(十一)绘制一个20面体 .

    前面介绍了OpenGL ES所有能够绘制的基本图形,点,线段和三角形.其它所有复杂的2D或3D图形都是由这些基本图形构成. 本例介绍如何使用三角形构造一个正20面体.一个正20面体,有12个顶点,20 ...

  10. iOS 判断数组不为空

    if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0)