c的解法
查看原帖
c的解法
1492502
SXking楼主2025/1/13 15:09
#include <stdio.h>
#include <string.h>
#define MAX 120

char arr[MAX][MAX];
int st[MAX][MAX];
int N, M, count, c = 1;
int dx[] = { 1,1,1,0,0,-1,-1,-1 };
int dy[] = { 1,0,-1,-1,1,-1,0,1 };

void dfs(int x,int y) { 
    arr[x][y] = '.';
    for (int i = 0; i < 8; i++) {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if (tx >= 1 && tx <= N && ty >= 1 && ty <= M && arr[tx][ty] == 'W') {
            dfs(tx, ty);
        }
    }
}

int main() {
    scanf("%d%d", &N, &M);
    getchar();
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            scanf("%c", &arr[i][j]);
        }
        getchar();
    }

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (arr[i][j] == 'W') {
                count++;
                dfs(i, j); 
            }
        }
    }
    printf("%d\n", count);
    return 0;
}

这是c的解法

2025/1/13 15:09
加载中...