Home > Back-end >  Application competition
Application competition

Time:10-07

On the Lake Counting problem of depth-first code based on the calculation of how many puddles, how much water each pools (i.e., how many W),

Input (the first line of two integer N and M, according to the size of the N * M garden, followed by N lines, each line M characters W is for water, numerical representative without water)

Output in one line, the first number is K a puddle, followed by K integers, each size number is corresponding to the pools (since), there is a space between the number (no Spaces at the end, there is no return)

For example:

Input:

10 12

W234567890WW

1 www567890ww

1234 ww7890ww

1234567890 ww

12345678901 w

12 w4567890w2

1 w3w567890ww

W2W4W678901W

1 w3w5678901w

12 w45678901w



Output:

3 6 9 15
My code
# include
using namespace std;
Char yuan [101] [101].
Int n, m; Int num=0; Int a, b, c;
Void DFS (int x, int y) {
Yuan [x] [y]='. '. Num++;//will replace for W.
//to iterate over eight directions
For (int I=1; I & lt;=1; I++)
{
For (int j=1; J & lt;=1; J++)
{
Int nx=x + I, ny=y + j.//to the x direction, y direction mobile j
If (nx & gt;=0 & amp; & Nx & lt; N& & Ny & gt;=0 & amp; & Ny & lt; M& & Yuan [nx] [ny]=='W')//whether the point within the garden, and whether there is water, if any, DFS
{
DFS (nx, ny); }}}
return; }
Int main ()
{
Int ans=0;
Cin & gt;> N & gt;> m;
for (int i=0; i{
For (int j=0; J{
Cin & gt;> Yuan [I] [j];
}
}
for (int i=0; i{
For (int j=0; J{
If (yuan [I] [j]=='W')
{
DFS (I, j); cout}}}
Cout & lt; Output for June 15 September 3, could you tell me how to order?
  • Related