Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:58:15+00:00 2026-05-19T13:58:15+00:00

I need an idea how to effectively find areas below marked with 0 in

  • 0

I need an idea how to effectively find areas below marked with 0 in two-dimensional array. It should be noted that there are other areas, such as this picture shows one of two who owns coordinate (0.0) and the other owns coordinate (21.3).

00000000000111110000111
00000000001111110000111
00000000011111100000111
00000000000111000001101
00000000011100000011101
00000001111100001111001
00000111111111011111001
00000001111100001111001
00000000010000000011001
00000000000000000001111

Of course a real array will be much larger.
Recursive version that goes to all sides and stops at mark 1 or array side isn’t fast enough.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:58:16+00:00Added an answer on May 19, 2026 at 1:58 pm

    It looks like you’re looking for a flood-fill algorithm. The wikipedia page I linked lists a few algorithms which may be faster than the obvious recursive method.

    Flood-fill will be a good match if the areas you’re looking for are small compared to the entire array, and you don’t need to search for all of them. If you need to know about most or all of them, then computing them all in a single shot using a union-merge based connected component labeling algorithm may be a better choice. Here’s some code that implements such an algorithm (note that I’ve altered it to run in a single pass):

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <vector>
    #include <map>
    
    const char *data[] = {
    "00000000000111110000111",
    "00000000001111110000111",
    "00000000011111100000111",
    "00000000000111000001101",
    "00000000011100000011101",
    "00000001111100001111001",
    "00000111111111111111001",
    "00000001111100001111001",
    "00000000010000000011001",
    "00000000000000000001111",
    NULL
    };
    
    struct label {
    private:
        int index;
        int rank;
        label *parent;
    public:
        label ()
            : index(-1), rank(0), parent(this)
        { }
    
        int getIndex(int &maxIndex) {
            if (parent != this)
                return find()->getIndex(maxIndex);
    
            if (index < 0)
                index = maxIndex++;
            return index;
        }
    
        label *find() {
            if (parent == this)
                return this;
    
            parent = parent->find();
            return parent;
        }
    
        label *merge(label *other)
        {
            label *xRoot = find();
            label *yRoot = other->find();
    
            if (xRoot == yRoot)
                return xRoot;
    
            if (xRoot->rank > yRoot->rank) {
                yRoot->parent = xRoot;
                return xRoot;
            } else {
                xRoot->parent = yRoot;
                if (xRoot->rank == yRoot->rank)
                    yRoot->rank++;
                return yRoot;
            }
        }
    };
    
    int width, height;
    
    int main() {
        for (int i = 0; data[0][i]; i++)
            width = i + 1;
        for (int i = 0; data[i]; i++) {
            height = i + 1;
        }
    
        std::vector<std::vector<unsigned short> > lblinfo;
        lblinfo.resize(height, std::vector<unsigned short>(width, 0));
    
        std::vector<label *> labels;
        labels.push_back(NULL); // 0 is used as an unassigned flag
    
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (data[y][x] == '1')
                    continue;
    
                // Try to find a neighboring label
                unsigned short lblid = 0;
                if (x != 0 && lblinfo[y][x-1] != 0)
                    lblid = lblinfo[y][x-1];
    
                // merge with cells above
                if (y != 0) {
                    for (int x2 = x - 1; x2 <= x + 1; x2++) {
                        if (x2 < 0)
                            continue;
                        if (x2 >= width)
                            continue;
    
                        unsigned short otherid = lblinfo[y - 1][x2];
                        if (!otherid)
                            continue;
    
                        if (!lblid)
                            lblid = otherid;
                        else {
                            labels[lblid]->merge(labels[otherid]);
                        }
                    }
                }
    
                if (!lblid) {
                    // assign a new label
                    lblid = labels.size();
                    labels.push_back(new label);
                }
                lblinfo[y][x] = lblid;
            }
        }
    
        // Assign indices to the labels by set and print the resulting sets
        int maxindex = 0;
        static const char chars[] = "abcefghijklmnopqrstuvwxyz";
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                unsigned short labelid = lblinfo[y][x];
                if (labelid == 0) {
                    putchar(data[y][x]);
                    continue;
                }
    
                label *label = labels[labelid];
                int idx = label->getIndex(maxindex);
                if (idx >= sizeof(chars) - 1) {
                    printf("\n\n Too many labels to print!\n");
                    exit(1);
                }
    
                putchar(chars[idx]);
            }
            printf("\n");
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically need to change the value that - admin_url() returns any idea?
I've gotten used to the idea that if I want/need to use alpha-trans PNGs
The idea is that a game might need something fast but also simplistic. Mainly
I need an idea for an efficient index/search algorithm, and/or data structure, for determining
I need to get a file from sourcesafe database programmatically. Any idea of how
I need to implement a SOAP Web Service on Jboss Seam 2.1.0. The idea
I need to archive data from a CRM system. Does anyone have any idea
I have this idea for a free backup application. The largest problem I need
Has anyone an idea how pipebytes.com works ? I need to implement similar system
Here is the idea I would like to develop. I need to have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.