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. 3.1 使用STC89C52控制MC20拨打电话

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. RedisClient For .Net

    Redis Client For .Net 介绍 redis支持各种语言版本的client,其中.net平台下比较火的有ServiceStack.Redis和StackExchange.Redis 选 ...

  3. django路由系统之反向生成url

    from niubin.service import v1 from django.urls import reverse from django.shortcuts import HttpRespo ...

  4. input date 对 placeholder 的支持问题

    正常情况下,text 的 input 会显示 placeholder 中的值,date 类型的 input 对其支持不好.实例代码如下: <input type="text" ...

  5. 前端 初级篇(CSS)

    CSS 简介 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:styl ...

  6. Python 模块续 configparser、shutil、XML、paramiko、系统命令、

    一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...

  7. php的异常和处理

    常见错误处理类型 语法错误 环境错误 逻辑错误 常见错误级别 Deprecated 最低级别的错误 不推荐,不建议,使用一些过期函数的时候会出现,程序继续执行 Notice 通知级别的错误 使用一些未 ...

  8. php 跨域 form提交 2种方法

    出于安全因素考虑,直接跨域访问是不允许的,下面介绍二种跨域的方法. 一,通过php curl function curlPost($url,$params) { $postData = ''; for ...

  9. [原创]spring及springmvc精简版--IOC

    本篇博客为自己学习spring和springmvc的一个总结.主要以代码为主,至于文字性描述理解性东西,可以自行百度.有认识不妥的地方,还望指出,相互学习. 以前很困惑spring中的一些概念,在学习 ...

  10. ubuntu里设置从串口登录

    1) Create a file called /etc/init/ttyS0.conf containing the following: # ttySAC0 - getty # # This se ...