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 ;
}

转载于:https://www.cnblogs.com/forever4444/archive/2009/05/11/1454349.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值