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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:19:03+00:00 2026-06-03T09:19:03+00:00

public boolean isConnected(int row, int col) { //helper Method int i; int j; if

  • 0
public boolean isConnected(int row, int col) {    //helper Method 
        int i;
        int j;

        if (BOARD[row][col].isEmpty())
            return false;
        for (i = row; i > 0; i--)
            if (hasRed(i, col))
                return true;
            else if (isEmpty(i, col))
                break;
        for (i = row; i < ROWS; i++)
            if (hasRed(i, col))
                return true;
            else if (isEmpty(i, col))
                break;
        for (i = col; i < COLS; i++)
            if (hasRed(row, i))
                return true;
            else if (isEmpty(row, i))
                break;
        for (i = col; i > 0; i--)
            if (hasRed(row, i))
                return true;
            else if (isEmpty(row, i))
                break;
        for (i = row, j = col; i > 0 && j < COLS; i--, j++)
            if (hasRed(i, j))
                return true;
            else if (isEmpty(i, j))
                break;
        for (i = row, j = col; i < ROWS && j > 0; i++, j--)
            if (hasRed(i, j))
                return true;
            else if (isEmpty(i, j))
                break;
        for (i = row, j = col; i > 0 && j > 0; i--, j--)
            if (hasRed(i, j))
                return true;
            else if (isEmpty(i, j))
                break;
        for (i = row, j = col; i < ROWS && j < COLS; i++, j++)
            if (hasRed(i, j))
                return true;
            else if (isEmpty(i, j))
                break;

        return false;

    }    

//The main Method which may cause an exception the algorithm after many test cases tried always terminates if there is a true solution but may or may not terminate for a false one the reason I guess is that the recursion step does not simplify the original problem but actually expands it ! in order to have a chance to get the correct solution, the problem is that in certain conditions which certainly should return false the algorithm cannot terminate as it keep checking previously solved subproblems and so on.

public boolean isConnected2(int rowCurr, int colCurr) {

            if (rowCurr >= ROWS || rowCurr < 0 || colCurr < 0 || colCurr >= COLS) 
                return false;                  //Base case 1 reached bounds of the 2d array
            if (isEmpty(rowCurr, colCurr))
                return false;
            if (isConnected(rowCurr, colCurr)) // base case 2 current piece is connected according to the method above
                return true;

            else {
                return     isConnected2(rowCurr + 1, colCurr + 1)                                          
                        || isConnected2(rowCurr - 1, colCurr - 1)
                        || isConnected2(rowCurr + 1, colCurr - 1)
                        || isConnected2(rowCurr - 1, colCurr + 1)
                        || isConnected2(rowCurr - 1, colCurr - 1)
                        || isConnected2(rowCurr + 1, colCurr)
                        || isConnected2(rowCurr - 1, colCurr)
                        || isConnected2(rowCurr, colCurr + 1) 
                        || isConnected2(rowCurr, colCurr - 1);
// if any of the above calls returns true we are done


            }

        }

The thing is I am not sure how to handle the special cases that cause the algorithm to recurse to infinitely, And I am sure due to the nature of(||) operator that if there is a true solution IT WILL TERMINATE. So is it not better in this case to just handle the StackOverFlow error and treat it as a false return for the method ??

  • 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-03T09:19:05+00:00Added an answer on June 3, 2026 at 9:19 am

    Don’t EVER leave a stack overflow in your program. Letting it happen and catching it is a huge burden on the JVM. Fix your recursion so that it can’t happen.

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

Sidebar

Related Questions

public boolean requestRouteToHost (int networkType, int hostAddress) This method in ConnectivityManager if used with
Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder
myEdit1.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { myEdit2.setText(); return
I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
I have the following method: @Override public boolean myMethod() { // do stuff }
I add this code in my activity public boolean onKeyDown(int keyCode, KeyEvent event) {
i have written a method in java like this public boolean ADD(String ID,String Name,String
public class CustomProtocolDecoder extends CumulativeProtocolDecoder{ byte currentCmd = -1; int currentSize = -1; boolean
public boolean playing=false; private void prepareTTSEngine() { try { synthesis = SpeechSynthesis.getInstance(this); synthesis.setSpeechSynthesisEvent(new SpeechSynthesisEvent()
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Log.d(LOG_TAG,

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.