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

Examples
input

Copy
3 4
AAAA
ABCA
AAAA
output

Copy
Yes
input

Copy
3 4
AAAA
ABCA
AADA
output

Copy
No
input

Copy
4 4
YYYR
BYBY
BBBY
BBBY
output

Copy
Yes
input

Copy
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
output

Copy
Yes
input

Copy
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
output

Copy
No
Note

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

【题意】

给定n*m矩阵,看是否有相同的字符可以构成一个环

【分析】

爆搜~
注意:1、构成环至少需要4个字符
    2、注意判断字符的来路


【代码】

 

#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=105;
int n,m,ans,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char mp[N][N];bool vis[N][N]={0};
void dfs(int x,int y,int px,int py,int step){
vis[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx<1||ny<1||nx>n||ny>m||mp[nx][ny]!=mp[x][y]) continue;
if(!vis[nx][ny]) dfs(nx,ny,x,y,step+1);
else{
if((nx!=px||ny!=py)&&step>=4){puts("Yes");exit(0);}
}
}
}
inline void Init(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
}
inline void Solve(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!vis[i][j]){
dfs(i,j,0,0,1);
}
}
}
puts("No");
}
int main(){
Init();
Solve();
return 0;
}
 

 

 

CF510B Fox And Two Dots(搜索图形环)的更多相关文章

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

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

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

  4. B. Fox And Two Dots

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

  5. Fox And Two Dots

    B - Fox And Two Dots Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

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

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

  7. D - Fox And Two Dots DFS

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

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

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

  9. CF 510b Fox And Two Dots

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

随机推荐

  1. Docker镜像相关

    一.中间镜像 通过持续集成工具Jenkins构建Docker镜像并运行容器,采用的是Docker Compose来进行编排构建运行的.但是每次构建完毕以后通过docker images命令查询,可以发 ...

  2. mysql使用sql语句查询数据库所有表注释已经表字段注释

    场景: 1. 要查询数据库 "mammothcode" 下所有表名以及表注释 /* 查询数据库 ‘mammothcode’ 所有表注释 */ SELECT TABLE_NAME,T ...

  3. asp.net mvc maproute定义可变数量的自定义片断变量

    有时候我们定义了如{controller}/{action}/{id}之类的路由规则,但是后面还可能跟上一堆可能会有可能不会有,但是路由规则是一样的,如{controller}/{action}/{i ...

  4. jquery方法.serializeArray()获取name和value并转为json数组

    jquery的.serializeArray()方法可以获取形如以下 [ {name: 'firstname', value: 'Hello'}, {name: 'lastname', value: ...

  5. python-类的方法与类的成员

    preface include: @classmethod @staticmethod @property 私有属性 类的成员 #!/usr/bin/env python class animal(o ...

  6. ambari hdp 集成 impala

    1.下载ambari-impala-service VERSION=`hdp-select status hadoop-client | sed 's/hadoop-client - \([0-9]\ ...

  7. C# dmp debug, can't load pdb file

    1.  Project->Properties->Build->Advance, Debug Info : Full/pdb-only . set to [none] will no ...

  8. Nginx 反向代理的正确配置

    server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; l ...

  9. 复习js

    js写在页面最后如果放在前面,需要加window.onload=function(){)常见的两种输出方式 在网页中弹出显示框,显示信息 alert() 在控制台输出消息,一般用来调试程序consol ...

  10. MTK 预置apk

    一.如何将带源码的APK预置进系统? 1)     在 packages/apps 下面以需要预置的 APK的 名字创建一个新文件夹,以预置一个名为Test的APK 为例 2)     将 Test ...