1 条题解

  • 1
    @ 2023-12-17 0:35:28

    最爱的暴力

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <string.h>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <map>
    #include <deque>
    #define ll long long
    #define pi 3.1415926535
    using namespace std;
    int m, n, sum, maxn, cnt = 1;
    int a[57][57];
    bool v[57][57];
    void dfs(int x, int y) {
        if (a[x][y] == 0) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
        }
        else if (a[x][y] == 1) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y),  cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 2) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
        }
        else if (a[x][y] == 3) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 4) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
        }
        else if (a[x][y] == 5) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
        }
        else if (a[x][y] == 6) {
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
        }
        else if (a[x][y] == 7) {
            if (x + 1 <= n && !v[x + 1][y]) v[x + 1][y] = 1, dfs(x + 1, y), cnt++;
        }
        else if (a[x][y] == 8) {
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 9) {
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 10) {
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 11) {
            if (y + 1 <= m && !v[x][y + 1])v[x][y + 1] = 1, dfs(x, y + 1), cnt++;
        }
        else if (a[x][y] == 12) {
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
        }
        else if (a[x][y] == 13) {
            if (x - 1 > 0 && !v[x - 1][y])v[x - 1][y] = 1, dfs(x - 1, y), cnt++;
        }
        else if (a[x][y] == 14) {
            if (y - 1 > 0 && !v[x][y - 1]) v[x][y - 1] = 1, dfs(x, y - 1), cnt++;
        }
        return;
    }
    int main() {
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        cin >> n >> m;
        memset(v, 0, sizeof(v));
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
                cin >> a[i][j];
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
                if (!v[i][j]) {
                    v[i][j] = 1;
                    sum++;
                    dfs(i, j);
                    maxn = max(cnt, maxn);
                    cnt = 1;
                }
        cout << sum << endl << maxn;
        return 0;
    }
    
    • 1

    信息

    ID
    250
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    (无)
    递交数
    55
    已通过
    19
    上传者