B. Fox And Two Dots
2 seconds
256 megabytes
standard input
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:
- 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.
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 "Yes" if there exists a cycle, and "No" otherwise.
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
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).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
int n, m, cnt; char cell[MS][MS];
int vis[MS][MS];
int dir[][] = { , , , , , -, -, }; bool dfs(char c, int sx, int sy, int x, int y)
{
if (sx == x&&sy == y&&vis[sx][sy])
{
return cnt >= ? true : false;
}
vis[x][y] = ;
for (int i = ; i < ; i++)
{
int tx = x + dir[i][];
int ty = y + dir[i][];
if (tx >= && tx < n&&ty >= && ty < m&&cell[tx][ty] == c&&vis[tx][ty] == || (tx == sx&&ty == sy))
{
cnt++;
if (dfs(c, sx, sy, tx, ty))
return true;
cnt--;
}
}
return false;
} bool solve()
{
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
memset(vis, , sizeof(vis));
cnt = ;
if (dfs(cell[i][j], i, j, i, j))
return true;
}
}
return false;
}
int main()
{
cin >> n >> m;
for (int i = ; i < n; i++)
cin >> cell[i];
if (solve())
cout << "Yes" << endl;
else
cout << "No" << endl;
return ;
}
B. 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 ...
- 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 ...
- 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 ...
- 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 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
随机推荐
- bzoj 3218 a + b Problem(最小割+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...
- bookhub -- 扁平化本地电子书管理与分享工具
代码 github 地址:https://github.com/JackonYang/bookhub 初稿: 1. 关键功能点 扫描本地电子书(扩展名 pdf/epub 等),将不重复的复制到特 ...
- 重新起步 iOS 开发
25Dec2013 Stanford iOS 公开课看到第三课 Programing in Objective-C 2.0 看完了第一部分,基本是以半小时一章的速度浏览的
- JavaFX 2 Dialogs
http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...
- CSS选择器的特殊性
在我们为元素添加样式的时候,或多或少会出现一个元素会有几个不同规则的样式.有#id的,有.class,直接标签元素的,还有各种组合起来的选择器.那CSS到底如何解决这些冲突呢,我们这次专门来探讨一下. ...
- Hibernate检索策略
1. Hibernate的检索策略概述: 检索数据时的 2 个问题: 1.不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象 ...
- POJ1328Radar Installation(贪心)
对于每一个点,可以找到他在x轴上的可行区域,这样的话就变为了对区间的贪心. #include<iostream> #include<stdio.h> #include<s ...
- 定义文档兼容性,让IE按指定的版本解析我们的页面
作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺 ...
- SQL存储过程调试
转自:http://www.cnblogs.com/xiangzhong/archive/2012/10/27/2742974.html 今天突然有同事问起,如何在sqlserver中调试存储过程(我 ...
- thttpd的定时器
运用了static函数实现文件封装 提升变量访问效率的关键字register,该关键字暗示该变量可能被频繁访问,如果可能,请将值存放在寄存器中 内存集中管理,每个节点在取消后并没有立即释放内存,而是调 ...