[dfs] aw1113. 红与黑(dfs连通性模型+dfs计数方式+模板题)

Posted Ypuyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[dfs] aw1113. 红与黑(dfs连通性模型+dfs计数方式+模板题)相关的知识,希望对你有一定的参考价值。

1. 题目来源

链接:1113. 红与黑

2. 题目解析

模板题,注意起点也算一个砖块。


时间复杂度: O ( n 2 ) O(n^2) O(n2)

空间复杂度: O ( n 2 ) O(n^2) O(n2)


dfs

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 25;

int n, m;
char g[N][N];
bool st[N][N];
int res;			// 全局记录答案

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

// 全局变量记录砖块个数
void dfs(int x, int y) {
    st[x][y] = true;
    
    for (int i = 0; i < 4; i ++ ) {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= m) continue;
        if (g[a][b] == '#') continue;
        if (st[a][b]) continue;

        res ++ ;
        dfs(a, b);
    }
}

// 递归记录砖块个数
int dfs(int x, int y) {
    st[x][y] = true;
    
    int res = 1;
    for (int i = 0; i < 4; i ++ ) {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= m) continue;
        if (g[a][b] == '#') continue;
        if (st[a][b]) continue;

        res += dfs(a, b);   // 递归传参
    }
    return res;
}

int main() {
    while (scanf("%d%d", &m, &n), m != 0 && n != 0) {
        for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
        
        
        int sx, sy;
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ ) 
                if (g[i][j] == '@') {
                    sx = i, sy = j;
                    break;
                }
                
        res = 1;			// 起始砖块为 1
        memset(st, 0, sizeof st);
        
        // int res = dfs(sx, sy);			// 递归传参
        
        dfs(sx, sy);
        printf("%d\\n", res);
    }

    return 0;
}

bfs

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 25;

int n, m;
char g[N][N];
bool st[N][N];
PII q[N * N];

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int bfs() {
    memset(st, 0, sizeof st);
    
    int sx, sy;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ ) 
            if (g[i][j] == '@') {
                sx = i, sy = j;
                break;
            }
    int res = 1;
    int hh = 0, tt = 0;

    q[0] = {sx, sy};
    st[sx][sy] = true;

    while (hh <= tt) {
        auto t = q[hh ++ ];

        int x = t.first, y = t.second;
        for (int i = 0; i < 4; i ++ ) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m) continue;
            if (g[a][b] == '#') continue;
            if (st[a][b]) continue;

            res ++ ;
            q[ ++ tt ] = {a, b};
            st[a][b] = true;
        }
    }

    return res;
}

int main() {
    while (scanf("%d%d", &m, &n), m != 0 && n != 0) {
        for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
    
        int res = bfs();
        printf("%d\\n", res);
    }

    return 0;
}

以上是关于[dfs] aw1113. 红与黑(dfs连通性模型+dfs计数方式+模板题)的主要内容,如果未能解决你的问题,请参考以下文章

AcWing 1113. 红与黑 (BFS DFS)

[dfs] aw1112. 迷宫(dfs连通性模型+模板题)

1113. 红与黑连通块

[FloodFill] aw1097. 池塘计数(bfs+dfs+FloodFill+模板题)

Bailian2816 红与黑DFS

寒假每日一题(入门组)week2 完结