poj 3620 Avoid The Lakes

很简单的dfs

/*
Poj: 3620 Avoid The Lakes
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

#define MaxN 105

using namespace std;

int dirx[4] = {0, 0, -1, 1};
int diry[4] = {-1, 1, 0, 0};
bool map[MaxN][MaxN];
bool visited[MaxN][MaxN];;
int n, m, k;

bool check(int x, int y)
{
    if(x >= 1 && x <= n && y >= 1 && y <= m)
        return true;
    return false;
}

int dfs(int x, int y)
{
    int total = 1;
    for(int i = 0; i < 4; i++) {
        int tmpx = x + dirx[i];
        int tmpy = y + diry[i];
        if(check(tmpx, tmpy) && !visited[tmpx][tmpy] && map[tmpx][tmpy]) {
            visited[tmpx][tmpy] = true;
            total += dfs(tmpx, tmpy);
        }
    }
    return total;
}

int main()
{   
    //freopen("data.in", "rb", stdin);
    while(scanf("%d%d%d", &n, &m, &k) != EOF) {
        memset(map, false, sizeof(map));
        for(int i = 0; i < k; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            map[a][b] = true;
        }
        
        memset(visited, false, sizeof(visited));
    
        int res = 0;  
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                if(!visited[i][j] && map[i][j]) {
                    visited[i][j] = true;
                    res = max(dfs(i, j), res);
                }
            }
        }
        printf("%d\n", res);        
    }
    
    return 0;
}

你可能感兴趣的:(poj 3620 Avoid The Lakes)