HZAU 1208 Color Circle (dfs)
来源:互联网 发布:淘宝记账软件手机版 编辑:程序博客网 时间:2024/06/04 19:59
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:
Every point is different.
k >= 4
All points belong to the same color.
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 3AAAABAAAA
Sample Output
Yes
题意
对于一张地图,判断能否找到一条路线,长度大于4,相同字母,并且回到原点。
思路
找图中的一个环,可以从某个点进入开始 Yes
,否则输出 No
。
时间复杂度:
AC 代码
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;#define MAXX 110000int n,m;int mapp[55][55];bool vis[55][55];int mv[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};bool flag;void dfs(int x,int y,int xa,int ya) // (x,y) 为当前点坐标, (xa,ya) 为它从哪个点来{ for(int i=0; i<4; i++) // 四个方向 { int xi=x+mv[i][0]; int yi=y+mv[i][1]; if(xi<0||xi>=n||yi<0||yi>=m||mapp[xi][yi]!=mapp[x][y])continue; // 要求搜索的点相同 if(!vis[xi][yi]) { vis[xi][yi]=true; dfs(xi,yi,x,y); if(flag)return; } else { if(xi==xa&&yi==ya)continue; // 忽略来的那一点,如果还遇到一个已经访问的点,则是一个环 flag=true; return; } }}void solve(){ for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(!vis[i][j]) { vis[i][j]=true; dfs(i,j,-1,-1); if(flag) { cout<<"Yes"<<endl; return; } } cout<<"No"<<endl;}int main(){ while(cin>>n>>m) { string c; memset(vis,false,sizeof(vis)); flag=false; for(int i=0; i<n; i++) { cin>>c; for(int j=0; j<m; j++) mapp[i][j]=c[j]; } solve(); } return 0;}
- HZAU 1208 Color Circle (dfs)
- HZAU 1208 Color Circle
- HZAU1208——Color Circle(dfs)
- Problem J: Color Circle----dfs
- Color Circle
- HZAU 1012 The Same Color
- HZAU 1203 One Stroke(dfs+二分 Or 双指针)
- Circle vs Triangle(DFS+博弈)
- HDOJ 题目3964 Find The Simple Circle(DFS)
- ACDREAM 05B Circle vs Triangle(DFS专场)
- HZAU oj 1015(LCS)
- HZAU 1209 Deadline (技巧)
- bzoj 1815: [Shoi2006]color 有色图 (置换+dfs)
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- HZAU校赛F题 LCS (dp)
- HZAU--21--Arithmetic Sequence(二维dp)
- HZAU--20--Catching Dogs(模拟)
- HZAU--19--Eat Candy(水题)
- Linux中用ALSA驱动声卡流程详解
- BZOJ 1304: [CQOI2009]叶子的染色 树形DP
- LCD驱动的添加
- 行人检测(上)-行人检测介绍
- iOS UILabel 设置 行间距 和 字间距 并计算高度
- HZAU 1208 Color Circle (dfs)
- Android组件化项目
- hexo+github搭建个人博客
- ARToolkit Part 2: Contour Detection 详解
- UICollectionView(一)——整体总结
- nVidia Tegra K1 LVDS配置
- leapmotion文档(2):虚拟现实中的手追踪
- 如何通过Html网页调用本地安卓app?
- TCP状态机