Satellite Photographs--DFS

分析:连通块问题。。标准DFS


Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
 
const int maxn = 1000+5;
 
char g[maxn][maxn];
bool vis[maxn][maxn];
int ans, cnt;
int n, m;
int dr[4] = {0, 1, 0, -1};
int dc[4] = {1, 0, -1, 0};
 
void dfs(int r, int c) {
    cnt++;
    for(int i = 0; i < 4; i++) {
        int tr = r+dr[i];
        int tc = c+dc[i];
        if(0 <= tr && tr < n && 0 <= tc && tc < m && !vis[tr][tc] && g[tr][tc] == '*') {
            vis[tr][tc] = 1;
            dfs(tr, tc);
        }
    }
}
 
int main() {
    while(~scanf("%d%d", &m, &n)) {
        memset(g, 0, sizeof(g));
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++)
            scanf("%s", g[i]);
        ans = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m ; j++) {
                if(g[i][j] == '*' && !vis[i][j]) {
                    vis[i][j] = 1;
                    cnt = 0;
                    dfs(i, j);
                    ans = max(ans, cnt);
                }
            }
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(Satellite Photographs--DFS)