zoukankan      html  css  js  c++  java
  • HDU 1198 Farm Irrigation

    Farm Irrigation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2492    Accepted Submission(s): 1107


    Problem Description
    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.


    Figure 1


    Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

    ADC
    FJK
    IHE

    then the water pipes are distributed like


    Figure 2


    Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

    Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

    Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
     
    Input
    There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
     
    Output
    For each test case, output in one line the least number of wellsprings needed.
     
    Sample Input
    2 2 DK HF 3 3 ADC FJK IHE -1 -1
     
    Sample Output
    2 3
     
    Author
    ZHENG, Lu
     
    Source
     
    Recommend
    Ignatius.L

    //做的最郁闷的一次并查集了,看到这个set()函数就知道哪郁闷了

    //

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    bool h1[100][100];
    bool h2[100][100];
    void set()
    {
      h1['B']['A']=h1['B']['C']=h1['B']['F']=h1['B']['G']=h1['B']['H']=h1['B']['I']=h1['B']['K']=1;
      h1['D']['A']=h1['D']['C']=h1['D']['F']=h1['D']['G']=h1['D']['H']=h1['D']['I']=h1['D']['K']=1;
      h1['F']['A']=h1['F']['C']=h1['F']['F']=h1['F']['G']=h1['F']['H']=h1['F']['I']=h1['F']['K']=1;
      h1['G']['A']=h1['G']['C']=h1['G']['F']=h1['G']['G']=h1['G']['H']=h1['G']['I']=h1['G']['K']=1;
      h1['I']['A']=h1['I']['C']=h1['I']['F']=h1['I']['G']=h1['I']['H']=h1['I']['I']=h1['I']['K']=1;
      h1['J']['A']=h1['J']['C']=h1['J']['F']=h1['J']['G']=h1['J']['H']=h1['J']['I']=h1['J']['K']=1;
      h1['K']['A']=h1['K']['C']=h1['K']['F']=h1['K']['G']=h1['K']['H']=h1['K']['I']=h1['K']['K']=1;

      h2['C']['A']=h2['C']['B']=h2['C']['E']=h2['C']['G']=h2['C']['H']=h2['C']['J']=h2['C']['K']=1;
      h2['D']['A']=h2['D']['B']=h2['D']['E']=h2['D']['G']=h2['D']['H']=h2['D']['J']=h2['D']['K']=1;
      h2['E']['A']=h2['E']['B']=h2['E']['E']=h2['E']['G']=h2['E']['H']=h2['E']['J']=h2['E']['K']=1;
      h2['H']['A']=h2['H']['B']=h2['H']['E']=h2['H']['G']=h2['H']['H']=h2['H']['J']=h2['H']['K']=1;
      h2['I']['A']=h2['I']['B']=h2['I']['E']=h2['I']['G']=h2['I']['H']=h2['I']['J']=h2['I']['K']=1;
      h2['J']['A']=h2['J']['B']=h2['J']['E']=h2['J']['G']=h2['J']['H']=h2['J']['J']=h2['J']['K']=1;
      h2['K']['A']=h2['K']['B']=h2['K']['E']=h2['K']['G']=h2['K']['H']=h2['K']['J']=h2['K']['K']=1;
    }
    int n,m,t;
    int f[2503],r[2503];
    int find_f(int x)
    {
        if(x!=f[x])
        {
            return f[x]=find_f(f[x]);
        }
        return x;
    }
    void union_set(int x,int y)
    {
        x=find_f(x);
        y=find_f(y);
        if(x==y) return;
        t--;
        if(r[x]>r[y])
            {
                f[y]=x;
            }
        else if(r[x]<r[y])
             {
                 f[x]=y;
             }
             else
             {
                f[y]=x;
                r[x]++;
             }
    }
    char s[66][66];
    int main()
    {
        int i,j,a,b;
        set();
        while(scanf("%d%d",&n,&m),n>0&&m>0)
        {
            memset(s,0,sizeof(s));//开始忘记这个、WA了
            for(i=1;i<=n;i++)
              scanf("%s",s[i]+1);
            t=n*m;
            for(i=1;i<=t;i++)
              f[i]=i,r[i]=0;

            for(i=1;i<=n;i++)
             for(j=1;j<=m;j++)
              {
                  if(h1[s[i][j]][s[i][j+1]])
                   {
                       a=(i-1)*m+j;
                       b=a+1;
                       union_set(a,b);
                   }
                   if(h2[s[i][j]][s[i+1][j]])
                   {
                       a=(i-1)*m+j;
                       b=i*m+j;    //这个也写错了次,Wa原因之一呀
                       union_set(a,b);
                   }
              }
            printf("%d\n",t);
        }
        return 0;
    }

  • 相关阅读:
    BZOJ 1266: [AHOI2006]上学路线route
    重磅!阿里云Promtheus 正式免费公测
    解锁云原生 AI 技能|在 Kubernetes 上构建机器学习系统
    更新与发展 | Alibaba Cloud Linux 2 特性与开发细节揭秘
    《2019上半年DDoS攻击态势报告》发布:应用层攻击形势依然严峻,海量移动设备成新一代肉鸡
    《2019年上半年Web应用安全报告》发布:90%以上攻击流量来源于扫描器,IP身份不再可信
    并发模式与 RPS 模式之争,性能压测领域的星球大战
    互联网商城的上云改造之旅
    技术人具备“结构化思维”意味着什么?
    弘康人寿基于 RocketMQ 构建微服务边界总线的实践
  • 原文地址:https://www.cnblogs.com/372465774y/p/2584910.html
Copyright © 2011-2022 走看看