Multi-question deep Priority Search

Source: Internet
Author: User

Find the first in-depth Priority Search question and find the number of eight connected blocks in the figure.

# Include <iostream>
# Include <cstring>
Using namespace STD;
# Define x 100
Int visit [x] [x];
Int A [x] [x];

Void DFS (int x, int y) // deep Priority Search
{
If (visit [x] [Y] |! A [x] [Y]) // if it is white or traversed
Return;
Visit [x] [Y] = 1; // mark the traversal
DFS (x-1, Y-1); DFS (x-1, Y); DFS (x-1, Y + 1); // recursively traverse the surroundings
DFS (x, Y-1); DFS (X, Y + 1 );
DFS (x + 1, Y-1); DFS (x + 1, Y); DFS (x + 1, Y + 1 );
}

Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int N, I, j;
While (CIN> N)
{
Memset (A, 0, sizeof ());
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
Cin> A [I] [J];
Int COUNT = 0;

Memset (visit, 0, sizeof (visit); // expand the boundary to make it white.
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
If (! Visit [I] [J] & A [I] [J])
{
Count ++; // count
DFS (I, j); // deep Priority Search
}
Cout <count <Endl;
}
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.