Avoid The Lakes--poj3620

Avoid The Lakes

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7023   Accepted: 3735

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<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int N,K,M,a,b,cot,best;
 6 int map[101][101];
 7 int mov[4][2]={0,1,0,-1,1,0,-1,0};
 8 bool can(int x,int y)/判断能否符合条件
 9 {
10     if(x<0||x>=N||y<0||y>=K||!map[x][y])
11     return false;
12     return true;
13 }
14 void dfs(int x,int y)//深搜
15 {
16     int xx,yy,i;
17     map[x][y]=0;//标记走过
18     for(i=0;i<4;i++)
19     {
20         xx=x+mov[i][0];
21         yy=y+mov[i][1];
22         if(can(xx,yy))
23         {
24             cot++;
25             dfs(xx,yy);
26         }
27     }
28 }
29 int main()
30 {
31     int i,j;
32     while(scanf("%d%d%d",&N,&K,&M)!=EOF)
33     {
34         memset(map,0,sizeof(map));
35         for(i=0;i<M;i++)
36         {
37             scanf("%d%d",&a,&b);
38             map[a-1][b-1]=1;
39         }
40         int sum=0;
41         best=0;
42         for(i=0;i<N;i++)
43         for(j=0;j<K;j++)
44         {
45             cot=1;
46             if(map[i][j])
47             {
48                 dfs(i,j);
49             }
50             best=best>cot?best:cot;//更新最优解
51         }
52         printf("%d\n",best);
53     }
54 return 0;
55 }
				
时间: 2024-11-08 22:16:12

Avoid The Lakes--poj3620的相关文章

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

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 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

Avoid The Lakes

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status 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 ins

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&

Poj3620--Avoid The Lakes(基础Dfs)

Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6908   Accepted: 3680 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【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 wate

poj练习题的方法

poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1077--Eightpoj1084--Square Destroyerpoj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝)poj1088--滑雪poj1129--Channel Allocation 着色问题 dfspoj1154--letters (dfs)p