本文主要是介绍poj 3620 Avoid The Lakes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很简单的dfs
/*
Poj: 3620 Avoid The Lakes
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>#define MaxN 105using 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!