Hdu 1198 Farm Irrigation (search + query set)

Source: Internet
Author: User

/* I used this question to search for it. I originally wanted to use and query the set. However, WA can't directly search for it.

*/

# Include <cstdio>

# Include <cstring>


Int n, m;
Int dx [] = {0, 0, 1,-1}, dy [] = {1,-1, 0}; // r l d u
Char s [2, 555] [555]
Int p [555], vis [555] [555];
Struct Node
{
Int u, d, l, r;
} Map [2, 555] [2, 555];


Int build (int x, int y)
{
If (s [x] [y] = 'A ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 0;
Map [x] [y]. l = 1;
Map [x] [y]. r = 0;
}
Else if (s [x] [y] = 'B ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 0;
Map [x] [y]. l = 0;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'C ')
{
Map [x] [y]. u = 0;
Map [x] [y]. d = 1;
Map [x] [y]. l = 1;
Map [x] [y]. r = 0;
}
Else if (s [x] [y] = 'D ')
{
Map [x] [y]. u = 0;
Map [x] [y]. d = 1;
Map [x] [y]. l = 0;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'E ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 1;
Map [x] [y]. l = 0;
Map [x] [y]. r = 0;
}
Else if (s [x] [y] = 'F ')
{
Map [x] [y]. u = 0;
Map [x] [y]. d = 0;
Map [x] [y]. l = 1;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'G ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 0;
Map [x] [y]. l = 1;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'H ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 1;
Map [x] [y]. l = 1;
Map [x] [y]. r = 0;
}
Else if (s [x] [y] = 'I ')
{
Map [x] [y]. u = 0;
Map [x] [y]. d = 1;
Map [x] [y]. l = 1;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'J ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 1;
Map [x] [y]. l = 0;
Map [x] [y]. r = 1;
}
Else if (s [x] [y] = 'k ')
{
Map [x] [y]. u = 1;
Map [x] [y]. d = 1;
Map [x] [y]. l = 1;
Map [x] [y]. r = 1;
}
}
Int insert (int nx, int ny, int x, int y, int d)
{
If (d = 0)
{
If (map [x] [y]. r & map [nx] [ny]. l)
Return 1;
}
Else if (d = 1)
{
If (map [x] [y]. l & map [nx] [ny]. r)
Return 1;
}
Else if (d = 2)
{
If (map [x] [y]. d & map [nx] [ny]. u)
Return 1;
}
Else if (d = 3)
{
If (map [x] [y]. u & map [nx] [ny]. d)
Return 1;
}
Return 0;
}
Int find (int x)
{
Return x = p [x]? X: p [x] = find (p [x]);
}
Int Union (int x, int y)
{
Int nx = find (x );
Int ny = find (y );
If (nx! = Ny)
P [ny] = nx;
}
Int dfs (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int nx = dx [I] + x;
Int ny = dy [I] + y;
If (nx> = 0 & nx <n & ny> = 0 & ny <m)
If (! Vis [nx] [ny] & insert (nx, ny, x, y, I ))
{
Vis [nx] [ny] = 1;
Union (x * m + y, nx * m + ny );
Dfs (nx, ny );
}
}
}
Int solve ()
{
Int ans = 0;
Memset (vis, 0, sizeof (vis ));
For (int I = 0; I <n; I ++)
For (int j = 0; j <m; j ++)
If (! Vis [I] [j])
{
Vis [I] [j] = 1;
Ans ++;
Dfs (I, j );
}


/* For (int I = 0; I <n * m; I ++)
If (p [I] = I)
Ans ++ ;*/
Printf ("% d \ n", ans );
}
Int main ()
{
While (scanf ("% d", & n, & m) = 2)
{
If (n <0 | m <0) break;
Memset (map, 0, sizeof (map ));
For (int I = 0; I <n * m; I ++)
P [I] = I;
For (int I = 0; I <n; I ++)
{
Scanf ("% s", s [I]);
For (int j = 0; j <m; j ++)
Build (I, j );
}
Solve ();
}
Return 0;
}

Related Article

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.