HDU 1198 Farm Irrigation

Source: Internet
Author: User
/*  And query the set! Finally, several fathers are the answer.  */  # Include  <  Iostream  >  
# Include < Set >
Using Namespace STD;
Struct G {
Int Left;
Int Right;
Int Up;
Int Down;
};
Const Int Maxn = 50 ;
Int Father [maxn * Maxn];
Int M, N;
G [ 11 ] = {{ 1 , 0 , 1 , 0 },{ 0 , 1 , 1 , 0 },{ 1 , 0 , 0 , 1 },
{ 0 , 1 , 0 , 1 },{ 0 , 0 , 1 , 1 },{ 1 , 1 , 0 , 0 },{ 1 , 1 , 1 , 0 },
{ 1 , 0 , 1 , 1 },{ 1 , 1 , 0 , 1 },{ 0 , 1 , 1 , 1 },{ 1 , 1 , 1 , 1 }};
Void Markset ( Int M, Int N ){
For ( Int I = 0 ; I < M * N; I ++ ){
Father [I] = I;
}
}
Int Findset ( Int X ){
If (Father [x] ! = X)
Father [x] = Findset (father [x]);
Return Father [x];
}
Void Merge ( Int X1, Int Y1, Int X2, Int Y2 ){
Int FX = X1 * N + Y1; //
Int FY = X2 * N + Y2; //
FX = Findset (FX );
FY = Findset (FY );
If (FX = FY ){
Return ;
} Else If (FX < FY ){
Father [FY] = FX;
} Else {
Father [FX] = FY;
}
}
Bool Connect1 ( Char Success, Char CH2 ){
If (G [Signature - ' A ' ]. Right = 1 && G [CH2 - ' A ' ]. Left = 1 )
Return True ;
Else
Return False ;
}
Bool Connect2 ( Char Success, Char CH2 ){
If (G [Signature - ' A ' ]. Down = 1 && G [CH2 - ' A ' ]. Up = 1 )
Return True ;
Else
Return False ;
}
Int Main (){
Char Map [maxn] [maxn];
Set < Int > S;
While (CIN > M > N ){
If (M < 0 && N < 0 )
Break ;
S. Clear ();
Markset (m, n );
For ( Int I = 0 ; I < M; I ++ ){
CIN > Map [I] [ 0 ];
If (I ! = 0 && Connect2 (Map [I - 1 ] [ 0 ], Map [I] [ 0 ]) {
Merge (I, 0 , I - 1 , 0 );
}
For ( Int J = 1 ; J < N; j ++ ){
CIN > Map [I] [J];
If (Connect1 (Map [I] [J - 1 ], Map [I] [J])
Merge (I, j - 1 , I, j );
If (I ! = 0 && Connect2 (Map [I - 1 ] [J], map [I] [J]) {
Merge (I - 1 , J, I, j );
}
}
}
For ( Int I = 0 ; I < M * N; I ++ )
Findset (I );
For ( Int I = 0 ; I < M * N; I ++ )
S. insert (father [I]);
Cout < S. Size () < Endl; // Use the STL set container to determine whether it is connected.
}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.