We have a square board of size n * n, the cells of which can take values of boolean type: #t is a painted cell, #f is an empty (not painted) cell. The board is considered to be filled correctly only if the painted cell has no neighbors to the right, left, top and bottom (only diagonally). So:
X 0 X 0 X 0 X 0 O You need to write the method method (boolean [] field, int n), which will take a one-dimensional (!) Array, the size of the board n and show the correctness of the board fill (#t, #f).
Right:
X 0 0 0 0 X 0 X 0 0 0 0 X 0 0 0 Wrong:
X 0 0 0 X 0 0 X 0 0 X 0 X 0 0 0 The numbering of elements in the array, respectively:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 In conclusion, you need to create a method that will count the number of possible correctly filled fields for a particular n. Ie creating in a naive way all possible options for filling in the fields and checking them with the help method.
Thank you so much for any tips! I searched on the Internet, found a solution for a rook problem that I thought was not applicable to this problem.
My own sketches (not working for even n):
public static boolean gueltig1(boolean[] feld, int n){ for (int i = 0; i < n * n; i++){ if(i + n <= n * n - 1){ if (feld[i] == true && (feld[Math.abs(i + 1)] == true || feld[Math.abs(i - 1)] == true || feld[Math.abs(i + n)] == true || feld[Math.abs(i - n)] == true)){ return false;} } if(i + n > n * n - 1 && i + 1 < n * n - 1){ if (feld[i] == true && (feld[Math.abs(i + 1)] == true || feld[Math.abs(i - 1)] == true || feld[Math.abs(i - n)] == true)){ return false;} } if(i == n * n - 1){ if (feld[i] == true && (feld[Math.abs(i - 1)] == true || feld[Math.abs(i - n)] == true)){ return false;} } } return true; } Or so:
public static boolean gueltig(boolean[] feld, int n){ for (int i = n + 1; i < n * n; i++){ if(feld[i] == true && (feld[i - n] == true || feld[Math.abs(i - 1)] == true)){ return false;} } return true; } PS: Beginner