This question already has an answer here:
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;
}
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
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
I know to setup font color in Java of AlertDialog button we can do like
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'
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