zoukankan      html  css  js  c++  java
  • Fox And Two Dots codeforces 510B(DFS)

    http://codeforces.com/problemset/problem/510/B

    题意:问你能否用相同的字母构成一个环。

    分析:每一个都直接从它自身开始,看看到最后是否还能回到它本身。(注意:需要最少4个点)

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 100;
    
    typedef long long LL;
    
    char maps[maxn][maxn];
    int n, m, flag;
    int dir[4][2]= {{0,1}, {1,0}, {0,-1}, {-1,0}};
    
    struct node
    {
        int step;
    }v[maxn][maxn];
    
    void DFS(int x, int y, int p, int q)
    {
        for(int i=0; i<4; i++)
        {
            int nx = dir[i][0]+x;
            int ny = dir[i][1]+y;
    
            if(nx==p && ny==q && v[x][y].step>=4)
            {
                flag = 1;
                return ;
            }
    
            if(nx>=0 && nx<n && ny>=0 && ny<m && !v[nx][ny].step && maps[nx][ny]==maps[p][q])
            {
                v[nx][ny].step = v[x][y].step+1;
                DFS(nx, ny, p, q);
            }
        }
    
        return ;
    }
    int main()
    {
        while(scanf("%d %d", &n, &m)!=EOF)
        {
            memset(maps, 0, sizeof(maps));
            for(int i=0; i<n; i++)
                scanf("%s", maps[i]);
    
             flag = 0;
    
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    memset(v, 0, sizeof(v));
                    if(!v[i][j].step)
                    {
                        v[i][j].step = 1;
                        DFS(i, j, i, j);
                    }
    
                    if(flag)  break;
                }
    
                if(flag)  break;
            }
    
            if(flag)  printf("Yes
    ");
            else printf("No
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    批处理实现SQLServer数据库备份与还原
    Axapta物流模块深度历险(二)
    Axapta4.0Tech
    Script#
    Axapta物流模块深度历险(一)
    Agrs Class
    折半的意义
    个人性格
    诚实
    英语学习闪存
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5768762.html
Copyright © 2011-2022 走看看