• 杭电1198--Farm Irrigation(****Bfs +并查集****)


    题目描述:  
    Farm Irrigation
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7577    Accepted Submission(s): 3252
    
    
    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
    Zhejiang University Local Contest 2005 
     
    
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1811 1829 1558 1195 1180
    View Code

    数组Dir[][]记录一下图中方向是否可连;

    ac[4][2]注意一下。

     第一眼看这个题, 确实是有点懵, 但是看懂后你就会发现就是个并查集。 

      <1> 先确定每个小格四个方向是否有管道接口。 
      <2>确定搜索方向, 注意要旋转一下原来坐标系。
      <3>看着看着就发现其实向上的接口只能和向下的连接(接口只能和其对立面相连接)。
    这个题思路也就是先跑一遍Bfs 把可以连接区域连接起来, 再利用并查集判断集合个数。
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 51
    using namespace std;
    char map[N][N]; int father[N*N];
    int Dir[11][4] = {
        1, 0, 1, 0,
        1, 0, 0, 1, 
        0, 1, 1, 0,
        0, 1, 0, 1,
        1, 1, 0, 0,
        0, 0, 1, 1,
        1, 0, 1, 1,
        1, 1, 1, 0,
        0, 1, 1, 1,
        1, 1, 0, 1,
        1, 1, 1, 1,
    };
    int ac[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; //比较疑惑坐标系建立方向。  应该就是 把确定小格方向的坐标系旋转一下。挺有意思 。  
    void Init(int n)
    {
        for(int i = 0; i < n; i++)
            father[i] = i;
    }
    int Find(int a)
    {
        if(a == father[a])
            return a;
        else
            return father[a] = Find(father[a]);
    }
    void Mercy(int a, int b)
    {
        int Q = Find(a);
        int P = Find(b);
        if(Q != P)
            father[Q] = P; 
    }
    int main()
    {
        int n, m;
        while(~scanf("%d%d", &n, &m))
        {
            if(n==-1 && m==-1)
                break;
            Init(n*m);
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)
                    cin >> map[i][j];
            for(int x = 0; x < n; x++)
                for(int y = 0; y < m; y++)
                {
                    int dIr = x*m+y;
                    for(int k = 0; k < 4; k++)
                    {
                        int nx = x + ac[k][0];
                        int ny = y + ac[k][1];
                        int ndIr = nx*m+ny;
                        int nk = (k%2==0? k+1:k-1);
                        if(nx >= 0 &&  nx < n && ny >= 0 && ny < m && Dir[map[x][y]-'A'][k] && Dir[map[nx][ny]-'A'][nk])
                            Mercy(dIr, ndIr);
                    }
                }
            int TotAl = 0;
            for(int i = 0; i < n*m; i++)
                if(father[i] == i)
                    TotAl++;
            printf("%d
    ", TotAl); 
        }
        return 0;
    }
  • 相关阅读:
    个人软件过程 1
    一个月学会VC++2012 3.我们动手吧!
    一个月掌握VC++2010?
    个人软件过程2 项目开发的基本流程
    个人软件过程4 功能说明和用户体验设计
    一个月学会VC++2010 5.对话框的数据交换
    个人软件过程3 需求分析
    一个月学会VC++2010 4.细说对象之香艳旖旎
    一个月学会VC++2010 1.送上门的银子
    一个月学会VC++2010 2.看起来风险不大
  • 原文地址:https://www.cnblogs.com/soTired/p/4862091.html
Copyright © 2020-2023  润新知