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 ;
}
最新文章
- java学习:Hibernate入门
- avalon2学习教程13组件使用
- java面试欠缺知识点总结
- POJ1459Power Network(dinic模板)
- LAMP之安装mysql/apache/php
- 【Nginx 3】FTP远程文件下载
- makefile中的target到底代表什么?
- Crosswalk入门
- JS实现一键复制功能
- Altium Designer 从导入DXF文件,并转换成板框
- Win8 系统下串口出现叹号 异常(10)
- Spring 系列: Spring 框架简介(转载)
- DDD实践2
- 前后端分离跨服务器文件上传-Java SpringMVC版
- 5)C语言函数(C自考学习)
- es随想一
- 微信小程序组件学习中
- Q - N! HDU - 1042
- Java代码走查具体考察点
- Spring 集成 Swagger UI
热门文章
- 标准c字符和字符串的使用方法
- MySQL数据库(7)_MySQL 数据备份与还原
- requirejs源码分析: requirejs 方法&ndash;2. context.require(deps, callback, errback);
- LeetCode:搜索旋转排序数组【33】
- windows下查看静态库和动态库的导出函数
- vagrant搭建
- HTML5(。。。。不完整)
- I.mx6s上移植wm8960驱动(基于linux3.0.101版本)
- MySQL运维问题集锦
- 主攻ASP.NET MVC4.0之重生:Jquery Mobile 面板