B. Fox And Two Dots

题目连接:

http://codeforces.com/contest/510/problem/B

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:

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

Sample Output

Yes

Hint

题意

给你一个n*m的网格

然后问你是否有只含有一种元素的环

题解:

dfs就好了

dfs的时候,记录一下fa,然后一直跑下去,跑到曾经vis过的地方,就说明遇到了环

代码

#include<bits/stdc++.h>
using namespace std; char mp[55][55];
int vis[55][55];
int flag = 0;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
void dfs(int x,int y,char c,int fax,int fay)
{
vis[x][y]=1;
if(flag)return;
for(int i=0;i<4;i++)
{
int xx = x+dx[i];
int yy = y+dy[i];
if(xx==fax&&yy==fay)continue;
if(xx<0||xx>=n)continue;
if(yy<0||yy>=m)continue;
if(mp[xx][yy]!=c)continue;
if(vis[xx][yy]){
flag=1;
return;
}
dfs(xx,yy,c,x,y);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",&mp[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!vis[i][j])
dfs(i,j,mp[i][j],i,j);
}
if(flag)printf("Yes");
else printf("No\n");
}

Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs的更多相关文章

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

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

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

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  3. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  4. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  5. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  6. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  8. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  9. CodeForces Round #290 Div.2

    A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...

随机推荐

  1. -Xbootclasspath参数、java -jar参数运行应用时classpath的设置方法

    当用java -jar yourJarExe.jar来运行一个经过打包的应用程序的时候,你会发现如何设置-classpath参数应用程序都找不到相应的第三方类,报ClassNotFound错误.实际上 ...

  2. 【C++】统计代码覆盖率(二)

    嗷嗷嗷!!!好激动,我好蠢.不过最后还是解决了.呜呜呜 有些都是东一块西一块查的,如果有侵权欢迎私信我,我注明出处. 一 gcov&CMake 昨天试了下测试代码和被测代码都是c++的情况,直 ...

  3. IntelliJ IDEA 13 Keygen

    import java.math.BigInteger; import java.util.Date; import java.util.Random; import java.util.zip.CR ...

  4. MapReduce 中job.setJarByClass()方法的疑惑

    在调试mr实例的时候,遇到如下的情况,如图所示 说明:就是我的mr程序类名称和我设置的setJarByclass()中设置的不一样,但是程序竟然没有报错!!!!当时把我吓尿了 疑惑:如果这样设置的话, ...

  5. windows下跑python flask,环境配置

    首先声明一下,我安装的是python 2.7. 第一步:下载easy_setup.py 下载地址:https://pypi.python.org/pypi/setuptools 这个下载地址真心难找, ...

  6. jetty8的多实例部署(LT项目开发参考)

    LT项目使用的EIP是运行在JETTY上,此文供开发和实施参考 1.windows下 win下部署多个jetty8很简单,首先将jetty8复制多个文件夹,其次按分配的端口号修改[JETTY_HOME ...

  7. PHP获取本周开始时间

    /*先设置时区*/date_default_timezone_set('PRC');/*网上的写法:总觉得这周跨年或者跨月的时候会悲剧 未验证*/echo mktime(0,0,0,date('m') ...

  8. JavaIO之RandomAccessFile随机访问文件

    package test.java.io; import java.io.RandomAccessFile; public class RandomAccFile { public static vo ...

  9. Warning: $HADOOP_HOME is deprecated.解决方法

    方式1(不推荐):注释hadoop-config.sh中的 if [ "$HADOOP_HOME_WARN_SUPPRESS" = "" ] && ...

  10. 开始使用Ambari吧

    最开始接触Hadoop是研究生入学后,帮师姐装装集群什么的.过程很繁琐,很重复,很是让人抓狂.当时装一个三台机器的集群需要两天左右,这还是装的很熟练的时间花费,刚入手的时候简直是惨不忍睹,三台机器装了 ...