POJ 3620--avoid the Lakes "DFS"

Source: Internet
Author: User

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 so his cows is death Ly afraid of water. His insurance agency would only repay him, however, a 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 ≤ ) columns. Each cell in the grid is either dry or submerged, and exactly K (1≤ kN x M) of the Cells are submerged. As one would expect, a lake has a central cell to which and other cells connect by sharing a long edge (not a corner). Any cell this shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected C Ell and is part of the lake.

Input

* Line 1:three space-separated integers: N, M, and K
* Lines 2. K+1:line i+1 describes one submerged location with II space separated integers that is its row and co Lumn: R and C

Output

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

Sample Input

3 4 53 22 23 12 31 1

Sample Output

4

Test instructions: The first line inputs N, M, K. Representing an area of n * M size, K represents a K-puddle, and the next K-row represents the coordinates of a K-puddle,

A puddle can form a pit of water and water around it, asking how many puddles are in the pits.

#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, a    Ns;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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3620--avoid the Lakes "DFS"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.