POJ3051 Satellite Photographs(DFS水题)

January 12, 2016

题目链接

题意:求图中最大的连通*的个数。

最近期末没刷题,这么水的题竟然跪了两发。。

#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
char pic[1050][150];
int judge[1050][150];
int n, m;
int ans, num;

void dfs(int a, int b){
    if(pic[a][b] == '.' || judge[a][b] == 1 || a <0 || b<0 || a >=m || b >=n)
        return;
    judge[a][b] = 1;
    num++;
    ans = max(ans, num);
    dfs(a-1, b);
    dfs(a+1, b);
    dfs(a, b+1);
    dfs(a, b-1);
}

int main(){
    //freopen("a.txt", "r", stdin);
    while(scanf("%d %d", &n, &m) != EOF){
        for(int i = 0; i < m; i++)
            scanf("%s", pic[i]);
        ans = 0;
        memset(judge, 0, sizeof(judge));
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++){
                num = 0;
                dfs(i, j);
            }
        cout << ans << endl;

    }
    return 0;
}

Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]