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. IoC容器Autofac正篇之简单实例(四)

    先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...

  2. LeetCode OJ 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  3. 监控 monitor java 代码

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  4. easyui datagrid deleteRow(删除行)的BUG!

    转自:http://my.oschina.net/fants/blog/77189项目中又用到easyui 的datagrid做数据展示.功能很强大,很实用,但bug也很多.今天这个就够让人头疼. 如 ...

  5. QQ登录界面

    @property (nonatomic,assign) IBOutlet UITextField *qq; @property (nonatomic,assign) IBOutlet UITextF ...

  6. spice up your desktop

    https://wiki.gnome.org/Projects/GnomeShell/CheatSheet windows10用了一段时间,回来看着gnome3-shell是那么的恶心,翻来翻去重新整 ...

  7. .net网站报错:对象的当前状态使该操作无效

    微软在2011年12月29号发布的2011年最后一个更新让哥哥为程序出现的异常头痛了一天. 这个异常在页面数据量小的时候并不会触发,只在页面数据量大的情况下才会出现,开始解决起来让人无从下手,最后才发 ...

  8. Hibernate主键生成方式之hilo

    当利用Hibernate的getHibernateTemplate().save(obj);插入的对象的主键ID为null的时候自动生成5位数的主键ID进行插入. 此笔记的由来: 老夫在此处上传材料后 ...

  9. 后台运行之BackgroundWorker

    BackgroundWorker 类允许您在单独的专用线程上运行操作. 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 似乎处于停止响应状态. 如果您需要能进行响应的用户界 ...

  10. UIImage图片拉伸方法

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...