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 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给出一串坐标,连在一起的算一个,计算每个由多少个坐标组成,输出最大值。
 1 #include<cstdio>
 2 #include<string.h>
 3 int dx[4]={-1,1,0,0};
 4 int dy[4]={0,0,-1,1};
 5 int n,m,map[150][150],x,y,i,max,ans,k,j;
 6 void f(int x,int y)
 7 {
 8     int i,nx,ny;
 9     for(i = 0 ; i < 4 ; i++)
10     {
11         nx=x+dx[i];
12         ny=y+dy[i];
13         if(nx > 0 && ny > 0 && nx <= n && ny <=m && map[nx][ny] == 1)
14         {
15             ans++;
16             map[nx][ny]=0;
17             f(nx,ny);
18         }
19     }
20 }
21 int main()
22 {
23     while(scanf("%d %d %d",&n,&m,&k)!=EOF)
24     {
25         max=0;
26         memset(map,0,sizeof(map));
27         for(i = 0 ; i < k ; i++)
28         {
29             scanf("%d %d",&x,&y);
30             map[x][y]=1;    //1表示没走过
31         }
32         for(i = 1 ; i <=n  ; i++)
33         {
34             for(j = 1 ; j <= m ; j++)
35             {
36
37                 if(map[i][j] == 1)
38                 {
39                     ans=1;
40                     map[i][j]=0;
41                     f(i,j);
42                     max=max>ans?max:ans;
43                 }
44
45             }
46
47         }
48         printf("%d\n",max);
49     }
50 }
时间: 2024-09-29 00:43:32

POJ 3620 Avoid The Lakes (求连接最长的线)(DFS)的相关文章

[深度优先搜索] 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&

Avoid The Lakes POJ

Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6409   Accepted: 3466 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 2774 后缀数组:求最长公共子串

思路:其实很简单,就是两个字符串连接起来,中间用个特殊字符隔开,然后用后缀数组求最长公共前缀,然后不同在两个串中,并且最长的就是最长公共子串了. 注意的是:用第一个字符串来判断是不是在同一个字符中,刚开始用了第二个字符的长度来判断WA了2发才发现. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<

POJ 1226后缀数组:求出现或反转后出现在每个字符串中的最长子串

思路:这题是论文里的最后一道练习题了,不过最后一题竟然挺水的. 因为求的是未反转或者反转后,最长公共子串. 刚开始还真不知道怎么构建连接成一个字符串,因为需要有反转嘛! 但是其实挺简单的,把未反转的和反转后的字符串都连起来,中间用未出现过的字符隔开就行了!然后未反转的和反转的在同一组. 二分枚举最长的公共前缀长度,然后统计看看这个最长的长度在不在所有的组里,如果在就符合-- #include<iostream> #include<cstdio> #include<cstrin

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符