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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:56:47+00:00 2026-06-14T00:56:47+00:00

I have a piece of code: private void colorize(int color, int x, int y)

  • 0

I have a piece of code:

private void colorize(int color, int x, int y) {
    visited[x][y] = true;
    if (x + 1 < d)
        if (board[x + 1][y] == board[x][y] && visited[x + 1][y] == false)
            colorize(color, x + 1, y);
    if (x - 1 >= 0)
        if (board[x - 1][y] == board[x][y] && visited[x - 1][y] == false)
            colorize(color, x - 1, y);
    if (y + 1 < d)
        if (board[x][y + 1] == board[x][y] && visited[x][y + 1] == false)
            colorize(color, x, y + 1);
    if (y - 1 >= 0)
        if (board[x][y - 1] == board[x][y] && visited[x][y - 1] == false)
            colorize(color, x, y - 1);
    board[x][y] = color;
}

I call it: colorize(int random, int 0, int 0). This gives me stackoverflow even for a small table (20×20). How can I do this without recursion?

  • 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-06-14T00:56:48+00:00Added an answer on June 14, 2026 at 12:56 am

    The code as given seems fine.

    There is a question as to what d is, but I’m assuming that it is correctly the width of a square grid.

    It’s possible that you have a problem in whatever code calls this, but the code you have given us should not have a stack overflow.


    Building off of Måns Rolandi Danielsson’s answer, here is one that doesn’t use the explicit stack, but builds one on the heap. My java is extremely rusty, but this should work. If anyone has fixes for this code, feel free to fix it.

    Instead of getting a stack overflow, at large table sizes I get a java.lang.OutOfMemoryError: Java heap space error instead. You could probably use a set or some other data structure (rather than a linked list) to optimize memory usage.

    import java.util.Queue;
    import java.util.LinkedList;
    
    class Pair<L,R> {
        private L l;
        private R r;
    
        public Pair(L l, R r){
            this.l = l;
            this.r = r;
        }
    
        public L getL(){ return l; }
        public R getR(){ return r; }
    }
    
    
    public class HelloW {
        static int d = 20;
        static boolean[][] visited = new boolean[d][d];
        static int[][] board = new int[d][d];
    
        static Queue<Pair<Integer, Integer>> Q = new LinkedList<Pair<Integer, Integer>>();
    
        static void colorize(int color, int orig_x, int orig_y) {
            Q.add(new Pair<Integer, Integer>(orig_x, orig_y));
    
            while (Q.isEmpty() == false) {
                Pair<Integer,Integer> foo = Q.remove();
                int x = foo.getL();
                int y = foo.getR();
                int old_color = board[x][y];
    
                visited[x][y] = true;
                board[x][y]  = color;
    
                if (x + 1 < d)
                    if (board[x + 1][y] == old_color && visited[x + 1][y] == false)
                        Q.add(new Pair<Integer, Integer>(x+1, y));
                if (x - 1 >= 0)
                    if (board[x - 1][y] == old_color && visited[x - 1][y] == false)
                        Q.add(new Pair<Integer, Integer>(x-1, y));
                if (y + 1 < d)
                    if (board[x][y + 1] == old_color && visited[x][y + 1] == false)
                        Q.add(new Pair<Integer, Integer>(x, y+1));
                if (y - 1 >= 0)
                    if (board[x][y - 1] == old_color && visited[x][y - 1] == false)
                        Q.add(new Pair<Integer, Integer>(x, y-1));
            }
        }
    
        public static void main (String[] args) {
            colorize(1, 0, 0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code: private void myFunc(){ obj = doSomething(); //If value
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I have this piece of code: private void prepareContent() { log.info(do something); // success?
If I have a piece of code such as this: private void DoOperations(//method passed
I have this small piece of code. private void Application_Startup(object sender, StartupEventArgs e) {
I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var
I have this piece of LINQ code private static void Original() { using (var
I have this piece of code in my Silverlight project: private void button1_Click(object sender,
I have such piece of code: namespace NetHacking { interface IPlugin { void Execute(PluginData
Here I have piece of code, showing example of how I want to update

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.