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

  • Home
  • SEARCH
  • 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 3985866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:56:34+00:00 2026-05-20T05:56:34+00:00

Assume I have a matrix something like this : 1 1 1 0 0

  • 0

Assume I have a matrix something like this :

1 1 1 0 0 0
1 1 1 0 0 1
0 0 0 0 0 1

If two ‘1’ are next to each other (horizontally and vertically only) and therefore belong to the same area. I need to find how many of these areas are there in a matrix. You can see that there’s two areas of ‘1’ in this matrix. I’ve been trying to solve this for hours now but the code gets really big and disgusting. Are there any algorithms out there I could adopt for this problem ?

  • 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-20T05:56:34+00:00Added an answer on May 20, 2026 at 5:56 am

    If you don’t really care about:

    • Keeping the input matrix intact

    • Performance and optimisations

    then here’s my take on this problem in C:

    #include <stdio.h>
    
    #define X       8
    #define Y       4
    
    #define IGN     1
    
    int A[Y][X] = {
            { 1, 1, 1, 0, 0, 0, 0, 1 },
            { 1, 1, 1, 0, 0, 1, 0, 1 },
            { 0, 0, 0, 0, 0, 1, 0, 1 },
            { 0, 0, 0, 0, 0, 1, 1, 1 },
    };
    
    int blank(int x, int y) {
            if ((x < 0) || (x >= X) || (y < 0) || (y >= Y) || (A[y][x] == 0))
                    return 0;
    
            A[y][x] = 0;
    
            return 1 + blank(x - 1, y) + blank(x + 1, y) + blank(x, y - 1) + blank(x, y + 1);
    }
    
    int main() {
            int areas = 0;
    
            int i, j = 0;
    
            for (i = 0; i < X; ++i)
                    for (j = 0; j < Y; ++j)
                            if (A[j][i] == 1)
                                    if (blank(i, j) > IGN)
                                            areas++;
    
            printf("Areas: %i\n", areas);
    
            return 0;
    }
    

    Once it encounters a 1, it recursively expands over all neighbouring 1 elements, counting them and turning them into 0. If the size of the area is larger than IGN, then it is taken into account.

    Notes:

    • If you need to keep the original matrix, you would have to use a copy for input.

    • If you plan on using this, you should probably turn this code into functions that get the array and its size from their parameters. Global variables and algorithm implementations in main() should be avoided, but I made an exception in this case because it reduces the complexity of the code and allows the algorithm to be more clear.

    • With IGN set to 1, lone elements are not considered an area. Changing IGN to 0 will get those too.

    • The if (A[j][i] == 1) conditional in the loop is not strictly necessary, but it serves as a minor optimisation by avoiding unnecessary function calls, although compiler optimisations probably make it redudant.

    • You can easily modify this to get a listing of the elements in each area.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a matrix that looks something like this: {{foobar, 77},{faabar, 81},{foobur, 22},{faabaa,
Assume I have one matrix with two columns. Every column has thousands of values.
Assume I have two tables, Student Test Id Name TestId Type StudentId -- ----
Assume I have a set of numbers like 1,2,3,4,5,6,7 input as a single String
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
I have the following problem Assume that we have a 9*8 matrix A matrix
Let's assume that I have a vector r <- rnorm(4) and a matrix W
let's assume i have the following logical matrix: log = [1 1 0; 0
I want to do a Sparse Matrix, Dense Vector multiplication. Lets assume the only

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.