zoukankan      html  css  js  c++  java
  • zoj 2412 Farm Irrigation

    ZOJ Problem Set - 2412
    Farm Irrigation

    Time Limit: 1 Second      Memory Limit: 32768 KB

    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
    Submit    Status
    //1863626 2009-05-10 21:31:51 Wrong Answer  2412 C++ 0 200 Wpl 
    //1864787 2009-05-11 18:23:54 Accepted  2412 C++ 10 200 Wpl 
    #include <iostream>
    #define MAX 55
    using namespace std;
    int n,m,un[MAX*MAX];
    char map[MAX][MAX];
    int dir[2][2]={{0,-1},{-1,0}};
    void Init()
    {
        
    int i,j;
        
    for(i=0;i<m;i++)
            
    for(j=0;j<n;j++)
            {
                un[i
    *n+j]=i*n+j;
                cin
    >>map[i][j];
            }
    }
    bool Bound(int x,int y)
    {
        
    if(x>=0&&y>=0&&x<m&&y<n)
            
    return true;
        
    else
            
    return false;
    }
    int Find(int x)
    {
        
    return un[x];
    }
    void Merge(int a,int b)
    {
        
    int mmax,mmin,i,temp;
        mmax
    =Find(a);
        mmin
    =Find(b);
        
    if(mmax==mmin)
            
    return ;
        
    if(mmin>mmax)
        {
            temp
    =mmin;
            mmin
    =mmax;
            mmax
    =temp;
        }
        
    for(i=0;i<n*m;i++)
        {
            
    if(un[i]==mmax)
                un[i]
    =mmin;
        }
    }
    int main()
    {
        
    int i,j,k;
        
    while(scanf("%d%d",&m,&n)!=EOF)
        {
            
    if(m<0||n<0)
                
    break;
            Init();
            
    int ti,tj,a,b;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    for(k=0;k<2;k++)
                    {
                        ti
    =i+dir[k][0];
                        tj
    =j+dir[k][1];
                        
    if(!Bound(ti,tj))
                            
    continue;
                        a
    =i*n+j;  //我用行存储
                        b=ti*n+tj;
                        
    if(k==0)  //向左
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='B'||map[ti][tj]=='D'||map[ti][tj]=='F'||map[ti][tj]=='G'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                        
    else     //向上
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='C'||map[ti][tj]=='D'||map[ti][tj]=='E'||map[ti][tj]=='H'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                    }
                }
            
    int sum=0;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    if(un[i*n+j]==(i*n+j))
                        sum
    ++;
                }
            printf(
    "%d\n",sum);
        }
        
    return 0;
    }

    好好反思这个代码为什么错误??????

    Problem : 1198 ( Farm Irrigation )     Judge Status : Wrong Answer
    RunId : 
    1357129    Language : C++    Author : forever4444
    Code Render Status : Rendered By HDOJ C
    ++ Code Rander Version 0.01 Beta
    //1863626 2009-05-10 21:31:51 Wrong Answer  2412 C++ 0 200 Wpl 
    #include <iostream>
    #define MAX 55
    using namespace std;
    int n,m,un[MAX*MAX];
    char map[MAX][MAX];
    int dir[2][2]={{0,-1},{-1,0}};
    void Init()
    {
        
    int i,j;
        
    for(i=0;i<m;i++)
            
    for(j=0;j<n;j++)
            {
                un[i
    *m+j]=i*m+j;
                cin
    >>map[i][j];
            }
    }
    bool Bound(int x,int y)
    {
        
    if(x>=0&&y>=0&&x<m&&y<n)
            
    return true;
        
    else
            
    return false;
    }
    int Find(int x)
    {
        
    return un[x];
    }
    void Merge(int a,int b)
    {
        
    int mmax,mmin,i,temp;
        mmax
    =Find(a);
        mmin
    =Find(b);
        
    if(mmax==mmin)
            
    return ;
        
    if(mmin>mmax)
        {
            temp
    =mmin;
            mmin
    =mmax;
            mmax
    =temp;
        }
        
    for(i=0;i<n*m;i++)
        {
            
    if(un[i]==mmax)
                un[i]
    =mmin;
        }
    }
    int main()
    {
        
    int i,j,k;
        
    while(scanf("%d%d",&m,&n)!=EOF)
        {
            
    if(m<0||n<0)
                
    break;
            Init();
            
    int ti,tj,a,b;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    for(k=0;k<2;k++)
                    {
                        ti
    =i+dir[k][0];
                        tj
    =j+dir[k][1];
                        a
    =i*m+j;  //我用行存储
                        b=ti*m+tj;
                        
    if(!Bound(ti,tj))
                            
    continue;
                        
    if(k==0)  //向左
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='B'||map[ti][tj]=='D'||map[ti][tj]=='F'||map[ti][tj]=='G'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                        
    else     //向上
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='C'||map[ti][tj]=='D'||map[ti][tj]=='E'||map[ti][tj]=='H'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                    }
                }
            
    int sum=0;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    if(un[i*m+j]==(i*m+j))
                        sum
    ++;
                }
            printf(
    "%d\n",sum);
        }
        
    return 0;
    }
    //1863626 2009-05-10 21:31:51 Wrong Answer  2412 C++ 0 200 Wpl 
    #include <iostream>
    #define MAX 55
    using namespace std;
    int n,m,un[MAX*MAX];
    char map[MAX][MAX];
    int dir[2][2]={{0,-1},{-1,0}};
    void Init()
    {
        
    int i,j;
        
    for(i=0;i<m;i++)
            
    for(j=0;j<n;j++)
            {
                un[i
    *m+j]=i*m+j;
                cin
    >>map[i][j];
            }
    }
    bool Bound(int x,int y)
    {
        
    if(x>=0&&y>=0&&x<m&&y<n)
            
    return true;
        
    else
            
    return false;
    }
    int Find(int x)
    {
        
    return un[x];
    }
    void Merge(int a,int b)
    {
        
    int mmax,mmin,i,temp;
        mmax
    =Find(a);
        mmin
    =Find(b);
        
    if(mmax==mmin)
            
    return ;
        
    if(mmin>mmax)
        {
            temp
    =mmin;
            mmin
    =mmax;
            mmax
    =temp;
        }
        
    for(i=0;i<n*m;i++)
        {
            
    if(un[i]==mmax)
                un[i]
    =mmin;
        }
    }
    int main()
    {
        
    int i,j,k;
        
    while(scanf("%d%d",&m,&n)!=EOF)
        {
            
    if(m<0||n<0)
                
    break;
            Init();
            
    int ti,tj,a,b;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    for(k=0;k<2;k++)
                    {
                        ti
    =i+dir[k][0];
                        tj
    =j+dir[k][1];
                        a
    =i*m+j;  //我用行存储
                        b=ti*m+tj;
                        
    if(!Bound(ti,tj))
                            
    continue;
                        
    if(k==0)  //向左
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='B'||map[ti][tj]=='D'||map[ti][tj]=='F'||map[ti][tj]=='G'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                        
    else     //向上
                        {
                            
    if(map[i][j]=='A'||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K')
                            {
                                
    if(map[ti][tj]=='C'||map[ti][tj]=='D'||map[ti][tj]=='E'||map[ti][tj]=='H'||map[ti][tj]=='I'||map[ti][tj]=='J'||map[ti][tj]=='K')
                                {
                                    Merge(a,b);
                                }
                            }
                        }
                    }
                }
            
    int sum=0;
            
    for(i=0;i<m;i++)
                
    for(j=0;j<n;j++)
                {
                    
    if(un[i*m+j]==(i*m+j))
                        sum
    ++;
                }
            printf(
    "%d\n",sum);
        }
        
    return 0;
    }
  • 相关阅读:
    perl 解json数组
    华为云3大体系化防护实践,保障金融业云上数据安全
    弹性文件服务解密 -- 块存储、文件存储、对象存储的区别
    【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程
    云+AI+5G时代,华为云已准备好多元化云服务架构
    高能街访 | 为什么他们都纷纷为深圳打Call?
    Angularjs进阶笔记(2)—自定义指令中的数据绑定
    Angularjs进阶笔记(1)—不同类型的双向数据绑定
    ServiceComb java-chassis和CSE java-chassis的区别
    使用inspector功能查看和管理契约
  • 原文地址:https://www.cnblogs.com/forever4444/p/1454349.html
Copyright © 2011-2022 走看看