Language: C++


#include <cstdio>
#include <cstring>

const int MAXN = 105;
char map[MAXN][MAXN];
int n, m;
int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

int dfs(int px, int py, char c)
{
    map[px][py] = ' ';
    for(int i = 0; i < 4; i++)
    {
        int x = px + d[i][0];
        int y = py + d[i][1];
        if(x >= 0 && x < n && y >=0 && y < m
           && c == map[x][y])
           dfs(x, y, c);
    }
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        if(0 == n && 0 == m) break;
        getchar();
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &map[i][j]);
            }
            getchar();
        }
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(' ' != map[i][j])
                {
                    dfs(i, j, map[i][j]);
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

#include <cstdio>
#include <cstring>

const int MAXN = 105;
char map[MAXN][MAXN];
int n, m;
int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

int dfs(int px, int py, char c)
{
    map[px][py] = ' ';
    for(int i = 0; i < 4; i++)
    {
        int x = px + d[i][0];
        int y = py + d[i][1];
        if(x >= 0 && x < n && y >=0 && y < m
           && c == map[x][y])
           dfs(x, y, c);
    }
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        if(0 == n && 0 == m) break;
        getchar();
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &map[i][j]);
            }
            getchar();
        }
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(' ' != map[i][j])
                {
                    dfs(i, j, map[i][j]);
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

Compiler output:
a.cc: In function 'int dfs(int, int, char)':
a.cc:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
   21 | }
      | ^
a.cc: At global scope:
a.cc:57:11: error: redefinition of 'const int MAXN'
   57 | const int MAXN = 105;
      |           ^~~~
a.cc:5:11: note: 'const int MAXN' previously defined here
    5 | const int MAXN = 105;
      |           ^~~~
a.cc:58:6: error: redefinition of 'char map [105][105]'
   58 | char map[MAXN][MAXN];
      |      ^~~
a.cc:6:6: note: 'char map [105][105]' previously declared here
    6 | char map[MAXN][MAXN];
      |      ^~~
a.cc:59:5: error: redefinition of 'int n'
   59 | int n, m;
      |     ^
a.cc:7:5: note: 'int n' previously declared here
    7 | int n, m;
      |     ^
a.cc:59:8: error: redefinition of 'int m'
   59 | int n, m;
      |        ^
a.cc:7:8: note: 'int m' previously declared here
    7 | int n, m;
      |        ^
a.cc:60:5: error: redefinition of 'int d [4][2]'
   60 | int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
      |     ^
a.cc:8:5: note: 'int d [4][2]' previously defined here
    8 | int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
      |     ^
a.cc:62:5: error: redefinition of 'int dfs(int, int, char)'
   62 | int dfs(int px, int py, char c)
      |     ^~~
a.cc:10:5: note: 'int dfs(int, int, char)' previously defined here
   10 | int dfs(int px, int py, char c)
      |     ^~~
a.cc: In function 'int dfs(int, int, char)':
a.cc:73:1: warning: no return statement in function returning non-void [-Wreturn-type]
   73 | }
      | ^
a.cc: At global scope:
a.cc:75:5: error: redefinition of 'int main()'
   75 | int main()
      |     ^~~~
a.cc:23:5: note: 'int main()' previously defined here
   23 | int main()
      |     ^~~~