zoukankan      html  css  js  c++  java
  • D

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:

    Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.

    The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:

    1. These k dots are different: if i ≠ j then di is different from dj.
    2. k is at least 4.
    3. All dots belong to the same color.
    4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.

    Determine if there exists a cycle on the field.

    Input

    The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.

    Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

    Output

    Output "Yes" if there exists a cycle, and "No" otherwise.

    Examples

    Input
    3 4
    AAAA
    ABCA
    AAAA
    Output
    Yes
    Input
    3 4
    AAAA
    ABCA
    AADA
    Output
    No
    Input
    4 4
    YYYR
    BYBY
    BBBY
    BBBY
    Output
    Yes
    Input
    7 6
    AAAAAB
    ABBBAB
    ABAAAB
    ABABBB
    ABAAAB
    ABBBAB
    AAAAAB
    Output
    Yes
    Input
    2 13
    ABCDEFGHIJKLM
    NOPQRSTUVWXYZ
    Output
    No

    Note

    In first sample test all 'A' form a cycle.

    In second sample there is no such cycle.

    The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).

    题目大意  :判断图中是否有些相同字母组成的环,如果有的话直接出书YES,没有输出NO

    思路 :  图中的每个点都有可能构成循环,所以要逐一遍历,如果该点可以构成循环的环 那么起点是该点,,终点也是该点,环至少是4个点构成,所以如果有环的话再次回到起点时步数一定大于等于四。

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,m,end_i,end_j;
    char aa;
    char arr[100][100];
    int mark[100][100]={0};
    int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    int flag=0;
    void dfs(int x,int y,int step)
    {
        if(flag==1) return;
        if(step>=4&&x==end_i&&y==end_j)如果回到了起点且步数大于等于4  则找到环了。
        {
            flag=1;
            return;
        }
        for(int i=0;i<4;i++){
            int dx=x+d[i][0];
            int dy=y+d[i][1];
            if(dx>=0&&dy>=0&&dx<n&&dy<m&&mark[dx][dy]==0&&arr[dx][dy]==aa){
                if(dx==end_i&&y==end_j&&step<3)//起点就是终点所以起点没有被标记,当进入到第二个点时,可能会回到起点,所以要对步数进行判断,看是否大于等于3
                    continue;
                mark[dx][dy]=1;
                dfs(dx,dy,step+1);
            }
        }
    }
    int main()
    {
        cin>>n>>m;//n行m列 
        for(int i=0;i<n;i++)    scanf("%s",&arr[i]);//存图
        
        for(int i=0;i<n;i++)
        {    
            for(int j=0;j<m;j++)
            {
                memset(mark,0,sizeof(mark));
                end_i=i;
                end_j=j;//起点与终点 
                aa=arr[i][j];//搜索的字符
                dfs(i,j,0);
                if(flag==1)
                    break;
            }
            if(flag==1) break;
        }
        if(flag==1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
        return 0;
    }
  • 相关阅读:
    HTTPS协议详解
    HTTP协议详解
    网络传输协议 UDP & TCP 详解
    Socket(套接字)基础概念
    网络基础
    OSI 七层协议
    经典SQL题 1/25/50/100美分,多少种可能拼凑成2美元
    5.1一阶谓词逻辑
    4.4符号视角下的科学
    4.3领域语言与自然语言的比较
  • 原文地址:https://www.cnblogs.com/Accepting/p/11241981.html
Copyright © 2011-2022 走看看