您的位置:首页 > 其它

华中农业大学第五届程序设计大赛 J Color Circle

2017-05-26 22:41 330 查看
题目连接:http://acm.hzau.edu.cn/problem.php?id=1208

——————————————————————

1208: Color Circle

Time Limit: 1 Sec Memory Limit: 1280 MB

Submit: 344 Solved: 104

[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:

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 3

AAA

ABA

AAA

Sample Output

Yes

————————————————————————————

题目大意:

问你能不能找到一个由一种颜色构成的圈

有求,

1.至少有4个点

2.所有点首尾相接构成一个圈

3.颜色相同

4.点是不同的

解题思路:

由于图很小,所以暴力搜就好了,

附本题代码

——————————————————————————

#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N   = 2e5+7;
//const int INF = (~(1<<31));

int read(){
int x=0,f=1;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch = getchar();}
return x;
}
/*******************************************/

char a[111][111];
int vis[111][111];
int n,m;
bool flag,flag2;

int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};

void dfs(int x,int y,int dep){
if(flag) return ;
int xx,yy;
for(int i=0;i<4;i++){
xx=x+fx[i];
yy=y+fy[i];
if(xx<1||xx>n||yy<1||yy>m)  continue;
if(a[xx][yy]!=a[x][y]) continue;

if(vis[xx][yy]&&dep-vis[xx][yy]+1>=4) {flag = true;return ;}
else if(!vis[xx][yy]) {
vis[xx][yy]=dep;
dfs(xx,yy,dep+1);
vis[xx][yy]=0;
}
}
}

int main(){
while(~scanf("%d%d",&n,&m)){memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)     scanf("%s",a[i]+1);
flag = flag2 = false;
for(int i=1;i<=n&&!flag2;i++)
for(int j=1;j<=m&&!flag2;j++){
dfs(i,j,1);
if(flag) flag2=true;
}

if(flag2) puts("Yes");
else      puts("No");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐