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 ...
随机推荐
- KMP 算法简单解释
讲KMP算法,离不开BF,实际上,KMP就是BF升级版,主要流程和BF一样 不同是在匹配失败时能利用子串的特征减少回溯,利用根据子串特征生成的Next数组来减少 <( ̄︶ ̄)↗[GO!] ...
- DNS提供的服务
DNS提供的服务 DNS是:1.一个由分层的DNS服务器实现的分布式数据库:2. 一个使主机能够查询分布式数据库的应用协议.DNS协议运行在UDP上,使用53号端口. 与http,FTP,SMTP协议 ...
- Codeforces Round #567 (Div. 2) B. Split a Number
Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- javaAPI操作ES分组聚合
连接es的客户端使用的 TransportClient SearchRequestBuilder requestBuilder = transportClient.prepareSearch(indi ...
- Mysql失败,异常pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9D\\x90\\xBF;......
问题描述: 插入Mysql时失败了,python代码报如下异常: pymysql.err.InternalError: (1366, "Incorrect string value: '\\ ...
- 面试刷题22:CAS和AQS是什么?
java并发包提供的同步工具和线程池,底层是基于什么原理来设计和实现的呢?这个非常重要. 我是李福春,我在准备面试,今天的题目是: CAS和AQS是什么? 答:CAS是一系列的操作集合,获取当前值进行 ...
- 如何设置mysql远程访问
如何设置mysql远程访问 Mysql默认是不可以通过远程机器访问的,通过下面的配置可以开启远程访问 在MySQL Server端: 执行mysql 命令进入mysql 命令模式, mysql> ...
- 洛谷5026 Lycanthropy 差分套差分
题目链接 https://www.luogu.com.cn/problem/P5026 题意 在一个长度为m的序列中,每次给一个下标x,和一个权值v,然后从x-v*3到x-v*2单调递增,从x-v*2 ...
- Springboot 事务注解--- @Transactional
spring boot @Transactional事物处理 spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...
- 利用sqlmap进行Access和Mysql注入
sqlmap将检测结果保存到C:\Users\Administrator.sqlmap\output (windows) linux:(/root/.sqlmap/output) Access注入 1 ...