zoukankan      html  css  js  c++  java
  • HDU

    题目链接

    https://cn.vjudge.net/contest/65959#problem/L

    题意
    @表示油田 如果 @@是连在一起的 可以八个方向相连 那么它们就是 一块油田 要找出 一共有几块油田

    思路

    可以先遍历 一遍地图

    遇到 @ 就更新答案 + 1 然后DFS 把与它相连 和与它相连的 相连的 油田 都变成 * 就可以了

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = acos(-1);
    const double E = exp(1);
    const double eps = 1e-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e2 + 5;
    const int MOD = 1e9 + 7;
    
    string G[maxn];
    
    int Move[8][2]
    {
        -1, 0,
         1, 0,
         0,-1,
         0, 1,
        -1, 1,
        -1,-1,
         1, 1,
         1,-1,
    };
    
    int n, m;
    
    bool ok(int x, int y)
    {
        if (x < 0 || x >= n || y < 0 || y >= m || G[x][y] == '*')
            return false;
        return true;
    }
    
    void dfs(int x, int y)
    {
        G[x][y] = '*';
        for (int i = 0; i < 8; i++)
        {
            int nx = x + Move[i][0];
            int ny = y + Move[i][1];
            if (ok(nx, ny))
                dfs(nx, ny);
        }
    }
    
    int main()
    {
        while (scanf("%d%d", &n, &m) && m)
        {
            for (int i = 0; i < n; i++)
                    cin >> G[i];
                int ans = 0;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (G[i][j] == '@')
                        {
                            ans++;
                            dfs(i, j);
                        }
                    }
                }
            cout << ans << endl;
        }
    }
  • 相关阅读:
    iOS学习,需要懂的一些基础
    学习RAC小记-适合给新手看的RAC用法总结(转)
    UITableView进阶,cell刷新,界面返回 保持所选cell
    冒泡排序
    ArrayList 实现随机点名
    ArrayList的使用
    java中一维数组的定义和遍历
    java 中二维数组的定义和遍历
    Java 引用数据类型
    java 中自定义类的概述
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433135.html
Copyright © 2011-2022 走看看