hzau 1208 Color Circle(dfs)
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)的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- MySQL中事务的概述ACID了解
事务可由一条非常简单的SQL语句组成,也可以有一组复杂的SQL语句组成.事务是访问并更新数据库中各种数据项的一个程序执行单元.在事务中操作,要么都做修改,要么都不做,这就是事务的目的,也是事务模型区别 ...
- phonegap file api
https://github.com/chrisben/imgcache.js/tree/master/examples 1.FILE API file api最大的两个功能是download和upl ...
- Protobuf支持 pointf
Protobuf支持 pointf序列化 加入:ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(System.Drawing.PointF), fa ...
- [笔记]如何将传统的回调函数转换为C#5.0支持的await格式
C#5.0引入了编译器支持的 async 和 await 关键字,这就为开发者提供了使用同步思想写异步代码的方便. 但是有些传统函数仅提供了异步回调实现,如何对其封装,使其可以享受await的便利呢? ...
- python的PIL模块安装
一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...
- time函数计算时间
学过C语言的都知道有个time函数可以计算时间, 也好像知道time(NULL)返回的是一个距离1970年1月1日0时0分0秒的秒数. #include <stdio.h> #includ ...
- linux mint —— 图片一张
概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...
- linux常用技巧(资料)
Linux中查看程序安装位置 如果是rpm的安装,用rpm -ql如果是一般安装 用 whereis 或者 find find /usr -name catalina.out======== 如何查看 ...
- Pro*C基础
SQL变量的申明: EXEC SQL BEGIN DECLARE SECTION; 类型 变量名[长度] varchar2 serv_number[]; 其中可以定义C变量 EXEC SQL END ...
- android驱动USB摄像头
http://blog.csdn.net/mirkerson/article/details/50764314 多亏了stackoverflow看到的一篇帖子,其中有几句关键的话,然后顺藤摸瓜解决了问 ...