HZAU1208——Color Circle(dfs)

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

求图中是否有一个环路,其中的字母都相同
爆搜

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#include <cctype>
#define INF 0x3f3f3f3f
#define MAXN 1000005
#define Mod 1000000007
using namespace std;
int n,m;
char mp[55][55];
int vis[55][55];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool dfs(int x,int y,char tag,int step,int prex,int prey)
{
    int tx,ty;
    vis[x][y]=1;
    if(step>=4)
    {
        for(int i=0;i<4;++i)
        {
            tx=x+dx[i];
            ty=y+dy[i];
            if(tx<1||tx>n||ty<1||ty>m)
                continue;
            if((tx!=prex||ty!=prey)&&mp[tx][ty]==tag&&vis[tx][ty])
                return true;
        }
    }
    for(int i=0;i<4;++i)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        if(tx<1||tx>n||ty<1||ty>m)
            continue;
        if((tx!=prex||ty!=prey)&&mp[tx][ty]==tag&&!vis[tx][ty])
        {
            vis[tx][ty]=1;
            if(dfs(tx,ty,tag,step+1,x,y))
                return true;
        }
    }
    return false;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;++i)
            scanf("%s",mp[i]+1);
        memset(vis,0,sizeof(vis));
        int flag=0;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
            {
                if(!vis[i][j])
                {
                    flag=dfs(i,j,mp[i][j],1,i,j);
                    if(flag)
                        break;
                }
            }
            if(flag)
                break;
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值