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

题意:找同一种颜色的环

这题麻烦的地方在于走过的路被标记了,那怎么判断有环呢?

其实记录步数就可以了,bfs和dfs道理是一样的

bfs做法:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node
{
int x,y;
};
bool v[][];
int book[][];
char a[][];
int d[][]={{-,},{,},{,-},{,}};
queue<node>q;
int main()
{
int n,m;
cin>>n>>m;
memset(v,,sizeof(v));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
bool f=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(!v[i][j])
{ memset(book,,sizeof(book));
v[i][j]=;
while(!q.empty()) q.pop();
node b;
b.x=i;
b.y=j;
book[i][j]=;
q.push(b);
while(!q.empty())
{
node b=q.front();
q.pop();
for(int k=;k<;k++)
{
int xx=b.x+d[k][];
int yy=b.y+d[k][];
if(xx<||yy<||xx>n||yy>m||a[xx][yy]!=a[i][j]) continue;
v[xx][yy]=;
node c;
c.x=xx;
c.y=yy;
if(book[xx][yy]>=book[b.x][b.y])//如果当前走过去格子有步数且的步数比当前这个格子还要大,说明已经走过了,形成了环。
{ f=;break;
}
if(book[xx][yy])continue;
q.push(c);
book[xx][yy]=book[b.x][b.y]+;//保存路的步数
}
if(f) break;
}
if(f) break;
}
if(f) break;
}
if(f) break;
}
if(f) cout<<"Yes";
else cout<<"No";
return ; }

dfs也是同样道理:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
char a[][];
bool book[][];
int v[][];
int z[];
int n,m;
int d[][]={{-,},{,},{,},{,-}};
bool f=;
int si,sj;
void dfs(char c,int x,int y)
{ for(int i=;i<;i++)
{
int xx=x+d[i][];
int yy=y+d[i][];
if(xx<||yy<||xx>n||yy>m) continue;
if(v[xx][yy]!=&&v[x][y]-v[xx][yy]>)//走过去的格子已经有值了且比现在走的格子还大不止1,大1可能是之前走过来的
{
f=;
return;
}
if(a[xx][yy]==c&&v[xx][yy]==)
{
v[xx][yy]=v[x][y]+;
book[xx][yy]=;
dfs(c,xx,yy);
if(f) return;
v[xx][yy]=; }
}
} int main()
{
scanf("%d %d",&n,&m);
memset(z,,sizeof(z));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(!book[i][j])
{
z[a[i][j]-'A']=;
memset(v,,sizeof(v));
book[i][j]=;
si=i;
sj=j;
v[i][j]=;
dfs(a[i][j],i,j);
if(f) break;
}
}
if(f) break;
}
if(f) cout<<"Yes";
else cout<<"No";
return ;
}

CodeForces - 510B Fox And Two Dots (bfs或dfs)的更多相关文章

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

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

  2. Codeforces 510B Fox And Two Dots 【DFS】

    好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...

  3. CF 510b Fox And Two Dots

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

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

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

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

  6. Fox And Two Dots

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

  7. B. Fox And Two Dots

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

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

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

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

随机推荐

  1. 斯坦福机器学习视频笔记 Week9 异常检测和高斯混合模型 Anomaly Detection

    异常检测,广泛用于欺诈检测(例如“此信用卡被盗?”). 给定大量的数据点,我们有时可能想要找出哪些与平均值有显着差异. 例如,在制造中,我们可能想要检测缺陷或异常. 我们展示了如何使用高斯分布来建模数 ...

  2. Docker 共享存储解决方案Rex-Ray

    github地址:https://github.com/rexray/rexray 安装: curl -sSL https://rexray.io/install | sh - 生成配置文件: htt ...

  3. java格式化输出 printf 例子

    import java.util.Date; public class Printf { public static void main(String[] args) { // %s表示输出字符串,也 ...

  4. MapReduce-从HBase读取数据处理后再写入HBase

    MapReduce-从HBase读取处理后再写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import o ...

  5. BZOJ2878 [Noi2012]迷失游乐园

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. ajax设置Access-Control-Allow-Origin实现跨域访问

    ajax跨域访问 1.jsonp方法,jsonp方法是一种非官方方法,这种方法只支持GET方式, 不如POST方式安全.(即使使用jquery的jsonp方法,type设为POST, 也会自动变为GE ...

  7. JMeter设置Http代理对web或者app进行录制

    一.录制web 1.首先保证JMeter的安装环境都正确.启动JMeter:在安装路径的bin目录下双击jmeter.bat (例如:D:\apache-jmeter-2.13\bin) ​2.打开J ...

  8. node express框架基本配置

    node express框架基本配置 初始化项目 express -e 安装依赖包 npm install 安装第三方包 npm install xxx --save-dev dos 运行node a ...

  9. 智课雅思词汇---十七、前綴il-, in-, ir-, im-有什麼關係

    智课雅思词汇---十七.前綴il-, in-, ir-, im-有什麼關係 一.总结 一句话总结:这几个长得非常像,并且意思也非常像 1.前綴il-, in-, ir-, im-是什麼意思? 前缀:i ...

  10. dp3--codevs2598 编辑距离问题

    dp3--codevs2598 编辑距离问题 一.心得 1.字符串相关问题dp的时候从0开始是个陷阱 二.题目 2598 编辑距离问题  时间限制: 1 s  空间限制: 128000 KB  题目等 ...