Add all neighbors (with given true value) in 2d array efficiently while avoiding nulls [duplicate]

214
January 29, 2018, at 3:05 PM

This question already has an answer here:

  • More efficient way to check neighbours in a two-dimensional array in Java 7 answers
  • Programming Logic: How to Check for Neighbors in a Grid? 6 answers

I have a boolean array[25][25] and that represents a grid. If a box in the grid is true, it is yellow, and if it is false, it is black. On paper I have been trying to figure out the most efficient way to find all neighbors that are true. In doing so, I must first check that the row != 0, row != 24, column != 0, column != 24 before I check the basic case. It is very long and includes what seems like too many if statements... is there a more efficient way that I am not seeing? Thanks.

This is what I have currently which works. I am just looking to see if there is a more compact way of creating a method that does the same thing.

private int findTrueNeighbors(int r, int c){
        int neighbors = 0;
        if (r > 0 && r < arr.length - 1) {
            // basic check
            if (c > 0 && c < arr[r].length) {
                // basic check
                if (arr[r-1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r-1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
                if (arr[r+1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else if ( c > 0) {
                // SPECIAL CASE: far right column
                if (arr[r-1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
                if (arr[r+1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else {
                // SPECIAL CASE: far left column
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r-1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
            }
        } else if (r > 0) {
            // SPECIAL CASE: in bottom row
            if (c > 0 && c < arr[r].length) {
                // basic check
                if (arr[r-1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r-1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else if ( c > 0) {
                // SPECIAL CASE: far right column
                if (arr[r-1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else {
                // SPECIAL CASE: far left column
                if (arr[r-1][c] == true) {
                    neighbors++;
                }
                if (arr[r-1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
            }
        } else {
            // SPECIAL CASE: in top row
            if (c > 0 && c < arr[r].length) {
                // basic check
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
                if (arr[r+1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else if ( c > 0) {
                // SPECIAL CASE: far right column
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
                if (arr[r+1][c-1] == true) {
                    neighbors++;
                }
                if (arr[r][c-1] == true) {
                    neighbors++;
                }
            } else {
                // SPECIAL CASE: far left column
                if (arr[r][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c+1] == true) {
                    neighbors++;
                }
                if (arr[r+1][c] == true) {
                    neighbors++;
                }
            }
        }
        return neighbors;
    }
Rent Charter Buses Company
READ ALSO
Using setFill with a variable?

Using setFill with a variable?

I'm trying to pass a variable in Java, I need help getting it to work, I've included all relevant code below, there's threejava file's i'm working with

219
How to set AlertDialog button font color in Kotlin

How to set AlertDialog button font color in Kotlin

I know to setup font color in Java of AlertDialog button we can do like

526
Could java library &#39;colt&#39; replace with python library &#39;numpy&#39;? How is the performance of colt?

Could java library 'colt' replace with python library 'numpy'? How is the performance of colt?

I don't really like implement models using python, but numpy is really goodThus currently I'm seeking an equivalent of numpy in java and somebody recommended 'colt'

209
listview not displaying text custom adapter

listview not displaying text custom adapter

I am trying to use a custom adapter to display some text to a listview and when i run the app on my phone it crashes

183