CodeForces 510B Fox And Two Dots(图论-回路判定)

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

These k dots are different: if i ≠ j then di is different from dj.
k is at least 4.
All dots belong to the same color.
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.

Sample Input
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
Hint
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).

题意:
给你一个字符图,不同字符代表不同颜色,问,图内是否有同色回路,回路中两结点之间必须位置相邻相异,且路径长度不小于4。

分析:
看到这个题,第一反应是dfs没错,但难点在于回路的判断。
形成回路的结点条件有以下几条:
1.结点在图内;
2.除非下一结点是起始结点,否则继续dfs未遍历结点;
3.形成的回路必须由不少于4个结点组成;
4.同色;
由于图规模只有50X50,故可对每一结点进行dfs搜索,直到找到同色回路位置。
此外条件判断顺序也是一大难点,需要多加注意。
//代码能力太差,还需多切几道搜索题

代码如下:

/*
*Author : Flint_x 
*Created Time : 2015-02-26 12:36:43 
*File name : h.cpp 
*/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double eps(1e-8);
typedef long long lint;

char map1[55][55];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool visit[55][55];
int m,n;
int step = 0;
bool dfs(char color , int startx , int starty , int x , int y){


    int nx,ny;
    if(x == startx && y == starty && visit[startx][starty]){
        return step >= 4;
    }
    visit[x][y] = 1;
    for( int k = 0 ; k < 4 ; k++){
        nx = x + dx[k];
        ny = y + dy[k];
        if((map1[nx][ny] == color && nx > 0 && ny > 0 && nx <= n && ny <= m && !visit[nx][ny]) || (nx == startx && ny == starty)){
            step ++;
//          cout << nx << endl;
//          cout << ny << endl;
            if(dfs(color , startx , starty , nx , ny)) 
                return true;
            step --;

        }
    }
    return false;
}
int main(){

    while(cin >> n){
        cin >> m;
        for( int i = 1 ; i <= n ; i++){
            for( int j = 1 ; j <= m ; j++){
                cin >> map1[i][j];
            }
        }
        bool flag = 0;

        for( int i = 1 ; i <= n ; i++){
            for( int j = 1 ; j <= m ; j++){
                memset(visit , 0 ,sizeof(visit));
                step = 0;
                flag = dfs(map1[i][j],i,j,i,j);
                if(flag){

                    cout << "Yes" << endl;
                    return 0;
                }
            }
        }
        if(!flag){
            cout << "No" << endl; 
            return 0;
        }
    }
    return 0;
}

你可能感兴趣的:(图论,ACM,and,novicer)