题目链接:http://poj.org/problem?id=3620 分四个方向dfs,最后找到最大值。
view code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 #define maxl 10000 9 int n, m, k, num, G[110][110], f[110][110]; 10 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; 11 int dfs(int p, int q) 12 { 13 f[p][q] = 1; 14 for (int i = 0; i < 4; i++) 15 { 16 int x = p + dir[i][0]; 17 int y = q + dir[i][1]; 18 if (!f[x][y] && G[x][y]) 19 { 20 num++; 21 dfs(x, y); 22 } 23 } 24 return num; 25 } 26 int main() 27 { 28 while (~scanf("%d%d%d", &n, &m, &k)) 29 { 30 int ans = 0; 31 memset(f, 0, sizeof(f)); 32 memset(G, 0, sizeof(G)); 33 while (k--) 34 { 35 int x, y; 36 scanf("%d%d", &x, &y); 37 G[x][y] = 1; 38 } 39 for (int i = 1; i <= n; i++) 40 for (int j = 1; j <= m; j++) 41 { 42 if (!f[i][j] && G[i][j]) 43 { 44 num = 1; 45 ans = max(ans, dfs(i, j)); 46 } 47 } 48 printf("%d\n", ans); 49 } 50 return 0; 51
|