POJ 3620--Avoid The Lakes【DFS】

Avoid The Lakes

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6775   Accepted: 3620

Description

Farmer John‘s farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his
farm.

The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ K ≤ N × M) of the
cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected
cell and is part of the lake.

Input

* Line 1: Three space-separated integers: NM, and K

* Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C

Output

* Line 1: The number of cells that the largest lake contains. 

Sample Input

3 4 5
3 2
2 2
3 1
2 3
1 1

Sample Output

4

题意:第一行输入 n , m ,k 。代表有个 n * m 大小的地区, k 代表有k个水坑,接下来的k行代表k个水坑的坐标,

一个水坑可以和它上下左右的水坑组成一个大水坑,问大水坑最多有多少个小水坑组成。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int map[110][110];
int vis[110][110];
int n, m, k, ans;

bool check(int x, int y){
    if(x < 1 || x > n || y < 1 || y > m)
        return 0;
    if(map[x][y] == 0)
        return 0;
    if(vis[x][y] == 1)
        return 0;
    return 1;
}

void dfs(int x,int y){
    ans++;
    vis[x][y] = 1;
    for(int i = 0; i < 4; ++i){
        int fx = x + dir[i][0];
        int fy = y + dir[i][1];
        if(check(fx, fy))
            dfs(fx, fy);
    }
}

int main (){
    while(scanf("%d%d%d", &n, &m, &k) != EOF){
        memset(map, 0, sizeof(map));
        while(k--){
            int x, y;
            scanf("%d%d", &x, &y);
            map[x][y] = 1;
        }
        memset(vis, 0, sizeof(vis));
        int sum = 0;
        for(int i = 1;i <= n; ++i)
        for(int j = 1; j <= m; ++j){
            ans = 0;
            if(map[i][j]){
                dfs(i, j);
                sum = max(sum, ans);
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-07 22:22:01

POJ 3620--Avoid The Lakes【DFS】的相关文章

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

[深度优先搜索] POJ 3620 Avoid The Lakes

Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 4270 Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of wate

POJ 3620 Avoid The Lakes

Avoid The Lakes Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the si

POJ 3620 Avoid The Lakes(DFS)

题目链接:http://poj.org/problem?id=3620 DFS基础题~ #include<cstdio> #include<iostream> #include<sstream> #include<cstdlib> #include<cstring> #include<string> #include<climits> #include<cmath> #include<algorithm&

POJ 3620 Avoid The Lakes (求连接最长的线)(DFS)

Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the larges

Poj 1426 Find The Multiple 【DFS】

Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19815 Accepted: 8048 Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only t

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

poj 1321 棋盘问题 【DFS】

题意:... 策略:深搜. 仔细分析我们发现,我们只需要对列进行标记,对于行我们考虑放棋子还是不放就行了. 代码: #include<stdio.h> #include<string.h> char s[10][10]; int n, m; int vis[10]; int ans; void dfs(int cur, int step) { if(step == m){ ans ++; return; } if(cur > n-1) return ; int i, j; f

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||