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 232319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:00:26+00:00 2026-05-11T20:00:26+00:00

I am having some stackoverflow issues and hopefully someone can give me some insight

  • 0

I am having some stackoverflow issues and hopefully someone can give me some insight into a non/less-recursive solution.

Ident[][] map = ...

private int explore(Ident item, int xcoord, int ycoord) {
    if ((map[xcoord][ycoord] == null) || !map[xcoord][ycoord].equals(item))
             return 0;

    map[xcoord][ycoord] = null;
    int sumX, sumY, counter = 1;
    item.translate(xcoord, ycoord);

    for (int y = -1; y <= 1; y++)
        for (int x = -1; x <= 1; x++) {
             sumX = x + xcoord;
             sumY = y + ycoord;
            if (((y != 0) || (x != 0)) && (sumX >= 0) && (sumX < map.length) && 
                     (sumY >= 0) && (sumY < map.[0].length))
                     counter += explore(item, sumX, sumY);
             }
        }
    }
    return counter;
}

This method is given a 2-dimensional array of Ident Objects, a target Ident and a starting position within the array. It recursively traverses the array
counting how large of a continuous area the Ident occupies. It also centers the inputted Ident item in the middle of the area.

By looping through the map array and calling the explore method on any non null elements I can construct an array of Ident items centered in their areas, and with sizes relative to their areas.

One can see that with anything but small maps, the stack will overflow.

Anyone have an alternate method of accomplishing the same task? Or some insight to help me find one?

  • 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-11T20:00:26+00:00Added an answer on May 11, 2026 at 8:00 pm

    This take me back to the mid-80’s. Any flood fill algorithm is going to require a certain amount of state. I unfortunately don’t remember the algorithms. What’s efficient for a large expanse probably isn’t going to efficient for a maze.

    To avoid recursion, instead of recursing, just add the data that you would have called to a stack. Loop around popping the next unexplored coordinate from the top of the stack. Using a stack rather than a FIFO queue improves locality a bit, although it probably isn’t going to make a huge difference here.

    private int explore(Ident item, int xcoord, int ycoord) {
        int counter = 0;
    
        Queue<Point> stack = Collections.asLifoQueue(new ArrayDeque<Point>());
        stack.add(new Point(xcoord, ycoord));
    
        while (!stack.isEmpty()) {
            Point point = stack.remove();
            xcoord = point.x;
            ycoord = point.y;
    
            if (!item.equals(map[xcoord][ycoord])) {
                continue;
            }
    
            ++counter;
            map[xcoord][ycoord] = null;
            item.translate(xcoord, ycoord);
    
            for (int y = -1; y <= 1; y++)
                for (int x = -1; x <= 1; x++) {
                     int sumX = x + xcoord;
                     int sumY = y + ycoord;
                     if (
                         !(x == 0 && y == 0) &&
                         0 <= sumX && sumX < map.length &&
                         0 <= sumY && sumY < map[0].length
                     ) {
                         stack.add(new Point(sumX, sumY));
                     }
                }
            }
        }
        return counter;
    }
    

    In the best traditions of stackoverflow this has not seen a compiler. (It’s also retains much of the original algorithm.)

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

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Note that an excellent source of information for Microsoft Office… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Comma at the end of a line of normal code.… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Try this: awk 'NR==1{sub(/^\xef\xbb\xbf/,"")}{print}' INFILE > OUTFILE On the first… May 12, 2026 at 12:40 am

Related Questions

Having read an existing post on stackoverflow and done some reading around on the
Eric Willis' response to this question: https://stackoverflow.com/questions/563041/receive-emails-in-a-net-service-c prompted me to start thinking of creating
I am fairly new at using the ASP.NET MVC framework and was hoping that
I am having some difficulty accessing the value of the selected radio button in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.