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. 'AndroidManifest.xml' must have a minimum of 2 segments.

    Manifest下必须有二级域名 <manifest xmlns:android="http://schemas.android.com/apk/res/android" p ...

  2. dpkg: error processing package libraspberrypi-doc (--configure): package is in a very bad inconsistent state;

    dpkg: error processing package libraspberrypi-doc (--configure): package is in a very bad inconsiste ...

  3. js sendBeacon

    页面性能日志: DNS解析耗时 TCP链接耗时 SSL安全链接耗时 网络请求耗时 DOM解析耗时 资源加载耗时 首包时间 白屏时间 首次可交换时间 Dom Ready时间 页面完全加载时间. 如某些统 ...

  4. SqlServer2008基础知识:安全与权限

    分享自 儒雅的男人blog http://www.cnblogs.com/yushaoye201314/archive/2013/04/19/3031203.html 好文,转载收藏 这两天在调用Mi ...

  5. 用Python写简单的爬虫

    准备: 1.扒网页,根据URL来获取网页信息 import urllib.parse import urllib.request response = urllib.request.urlopen(& ...

  6. SQL备份一张表的数据

    使用如下语句 SELECT  *  into   table_bak   FROM   table 可把表table中的数据备份到  table_bak   ,而且无需重新建table_bak .会自 ...

  7. tooltips插件

    摘要: 继‘带箭头提示框’,本文将分享几款带箭头提示框. qtipqTip是一种先进的提示插件,基于jQuery框架.以用户友好,而且功能丰富,qTip为您提供不一般的功能,如圆角和语音气泡提示,并且 ...

  8. Laravel查询构造器简介

    数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...

  9. Java位运算加密

    创建一个类,通过位运算中的”^"异或运算符把字符串与一个指定的值进行异或运算,从而改变字符串每个字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,再与那个指定的 ...

  10. {"errorCode":50} 的解决办法

    # 无反爬 import urllib.parse import urllib.request import json content = input('请输入需要翻译的词语:') # url = ' ...