【POJ】[3620]Avoid The Lakes

文章字数:178

问题描述

Avoid The Lakes

问题分析

注意横纵坐标的对应关系
注意给的坐标是以1n而不是0n-1
每次查找后把当前位置填充防止重复查找
取查找的最大值为结果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<string.h>
int map[120][120];
int move[6]= {1,-1,0,0};
int W,H,cnt;
void dfs(int n,int m) {
    cnt++;
    map[n][m]=0;
    for(int i=0; i<4; i++) {
        int tn=n+move[i],tm=m+move[(i+2)%4];
        if(tn>0&&tn<=H&&tm>0&&tm<=W&&map[tn][tm]==1)
            dfs(tn,tm);
    }
    return ;
}
int main() {
    int T;
    while(scanf("%d %d %d",&H,&W,&T)!=EOF) {
        memset(map,0,sizeof(map));
        while(T--) {
            int n,m;
            scanf("%d %d",&n,&m);
            map[n][m]=1;
        }
        int res=0;
        for(int i=1; i<=H; i++)
            for(int j=1; j<=W; j++) {
                if(map[i][j]==0)
                    continue;
                cnt=0;
                dfs(i,j);
                if(res<cnt)
                    res=cnt;
            }
        printf("%d\n",res);
    }
    return 0;
}

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...