• DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots


    题目传送门

     1 /*
     2     DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环
     3 */
     4 #include <cstdio>
     5 #include <iostream>
     6 #include <cstring>
     7 #include <string>
     8 #include <map>
     9 #include <algorithm>
    10 #include <vector>
    11 #include <set>
    12 #include <cmath>
    13 using namespace std;
    14 
    15 const int MAXN = 1e6 + 10;
    16 const int INF = 0x3f3f3f3f;
    17 int dx[4] = {1, -1, 0, 0};
    18 int dy[4] = {0, 0, 1, -1};
    19 char a[55][55];
    20 int used[55][55];
    21 int n, m;
    22 
    23 bool DFS(int x, int y, int px, int py, char ch)
    24 {
    25     used[x][y] = 1;
    26     for (int i=0; i<=4-1; ++i)
    27     {
    28         int tx = x + dx[i];
    29         int ty = y + dy[i];
    30         if (tx == px && ty == py)   continue;
    31         if (tx >= 0 && tx <= n-1 && ty >= 0 && ty <=m-1 && a[tx][ty] == ch)
    32         {
    33             if (used[tx][ty])   return true;
    34             if (DFS (tx, ty, x, y, ch)) return true;
    35         }
    36     }
    37     
    38     return false;
    39 }
    40 
    41 int main(void)
    42 {
    43     //freopen ("B.in", "r", stdin);
    44     
    45     while (cin >> n >> m)
    46     {
    47         memset (used, 0, sizeof (used));
    48         for (int i=0; i<=n-1; ++i)
    49         {
    50             scanf ("%s", &a[i]);
    51         }
    52         
    53         bool flag = false;
    54         for (int i=0; i<=n-1; ++i)
    55         {
    56             for (int j=0; j<=m-1; ++j)
    57             {
    58                 if (!used[i][j])
    59                 {
    60                     if (DFS (i, j, -1, -1, a[i][j]))
    61                     {
    62                         puts ("Yes");   flag = true;    break;
    63                     }
    64                 }
    65             }
    66             if (flag)   break;
    67         }
    68         
    69         if (!flag)  puts ("No");
    70     }
    71     
    72     return 0;
    73 }
    编译人生,运行世界!
  • 相关阅读:
    QR code 乱谈(一)
    用JAVA实现数字水印(可见)
    ctf总结
    Unix/Linux常用命令
    C语言概述
    C语言发发展历史
    为什么要学习C语言
    计算机应用领域
    计算机发展趋势
    如何学习计算机
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4366706.html
Copyright © 2020-2023  润新知