ZTE holds the Blue Sword Road in the preliminary question--several houses

Source: Internet
Author: User

ZTE holds the Blue Sword Road of the preliminary title-several houses: in order to plan the city, we need to count the number of housing information. As follows, a aerial photograph will be separated into m*n, with the number 0 or 1 to indicate whether a lattice sees the open space or the roof, the adjacent roof belongs to the same house, the roof on the diagonal is not the same house (this is also true), now enter the aerial photograph of the size (m row n column), And then enter the layout of the top view of the rectangle and ask us to output how many houses there are in this rectangular space.

Test Case:

TestCase 1:

Input:

5 5

0 0 0) 0 0

0 1 0) 1 0

0 1 0) 1 0

0 1 1) 1 0

0 0 0) 0 0

Expected Return Value:

1

TestCase 2:
Input:
5 6

1 1 0 0 1 0

0 1 1 1 0 0

1 0 0 1 0 0

0 0 0 0 1 1

0 0 0 0 1 1

Expected Return Value:

4
This topic is actually very simple, as long as a set to store the value is 1 of the point can be my idea is to use a set (pair (Int,int)) to store data, Then each time to find the adjacent if there is the deletion of adjacent to this point and extended also meet the adjacent conditions for the point of 1, and then every count+1, determine whether set is empty, not empty, and then delete again.
Paste code:

#include <iostream> #include <set> using namespace std;
BOOL Search (set<pair<int, int>> temp, int num_1, int num_2);
void ERAs (Set<pair<int, int>> &temp, int num_1, int num_2, int m, int n);
    int main () {int m, n;
        while (Cin >> m >> N) {int count = 0;
        Set<pair<int, Int>> temp;
        int **arr = new Int*[m];
        for (int i = 0; i < m; i++) arr[i] = new Int[n];
                for (int i = 0, i < m; i++) {for (int j = 0; J < N; j + +) {cin >> arr[i][j];
            if (arr[i][j] = = 1) temp.insert (Pair<int, Int> (i, j)); }}//This is the code for the House count while (!temp.empty ()) {eras (temp, temp.begin ()->first, Temp.begin (
            )->second, M, N);
        count++;
        } for (int i = 0; i < m; i++) delete[] arr[i];
        Delete[] arr; cout << "Number of houses" << count << Endl;
} return 0; } bool Search (Set<pair<int, int>> temp, int num_1, int num_2) {//This is to determine if the point adjacent to it is in set if (!temp.empty ()
    &&temp.find (Pair<int, int> (num_1, num_2))! = Temp.end ()) return true;
return false; } void ERAs (Set<pair<int, int>> &temp, int num_1, int num_2, int m, int n) {//recursively delete adjacent, but I am lazy, non-recursive writing is not written
    .
    if (Temp.empty ()) return;
    Temp.erase (Pair<int, int> (num_1, num_2));
    if (num_2>0 && Search (temp, num_1, num_2-1)) ERAs (temp, num_1, num_2-1, M, N);
    if (num_2<n-1 && Search (temp, num_1, num_2 + 1)) ERAs (temp, num_1, num_2 + 1, m, N);
    if (num_1>0 && Search (temp, num_1-1, num_2)) ERAs (temp, num_1-1, num_2, M, N);
if (num_1<m-1 && Search (temp, num_1 + 1, num_2)) ERAs (temp, num_1 + 1, num_2, M, N);

 }

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.