D - Fox And Two Dots DFS
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.
Examples
3 4
AAAA
ABCA
AAAA
Yes
3 4
AAAA
ABCA
AADA
No
4 4
YYYR
BYBY
BBBY
BBBY
Yes
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
Yes
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
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).
题目大意 :判断图中是否有些相同字母组成的环,如果有的话直接出书YES,没有输出NO
思路 : 图中的每个点都有可能构成循环,所以要逐一遍历,如果该点可以构成循环的环 那么起点是该点,,终点也是该点,环至少是4个点构成,所以如果有环的话再次回到起点时步数一定大于等于四。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,end_i,end_j;
char aa;
char arr[][];
int mark[][]={};
int d[][]={{,},{-,},{,},{,-}};
int flag=;
void dfs(int x,int y,int step)
{
if(flag==) return;
if(step>=&&x==end_i&&y==end_j)如果回到了起点且步数大于等于4 则找到环了。
{
flag=;
return;
}
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]==aa){
if(dx==end_i&&y==end_j&&step<)//起点就是终点所以起点没有被标记,当进入到第二个点时,可能会回到起点,所以要对步数进行判断,看是否大于等于3
continue;
mark[dx][dy]=;
dfs(dx,dy,step+);
}
}
}
int main()
{
cin>>n>>m;//n行m列
for(int i=;i<n;i++) scanf("%s",&arr[i]);//存图 for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
memset(mark,,sizeof(mark));
end_i=i;
end_j=j;//起点与终点
aa=arr[i][j];//搜索的字符
dfs(i,j,);
if(flag==)
break;
}
if(flag==) break;
}
if(flag==) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return ;
}
D - Fox And Two Dots DFS的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- B. Fox And Two Dots
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Fox And Two Dots
B - Fox And Two Dots Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- Codeforces 510B Fox And Two Dots 【DFS】
好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...
随机推荐
- Swagger2在DBA Service中生成RESTful API的实践
目的与背景: 目的:对外暴露DBA Service必要的RESTful API,形成规整的API文档 背景:DBA Service后端采用Spring-boot,属于Spring家族,故生成API的工 ...
- C#获取设备话筒主峰值(实时音频输出分贝量)
1.引用类库NAudio,Git地址 https://github.com/naudio/NAudio 2.添加如下代码和引用: public float GetVoicePeakValue() { ...
- OpenCV-Python 对极几何 | 五十一
目标 在本节中 我们将学习多视图几何的基础知识 我们将了解什么是极点,极线,极线约束等. 基础概念 当我们使用针孔相机拍摄图像时,我们失去了重要信息,即图像深度. 或者图像中的每个点距相机多远,因为它 ...
- 3分钟学会简单使用Vim
Vim是一款运行在命令行里的文字编辑器,它是Linux人员的标配.在Windows环境下也可以有特别的用处,比如创建没有文件名的文件(.gitignore). Vim的功能十分强大,以至于有一些人对它 ...
- ArcGIS Desktop的安装
1.双击ArcGIS Desktop安装目录下的Setup.exe. 2.点击“下一步”. 3.选择“我接受许可协议(A)”,点击“下一步”. 4.选择“完全安装”,点击“下一步”. 5.点击“更改” ...
- coding++ :Layui-form 表单模块
虽然对layui比较熟悉了,但是今天有时间还是将layui的form表单模块重新看一下. https://www.layui.com/doc/modules/form.html 1):更新渲染 lay ...
- Dubbo与Spring Cloud的比较
区别: ----- 来源(背景): Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点. Spring Cloud,从命名我们就可以知道,它是Spring Source ...
- sql select sql查询
select 一.课上练习代码 1 查询所有学生信息 select * from tb_student; select * from tb_teacher; 2 查询所有课程名称及学分(投影和别名) ...
- @RequestBody和@RequestParam的使用详解
此次分享转载至:https://blog.csdn.net/justry_deng/article/details/80972817 这边文章讲解的比较透彻,主要是在springboot项目中进行使用 ...
- C 部落划分
时间限制 : - MS 空间限制 : - KB SPJ 评测说明 : 1s,128m 问题描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人 ...