POJ 3620 Avoid The Lakes

http://poj.org/problem?id=3620

DFS

从任意一个lake出发

重置联通的lake 并且记录 更新ans

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 using namespace std;
 5 
 6 
 7 int N,M,K;
 8 bool pool[107][107];
 9 int d[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
10 int res = 0;
11 int ans = 0;
12 bool OK(int x, int y)
13 {
14     if (x < 0 || x > N || y < 0 || y > M) return false;
15     return pool[x][y];
16 }
17 void dfs(int x, int y)
18 {
19     res++;
20     pool[x][y] = false;
21     int nx, ny;
22     for (int i = 0; i < 4; i++)
23     {
24         nx = x+d[i][0];
25         ny = y+d[i][1];
26         if ( OK(nx, ny) )
27         {
28             dfs(nx, ny);
29         }
30     }
31 }
32 int main()
33 {35     while (cin >> N >> M >> K)
36     {
37         memset(pool, 0, sizeof(pool));
38         for (int i = 0; i < K; i++)
39         {
40             int r, c;
41             scanf("%d%d", &r, &c);
42             pool[r][c] = true;
43         }
44         res = 0;
45         ans = 0;
46         for (int i = 1; i <= N; i++)
47         for (int j = 1; j <= M; j++)
48         {
49           if (OK(i, j))
50           {
51               res = 0;
52               dfs(i, j);
53           }
54           ans = max(ans, res);
55         }
56         cout << ans << endl;
57     }
58     return 0;
59 }

 

posted @ 2017-04-21 20:55  Lorazepam  阅读(176)  评论(0编辑  收藏  举报