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. MySQL中备份的几种方式

    前言: 并不是每家公司都高大上,并不是每家公司都会用一些很前沿的技术来做备份这一块,有些企业或者有些行业或者团队本身由于各方面的原因使用简单或者复杂的方式来做备份这块,这次这个文档算是对以前工作的总结 ...

  2. 002-JVM运行时数据区【内存模型】

    一.概述 JVM定义了不同运行时数据区,他们是用来执行应用程序的.某些区域随着JVM启动及销毁,另外一些区域的数据是线程性独立的,随着线程创建和销毁. 1.1.jvm自身物理结构 1.2.java内存 ...

  3. phonegap file api

    https://github.com/chrisben/imgcache.js/tree/master/examples 1.FILE API file api最大的两个功能是download和upl ...

  4. 基于CentOS的SSHD服务的Docker镜像

    原文地址 1.Dockerfile文件 FROM registry.aliyuncs.com/acs-sample/centos:6 MAINTAINER xuqh "xqh_163@163 ...

  5. [转+整理]linux shell 将字符串分割成数组

    原文链接:http://1985wanggang.blog.163.com/blog/static/776383320121745626320/ a="one,two,three,four& ...

  6. 流量监控系统---storm集群配置

    1.集群部署的基本流程 集群部署的流程:下载安装包.解压安装包.修改配置文件.分发安装包.启动集群 注意: 所有的集群上都需要配置hosts vi  /etc/hosts 192.168.223.20 ...

  7. $Java正则表达式基础整理

    (一)正则表达式及语法简介 String类使用正则表达式的几个方法: 正则表达式支持的合法字符: 特殊字符: 预定义字符: 方括号表达式: 圆括号表达式:用于将多个表达式组成一个子表达式,可以使用或运 ...

  8. SpringBoot Redis工具类封装

    1.接口类 package com.sze.redis.util; import java.util.List; import java.util.Set; import java.util.conc ...

  9. 【Flask】sqlalchemy 排序

    ### 排序:1. order_by:可以指定根据这个表中的某个字段进行排序,如果在前面加了一个-,代表的是降序排序.2. 在模型定义的时候指定默认排序:有些时候,不想每次在查询的时候都指定排序的方式 ...

  10. 理解Android Build系统【转】

    本文转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/ Android Build 系统是用来编译 And ...