1208: Color Circle

Time Limit: 1 Sec  Memory Limit: 1280 MB
Submit: 289  Solved: 85
[Submit][Status][Web Board]

Description

There are colorful flowers in the parterre in front of the door of college and form many beautiful patterns. Now, you want to find a circle consist of flowers with same color. What should be done ?

Assuming the flowers arranged as matrix in parterre, indicated by a N*M matrix. Every point in the matrix indicates the color of a flower. We use the same uppercase letter to represent the same kind of color. We think a sequence of points d1, d2, … dk makes up a circle while:

1. Every point is different.

2. k >= 4

3. All points belong to the same color.

4. For 1 <= i <= k-1, di is adjacent to di+1 and dk is adjacent to d1. ( Point x is adjacent to Point y while they have the common edge).

N, M <= 50. Judge if there is a circle in the given matrix.

Input

There are multiply test cases.

In each case, the first line are two integers n and m, the 2nd ~ n+1th lines is the given n*m matrix. Input m characters in per line.

Output

Output your answer as “Yes” or ”No” in one line for each case.

Sample Input

3 3
AAA
ABA
AAA

Sample Output

Yes

HINT

dfs

 #include <bits/stdc++.h>
using namespace std; const int MAXN = ; int dir[][] = {{-, }, {, }, {, -}, {, },};
char g[MAXN][MAXN];
int depth[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m;
char bg;
bool circle; void init()
{
memset(g, '\0', sizeof(g));
memset(depth, , sizeof(depth));
memset(vis, false, sizeof(vis));
circle = false;
} bool check(int r, int c)
{
if (r < || r >= n) {
return false;
}
if (c < || c >= m) {
return false;
}
return true;
} void dfs(int r, int c, int d)
{
if (circle) {
return;
}
int i;
int r2, c2;
for (i = ; i < ; ++i) {
r2 = r + dir[i][];
c2 = c + dir[i][];
if (!check(r2, c2)) {//越界
continue;
}
if (g[r][c] != bg) {//不同
continue;
}
if (!vis[r2][c2]) {//没访问过
vis[r2][c2] = true;
depth[r2][c2] = d + ;
dfs(r2, c2, d + );
depth[r2][c2] = ;
vis[r2][c2] = false;
} else if (d - depth[r2][c2] + >= ) {//找到环
circle = true;
return;
}
}
} int main()
{
int i, j; while (~scanf("%d%d", &n, &m)) {
//init();
for (i = ; i < n; ++i) {
scanf("%s", g[i]);
}
circle = false;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
bg = g[i][j];
vis[i][j] = true;
depth[i][j] = ;
dfs(i, j, );
depth[i][j] = ;
vis[i][j] = false;
if (circle) {
break;
}
}
if (circle) {
break;
}
}
if (circle) {
printf("Yes\n");
} else {
printf("No\n");
}
} return ;
}

hzau 1208 Color Circle(dfs)的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)

    Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  9. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. Android系统移植与调试之------->如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏

    这两天由于一个客户的要求,将MID竖屏时候的状态条上的音量键去掉.所以尝试修改了一下,成功了,分享一下经验. 先看一下修改后的效果图,如下所示 . 横屏的时候:有音量加减键 竖屏的时候:音量加减键被去 ...

  2. Java输入输出重定向代码

    try {   BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt")) ...

  3. SQL2008 R2直接恢复 mdf后缀数据文件

    数据库默认存储地址为   C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA  ,那么我首先把朋友给的,md ...

  4. Slyce,这家硅谷创业公司的来头你知道吗

    Slyce,也许你没听过,一家硅谷创业公司,旨在帮助运动员和其他社会名流组织.优化社交媒体,过滤粉丝的声音,让明星更好的在社交媒体上和他们互动.但是如果如果说库里,那你应该就知道了,拿到了上届NBA总 ...

  5. 检测tomcat服务是否正常

    由于tomcat服务经常会出现进程在,但是服务却无法正常响应的问题,而且进程跑在docker容器中,使用zabbix控制不是很方便,故此写了个简单的小脚本: #!/bin/bash #Author:f ...

  6. mysql数据库补充知识6 完整性约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  7. Overload and Override without Overwrite - Java

    Override(覆盖/覆写): 子类Override父类中的函数(方法).Overload(重载): 同一个类中包含多个同名的函数(方法), 但各个函数的参数列表不同. Override和Overl ...

  8. css的继承性理解

    1) 所有的text 相关属性都被继承: 如 font-family font-size; font-style;font-weight;font;font-variant;letter-spacin ...

  9. UnsatisfiedLinkError X.so is 64-bit instead of 32-bit之Android 64 bit SO加载机制

    http://blog.csdn.net/canney_chen/article/details/50633982 今天用户反馈应用闪退崩溃了.然后找呀找… 过程原来是这样的: 还是说下项目背景 应用 ...

  10. 通过加载Xib文件来创建UITableViewCell造成复用数据混乱问题方案

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...