Hdu-1241-Oil Deposits

Source: Internet
Author: User

Q: A map of m * n, where the grid is * or @. For @, the grid is a block that is connected horizontally, vertically, and diagonally, there are multiple @ blocks in total.

 

--> Repeat the simple winter vacation search questions and do not use scanf or getchar for input.


[Cpp] # include <cstdio>
# Include <iostream>
# Include <cstring>
 
Using namespace std;
 
Const int maxn = 100 + 10;
Char MAP [maxn] [maxn];
Int cnt, m, n;
Int dx [] = {-1,-1,-1, 0, 1, 1, 1, 0 };
Int dy [] = {-1, 0, 1, 1, 1, 0,-1,-1 };
Bool vis [maxn] [maxn];
 
Void dfs (int x, int y)
{
Vis [x] [y] = 1;
For (int I = 0; I <8; I ++)
{
Int newx = x + dx [I];
Int newy = y + dy [I];
If (newx> = 0 & newx <m & newy> = 0 & newy <n & MAP [newx] [newy] = '@'&&! Vis [newx] [newy])
Dfs (newx, newy );
}
}
 
Int main ()
{
Int I, j;
While (scanf ("% d", & m, & n) = 2 & m)
{
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
Cin> MAP [I] [j];
 
Cnt = 0;
Memset (vis, 0, sizeof (vis ));
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
{
If (MAP [I] [j] = '@'&&! Vis [I] [j])
{
Cnt ++;
Dfs (I, j );
}
}
Printf ("% d \ n", cnt );
}
Return 0;
}

# Include <cstdio>
# Include <iostream>
# Include <cstring>

Using namespace std;

Const int maxn = 100 + 10;
Char MAP [maxn] [maxn];
Int cnt, m, n;
Int dx [] = {-1,-1,-1, 0, 1, 1, 1, 0 };
Int dy [] = {-1, 0, 1, 1, 1, 0,-1,-1 };
Bool vis [maxn] [maxn];

Void dfs (int x, int y)
{
Vis [x] [y] = 1;
For (int I = 0; I <8; I ++)
{
Int newx = x + dx [I];
Int newy = y + dy [I];
If (newx> = 0 & newx <m & newy> = 0 & newy <n & MAP [newx] [newy] = '@'&&! Vis [newx] [newy])
Dfs (newx, newy );
}
}

Int main ()
{
Int I, j;
While (scanf ("% d", & m, & n) = 2 & m)
{
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
Cin> MAP [I] [j];

Cnt = 0;
Memset (vis, 0, sizeof (vis ));
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
{
If (MAP [I] [j] = '@'&&! Vis [I] [j])
{
Cnt ++;
Dfs (I, j );
}
}
Printf ("% d \ n", cnt );
}
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.