Fox And Two Dots
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Sample Input
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
Hint
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,听房神说还有很简单的方法,明天继续看一下。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char maze[][];
int vis[][];
int n,m;
int flag = ;
int to[][] = {{,},{-,},{,},{,-}};
bool check(int x,int y){
if(x<||x>=n||y<||y>=m) return false;
//if(vis[x][y]) return false;
return true;
}
void dfs(int x,int y,int prex,int prey){
if(!check(x,y)) return;
vis[x][y] = ;
int postx,posty;
for(int i = ; i<; i++){
postx = x + to[i][];
posty = y + to[i][];
if(check(postx,posty)&&maze[postx][posty] == maze[x][y]&&(postx!=prex||posty!=prey)){
if(vis[postx][posty]){
flag = ;
return;
}
dfs(postx,posty,x,y);
}
}
}
void input(){ scanf("%d%d",&n,&m);
for(int i = ; i<n; i++) scanf("%s",maze[i]);
for(int i = ; i<n; i++){
for(int j = ; j<m; j++){
if(!vis[i][j]){
dfs(i,j,-,-);
}
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
int main()
{
input();
return ;
}
卷珠帘
Fox And Two Dots的更多相关文章
- 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 ...
- B. Fox And Two Dots
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ================================================================= ...
- CF 510b Fox And Two Dots
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- D - Fox And Two Dots DFS
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
随机推荐
- Adobe Flash CC 2014 下载及破解
来源 :http://prodesigntools.com/adobe-cc-2014-direct-download-links.html 地址:http://trials3.adobe.com/A ...
- MVC 项目中又一新方法实现依懒注入 (AutoFac)
详情请查看:http://docs.autofac.org/en/latest/integration/mvc.html#quick-start
- Ansible3:ansible.cfg配置说明【转】
Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码.是否开启sudo认证.actio ...
- JSP基本语法--实例演练
基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...
- iptables查看、添加、删除规则
1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...
- zookeeper集群实例
zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...
- pci 相关资料
1.http://www.cnblogs.com/image-eye/archive/2012/02/15/2352699.html
- sinaBlog中小知识总结
1.上拉刷新的时候记得移除老的数据(同样应用于其他地方) if (vc.currentPage == 1) { //上拉加载 记得移除新的 [vc.totalArr removeAllObjects] ...
- Android OpenGL ES(十一)绘制一个20面体 .
前面介绍了OpenGL ES所有能够绘制的基本图形,点,线段和三角形.其它所有复杂的2D或3D图形都是由这些基本图形构成. 本例介绍如何使用三角形构造一个正20面体.一个正20面体,有12个顶点,20 ...
- iOS 判断数组不为空
if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0)