Avoid The Lakes--poj3620
来源:互联网 发布:12306网络购票时间 编辑:程序博客网 时间:2024/05/19 00:12
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: N, M, 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 53 22 23 12 31 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 }
- POJ3620:Avoid The Lakes
- POJ3620 Avoid The Lakes
- Avoid The Lakes--poj3620
- POJ3620 Avoid The Lakes
- 【poj3620】Avoid The Lakes
- Avoid The Lakes<poj3620>
- Avoid The Lakes(poj3620)
- DFS poj3620 Avoid The Lakes
- poj3620 avoid the lakes【dfs】
- DFS poj3620(Avoid The Lakes)
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
- pku3620 Avoid The Lakes
- 1543 Avoid The Lakes
- F. Avoid The Lakes
- Avoid The Lakes
- BNU Avoid The Lakes
- Avoid The Lakes POJ
- Avoid The Lakes
- Navicat导出导入数据库
- Seeding--zoj2100
- 正则判断是否为手机号码
- iframe设置高度宽度100%,实际高度有限,如何解决?
- Fire Net--hdu1045
- Avoid The Lakes--poj3620
- 二叉树
- Lotto--poj2245
- 书单
- Lake Counting--poj2386
- Nginx下限速限制下载速度实例
- TIANKENG’s restaurant--hdu4883
- copyMakeBorder函数
- android 点滴——XML和JSON对比