zoukankan      html  css  js  c++  java
  • 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
    ",max);
    49     }
    50 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    基本数据类型和包装数据类型
    编程英语多样化(长期更新)
    jdk,jre,jvm,openJdk
    英语句子频率印象流
    单击单选按钮换成图片
    标签注意事项:
    背景图片处理
    天猫导航栏
    百度地图
    两栏布局
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5715021.html
Copyright © 2011-2022 走看看