Simplification of successive 'if' statements in Java [duplicate] - java

This question already has answers here:
Find elements surrounding an element in an array
(8 answers)
Closed 6 years ago.
I have a series of if statements, as shown below:
if (board[x+1][y]==true) {
ar+=1;
}
if (board[x][y+1]==true) {
ar+=1;
}
if (board[x-1][y]==true) {
ar+=1;
}
if (board[x][y-1]==true) {
ar+=1;
}
if (board[x+1][y+1]==true) {
ar+=1;
}
if (board[x+1][y-1]==true) {
ar+=1;
}
if (board[x-1][y+1]==true) {
ar+=1;
}
if (board[x-1][y-1]==true) {
ar+=1;
}
Is there a way to simplify/condense these statements with Java?

Simply loop around the position that you care about. Skipping the center of the "box".
Tip: You access a 2D array by row then column, or [y][x] (at least, that's how you'd translate the board from looking at the code).
// int x, y; // position to look around
for (int xDiff = -1; xDiff <= 1; xDiff++) {
for (int yDiff = -1; yDiff <= 1; yDiff++) {
if (xDiff == 0 && yDiff == 0) continue;
if (board[y+yDiff][x+xDiff]) {
ar += 1;
}
}
}
Beware - Out of bounds exception is not handled

The following would be a more visual equivalent, easily extensible to other shapes of the area of interest. Note ternary operators, ?: which are necessary in Java to convert bools to ints.
ar += (board[x-1][y-1]?1:0) + (board[x-1][y]?1:0) + (board[x-1][y+1]?1:0);
ar += (board[x+0][y-1]?1:0) + (board[x+0][y+1]?1:0);
ar += (board[x+1][y-1]?1:0) + (board[x+1][y]?1:0) + (board[x+1][y+1]?1:0);

In addition to the answers you already have, you can also use enhanced for loops (and re-use the range as it is the same for both).
int[] range = { -1, 0, 1 };
for (int i : range) {
for (int j : range) {
if ((i != 0 || j != 0) && board[i][j]) {
ar++;
}
}
}

This code should give the same result (probably you want to check that you are always in the bounds of the matrix
for(int i=-1; i<=1; i++) {
for(int j=-1; j<=1; j++) {
if((i != 0 || j != 0) && board[x+i][y+j]) {
ar++;
}
}
}

You can simplify as follows as well :
for(int i = x-1;i<=x+1;i++) {
for(int j=y-1;j<=y+1;j++) {
if(i==x && j==y) continue;
if (board[i][j]) {
ar+=1;
}
}
}

Looks like you're testing surrounding squares apart from (x, y) itself. I'd use a loop to maintain a counter, with extra step to exclude the (x, y) centre cell.
for (int xTest = x - 1; xTest <= x + 1; xTest++) {
for (int yTest = y - 1; yTest <= y + 1; yTest++) {
if (xTest == x && yTest == y) {
continue;
}
if (board[xTest][yTest]) {
ar += 1;
}
}
}
Also, note the == true is unnecessary. Boolean statements are first class citizens in Java.

Related

How to get all the neihbors neighbors of a point in a 2d array?

I am trying to get all the neighbors of a combination of simple one character strings in a 2d array. Meaning, my output looks like this currently in a 3x5:
A B C
D E F
A S D
F S A
G S A
So the neighbor of (1,0) should be = A B E S A .
Currently I have the following:
public void getNeighborsOfPoint(int x, int y) {
for (int xx = -1; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx == 0 && yy == 0) {
continue; // You are not neighbor to yourself
}
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
if (isOnMap(x + xx, y + yy)) {
System.out.println(grid[x+xx][y+yy]);
}
}
}
public boolean isOnMap(int x, int y) {
return x >= 0 && y >= 0 && x < length && y < width;
}
However it is only returning A E A in the example I provided.(it is not returning the ones cross-wise)
What is the right code to make it work? Note that the input will not always be 3 x 5. It may be a lot of different combination of x and y s.
The diagonals aren't included because of this code:
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
When it's on the diagonal Math.abs(xx) == 1 && Math.abs(yy) == 1. So their sum will be greater than 1. You're skipping over the diagonals by having this code here.
The reason you're not getting the diagonals in your current group is that second if statement. You need to cover, for example, (2, 1) which is when xx is 1 and yy is 1. But abs(1) + abs(1) = 2 and 2 > 1, so you don't include it.
As a refactoring exercise, it might be a little cleaner if you have the inside of that for loop simplified to one conditional.
if (expression) {
continue
};
// other stuff
is equivalent to
if (!expression) {
// other stuff.
}
And for you, expression (in psuedocode) is not(xx=0 and yy=0) and isOnMap(xx, yy)
In a loop, the continue keyword means that you will skip to the next iteration of the loop. In your case, you have :
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
if (isOnMap(x + xx, y + yy)) {
System.out.println(grid[x+xx][y+yy]);
}
So if the first condition is verified, you will not print any answer, meaning that your program won't consider A(xx, yy) to be a neighbord.
In your ABESA example, B and S are ignored because of this.
If you want to use 2d arrays with variable number of rows and columns you have to pass them as parameters in your's isOnMap method like below:
public static boolean isOnMap(int x, int y, int length, int width) {
return x >= 0 && y >= 0 && x < length && y < width;
}
You can handle the special cases of your 2d array (when one or both rownumber and columnnumber of your element are equal to 0) rewriting your getNeighborsOfPoint method in this way:
public static void getNeighborsOfPoint(int x, int y, char[][] grid) {
final int length = grid.length;
final int width = grid[0].length;
if (isOnMap(x, y, length, width)) {
for (int i = Math.max(0, x - 1); i < Math.min(length, x + 2); ++i) {
for (int j = Math.max(0, y - 1); j < Math.min(width, y + 2); ++j) {
if (i != x || j != y) {
System.out.println(grid[i][j]);
}
}
}
}
}

Filter the period in Char[]

The valid sudoku problem on Leetcode is solved, however, there is still a little question regarding the '.', which I fail to filter until I try another way.
Check out the comment parts regarding (1) & (2), (1) = the correct way to filter period by using continue; if '.' is found. (2) = is the wrong way which I used before, it will only allow digits to get passed for the following if statements.
Assume 1~9 digits and '.' will be the only inputs.
I just need someone to help me analyze the difference between these 2 ways, so I can learn from the mistakes.
Thank you for the help!
public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board.length > 9 || board[0].length > 9 || board == null) {
return false;
}
boolean[] brain;
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
// ------- (1) Begin -------
if (board[x][y] == '.') {
continue;
}
if (brain[board[x][y] - '1']) {
return false;
} else {
brain[board[x][y] - '1'] = true;
}
// ------- (1) End -------
// statments (1) above is the correct one, I used to use code below:
/*
// ------- (2) Begin -------
if (board[x][y] != '.') {
if (brain[board[x][y] - '1']) {
return false;
} else {
brain[board[x][y] - '1'] = true;
}
}
// ------- (2) Begin -------
*/
// which failed for not filter out '.' properly
// so I changed to filter '.' out by using continue;
}
}
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
if (board[y][x] == '.') {
continue;
}
if (brain[board[y][x] - '1']) {
return false;
} else {
brain[board[y][x] - '1'] = true;
}
}
}
for (int block = 0; block < 9; block++) {
// Reset brain
brain = new boolean[9];
for (int r = block / 3 * 3; r < block / 3 * 3 + 3; r++) {
for (int c = block % 3 * 3; c < block % 3 * 3 + 3; c++) {
if (board[r][c] == '.') {
continue;
}
if (brain[board[r][c] - '1']) {
return false;
} else {
brain[board[r][c] - '1'] = true;
}
}
}
}
return true;
}
}
The two methods you use to skip dots on your Sudoku board are equivalent under the assumption that the digits 1 thru 9 and the dot are your only input.
I have tried to verify this with two different Sudoku boards, and both methods returned the same result. As I believe the two methods to do the same I think you will be hard pressed to find a board that shows differing results for each method.

ArrayIndexOutOfBoundsException - (36. Valid Sudoku on Leetcode)

all.
I am working on this problem which comes from Leetcode.
Here is the link if anyone want to see the problem: https://leetcode.com/problems/valid-sudoku/
I think I almost solve this problem, but something wrong with the "java.lang.ArrayIndexOutOfBoundsException" which I used capital comment in my code to mark it out, ==> "if (brain[((int) board[x][y]) - 1])"
I checked it many times and I think the indexes should be perfectly fine within 0~8 by doing "for( int i = 0, i < 9; i++)" and I could not find the reason.
I guess this must be easy stupid question, but I kinda spend a lot of time to find it out. Can someone help me out?
BIG THANKS to whoever help!
public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board.length > 9 || board[0].length > 9 || board == null) {
return false;
}
boolean[] brain;
// 9 digits are corresponding to 1 ~ 9 ==> 0 - 1, 1 - 2 ... 8 - 9
// ex: brain[2] is checking for digit "3" , so using brain[3-1] to check "3"
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
if (board[x][y] != '.') {
// THE NEXT LINE IS THE PROBLEM!!!
if (brain[((int) board[x][y]) - 1]) {
// my condition failed by using:
// brain[board[x][y] - 1]
// other's condition Partially passed by using
// (still failed for final submission:
// brain[(int) (board[x][y] - '1')]
// need to compare the difference
return false;
} else {
brain[((int) board[x][y]) - 1] = true;
}
}
}
}
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
if (board[x][y] != '.') {
if (brain[((int) board[y][x]) - 1]) {
return false;
} else {
brain[((int) board[y][x]) - 1] = true;
}
}
}
}
for (int block = 0; block < 9; block++) {
// Reset brain
brain = new boolean[9];
for (int r = block / 3 * 3; r < block / 3 * 3 + 3; r++) {
for (int c = block % 3 * 3; c < block % 3 * 3 + 3; c++) {
if (board[r][c] != '.') {
if (brain[((int) board[r][c]) - 1]) {
return false;
} else {
brain[((int) board[r][c]) - 1] = true;
}
}
}
}
}
return true;
}
}
You are trying to cast a char as int. Try to manually cast these values and you will notice that 0=48 and 9=57, which i guess should be the cause of your exception.
You might want to check if the value in there represents a number and get the numeric value of the char by using the Character#getNumericValue method.
if (Character.isDigit(board[x][y]) &&
brain[Character.getNumericValue(board[x][y])) - 1]) {
...
}

Java ArrayIndexOutOfBounds Error

So I have an array Canvas[256][256] that has a random index Canvas[r][r] (r being random) set to 1. I then want to loop through the array to see exactly which index is not 0, and then pick a random spot (above, below, left, right) and set that one to 1 as well. It works perfectly fine through the first loop, but then gives me an array out of bounds error after that.
public static void checkPopulation() {
for(int x = 0; x < Canvas.length; x++) {
for(int y = 0; y < Canvas.length; y++) {
if(Canvas[x][y] != 0) {
particleDiffusion(x, y);
}
}
}
}
public static void particleDiffusion(int x, int y) {
Random r = new Random();
if(r.nextInt(3) == 0) {
Canvas[x+1][y] = 255;
} else if(r.nextInt(3) == 1) {
Canvas[x][y+1] = 255;
} else if(r.nextInt(3) == 2) {
Canvas[x-1][y] = 255;//THIS IS WHERE ERROR IS POINTING
} else if(r.nextInt(3) == 3) {
Canvas[x][y-1] = 255;
}
if(stepsTaken < diffusionStep) {
checkPopulation();
} else {
System.out.println("done");
return;
}
}
can somebody help me with what I am doing wrong? And why it loops through once and then gives the error?
You check if x and y are less than Canvas.length, then you use x+1 and y+1 as array indexes, that's the reason. The fixed code is below.
public static void checkPopulation() {
for(int x = 0; x < Canvas.length; x++) {
for(int y = 0; y < Canvas.length; y++) {
if(Canvas[x][y] != 0) {
particleDiffusion(x, y);
}
}
}
}
public static void particleDiffusion(int x, int y) {
Random r = new Random();
if(r.nextInt(3) == 0) {
if( x+1 < Canvas.length)
Canvas[x+1][y] = 255;
} else if(r.nextInt(3) == 1) {
if( y+1 < Canvas.length)
Canvas[x][y+1] = 255;
} else if(r.nextInt(3) == 2) {
if( x > 0)
Canvas[x-1][y] = 255;//THIS IS WHERE ERROR IS POINTING
} else if(r.nextInt(3) == 3) {
if( y > 0)
Canvas[x][y-1] = 255;
}
if(stepsTaken < diffusionStep) {
checkPopulation();
} else {
System.out.println("done");
return;
}
}
Edit: Also you should check x-1 and y-1 are not negative.
Alright, let's walk through all your problems one by one.
The indices. This is obvious and I won't discuss it further.
checkPopulation() and particleDiffusion() recursively invoke each other. Since neither stepsTaken or diffusionStep are modified in the code path you will eventually see a StackOverflow. You don't seem to need to use recursion for this, why not use a loop instead? I'm assuming you have an initial call to checkPopulation(), so how about replacing it with:
for (int stepsTaken = 0; stepsTaken < diffusionStep; stepsTaken++) {
checkPopulation();
}
Right now, at a high level, your methods look like
main() {
checkPopulation();
}
checkPopulation() {
...
particleDiffusion();
}
particleDiffusion() {
...
if (stepsTaken < diffusionStep) {
checkPopulation();
}
}
With the change, it now looks like
main() {
for (int stepsTaken = 0; stepsTaken < diffusionStep; stepsTaken++) {
checkPopulation();
}
}
checkPopulation() {
...
particleDiffusion();
}
particleDiffusion() {
...
}
Calling r.nextInt(3) == x multiple times does not distribute the probability across the four directions evenly. There's no guarantee any of the four cases will actually be successful. Save the result of r.nextInt(3) in a variable and then do the comparison on that variable.

java:implement 8 queen using depth first search

i am try to implement 8 queen using depth search for any initial state it work fine for empty board(no queen on the board) ,but i need it to work for initial state if there is a solution,if there is no solution for this initial state it will print there is no solution
Here is my code:
public class depth {
public static void main(String[] args) {
//we create a board
int[][] board = new int[8][8];
board [0][0]=1;
board [1][1]=1;
board [2][2]=1;
board [3][3]=1;
board [4][4]=1;
board [5][5]=1;
board [6][6]=1;
board [7][7]=1;
eightQueen(8, board, 0, 0, false);
System.out.println("the solution as pair");
for(int i=0;i<board.length;i++){
for(int j=0;j<board.length;j++)
if(board[i][j]!=0)
System.out.println(" ("+i+" ,"+j +")");
}
System.out.println("the number of node stored in memory "+count1);
}
public static int count1=0;
public static void eightQueen(int N, int[][] board, int i, int j, boolean found) {
long startTime = System.nanoTime();//time start
if (!found) {
if (IsValid(board, i, j)) {//check if the position is valid
board[i][j] = 1;
System.out.println("[Queen added at (" + i + "," + j + ")");
count1++;
PrintBoard(board);
if (i == N - 1) {//check if its the last queen
found = true;
PrintBoard(board);
double endTime = System.nanoTime();//end the method time
double duration = (endTime - startTime)*Math.pow(10.0, -9.0);
System.out.print("total Time"+"= "+duration+"\n");
}
//call the next step
eightQueen(N, board, i + 1, 0, found);
} else {
//if the position is not valid & if reach the last row we backtracking
while (j >= N - 1) {
int[] a = Backmethod(board, i, j);
i = a[0];
j = a[1];
System.out.println("back at (" + i + "," + j + ")");
PrintBoard(board);
}
//we do the next call
eightQueen(N, board, i, j + 1, false);
}
}
}
public static int[] Backmethod(int[][] board, int i, int j) {
int[] a = new int[2];
for (int x = i; x >= 0; x--) {
for (int y = j; y >= 0; y--) {
//search for the last queen
if (board[x][y] != 0) {
//deletes the last queen and returns the position
board[x][y] = 0;
a[0] = x;
a[1] = y;
return a;
}
}
}
return a;
}
public static boolean IsValid(int[][] board, int i, int j) {
int x;
//check the queens in column
for (x = 0; x < board.length; x++) {
if (board[i][x] != 0) {
return false;
}
}
//check the queens in row
for (x = 0; x < board.length; x++) {
if (board[x][j] != 0) {
return false;
}
}
//check the queens in the diagonals
if (!SafeDiag(board, i, j)) {
return false;
}
return true;
}
public static boolean SafeDiag(int[][] board, int i, int j) {
int xx = i;
int yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
if (board[xx][yy] != 0) {
return false;
}
yy++;
xx++;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
if (board[xx][yy] != 0) {
return false;
}
yy--;
xx--;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
if (board[xx][yy] != 0) {
return false;
}
yy--;
xx++;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
if (board[xx][yy] != 0) {
return false;
}
yy++;
xx--;
}
return true;
}
public static void PrintBoard(int[][] board) {
System.out.print(" ");
for (int j = 0; j < board.length; j++) {
System.out.print(j);
}
System.out.print("\n");
for (int i = 0; i < board.length; i++) {
System.out.print(i);
for (int j = 0; j < board.length; j++) {
if (board[i][j] == 0) {
System.out.print(" ");
} else {
System.out.print("Q");
}
}
System.out.print("\n");
}
}
}
for example for this initial state it give me the following error:
Exception in thread "main" java.lang.StackOverflowError
i am stuck, i think the error is infinite call for the method how to solve this problem.
any idea will be helpful,thanks in advance.
note:the broad is two dimensional array,when i put (1) it means there queen at this point.
note2:
we i put the initial state as the following it work:
board [0][0]=1;
board [1][1]=1;
board [2][2]=1;
board [3][3]=1;
board [4][4]=1;
board [5][5]=1;
board [6][6]=1;
board [7][1]=1;
[EDIT: Added conditional output tip.]
To add to #StephenC's answer:
This is a heck of a complicated piece of code, especially if you're not experienced in programming Java.
I executed your code, and it outputs this over and over and over and over (and over)
back at (0,0)
01234567
0
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
back at (0,0)
And then crashes with this
Exception in thread "main" java.lang.StackOverflowError
at java.nio.Buffer.<init>(Unknown Source)
...
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at Depth.eightQueen(Depth.java:56)
at Depth.eightQueen(Depth.java:60)
at Depth.eightQueen(Depth.java:60)
at Depth.eightQueen(Depth.java:60)
at Depth.eightQueen(Depth.java:60)
...
My first instinct is always to add some System.out.println(...)s to figure out where stuff is going wrong, but that won't work here.
The only two options I see are to
Get familiar with a debugger and use it to step through and analyze why it's never stopping the loop
Break it down man! How can you hope to deal with a massive problem like this without breaking it into digestible chunks???
Not to mention that the concept of 8-queens is complicated to begin with.
One further thought:
System.out.println()s are not useful as currently implemented, because there's infinite output. A debugger is the better solution here, but another option is to somehow limit your output. For example, create a counter at the top
private static final int iITERATIONS = 0;
and instead of
System.out.println("[ANUMBERFORTRACING]: ... USEFUL INFORMATION ...")
use
conditionalSDO((iITERATIONS < 5), "[ANUMBERFORTRACING]: ... USEFUL INFORMATION");
Here is the function:
private static final void conditionalSDO(boolean b_condition, String s_message) {
if(b_condition) {
System.out.println(s_message);
}
}
Another alternative is to not limit the output, but to write it to a file.
I hope this information helps you.
(Note: I edited the OP's code to be compilable.)
You asked for ideas on how to solve it (as distinct from solutions!) so, here's a couple of hints:
Hint #1:
If you get a StackOverflowError in a recursive program it can mean one of two things:
your problem is too "deep", OR
you've got a bug in your code that is causing it to recurse infinitely.
In this case, the depth of the problem is small (8), so this must be a recursion bug.
Hint #2:
If you examine the stack trace, you will see the method names and line numbers for each of the calls in the stack. This ... and some thought ... should help you figure out the pattern of recursion in your code (as implemented!).
Hint #3:
Use a debugger Luke ...
Hint #4:
If you want other people to read your code, pay more attention to style. Your indentation is messed up in the most important method, and you have committed the (IMO) unforgivable sin of ignoring the Java style rules for identifiers. A method name MUST start with a lowercase letter, and a class name MUST start with an uppercase letter.
(I stopped reading your code very quickly ... on principle.)
Try to alter your method IsValid in the lines where for (x = 0; x < board.length - 1; x++).
public static boolean IsValid(int[][] board, int i, int j) {
int x;
//check the queens in column
for (x = 0; x < board.length - 1; x++) {
if (board[i][x] != 0) {
return false;
}
}
//check the queens in row
for (x = 0; x < board.length - 1; x++) {
if (board[x][j] != 0) {
return false;
}
}
//check the queens in the diagonals
if (!SafeDiag(board, i, j)) {
return false;
}
return true;
}

Categories

Resources