Combinations with replacement Java - java

For the last couple of days, I am trying to solve this problem of combinations with replacement in java. I checked other languages as well maybe it was done in them and I could translate to java but with no luck, so any help greatly appreciated.
So here is the problem(mock interview question) I came upon:
Combine from range 0 - 6(n)
In an array of size r (let's say 3)
So the formula for combinations with replacement is C(n,r)=(n+r−1)! / r!(n−1)!. In this case the combinations will be 84
000
010,
011,
...,
025,
055,
...,
666.
However, I can't get my head around this algorithm WITH REPLACEMENT which is an entirely different story from without replacement.
Thank you again in advance for your help.

Retrieved first version of answer:
You can use nn=(n+1) variants of digit at every of r places, so overall number of combinations is P = nn^r. Note that every combination corresponds to the number in range 0..P-1.
So you can walk through all integer values in range 0..P-1 and represent loop counter in nn-ary system.
Java code
public static void main (String[] args) throws java.lang.Exception
{
int n = 2;
int r = 3;
int nn = n + 1;
int p = 1;
for (int i=0; i<r; i++)
p *= nn;
for (int i=0; i<p; i++){
int t = i;
String comb = "(";
for (int j=0; j<r; j++){
comb = comb + String.format("%2d, ", t % nn);
t = t / nn;
}
comb = comb.substring(0, comb.length()-2) + ')';
System.out.println(comb);
}
}
Python code:
n = 2
r = 3
nn = n + 1
p = nn**r
for V in range(p):
t = V
comb = []
for i in range(r):
d = t % nn
comb.append(d)
t = t // nn
print(comb)
[0, 0, 0]
[1, 0, 0]
[2, 0, 0]
[0, 1, 0]
[1, 1, 0]
[2, 1, 0]
[0, 2, 0]
[1, 2, 0]
[2, 2, 0]
[0, 0, 1]
[1, 0, 1]
[2, 0, 1]
[0, 1, 1]
[1, 1, 1]
[2, 1, 1]
[0, 2, 1]
[1, 2, 1]
[2, 2, 1]
[0, 0, 2]
[1, 0, 2]
[2, 0, 2]
[0, 1, 2]
[1, 1, 2]
[2, 1, 2]
[0, 2, 2]
[1, 2, 2]
[2, 2, 2]
The second version: for combinations with replacement.
Recursive (the simplest way) generation in Python.
def cwrreq(maxlen, maxx, s):
if len(s)== maxlen:
print(s)
else:
for i in range(maxx + 1):
cwrreq(maxlen, i, s + str(i))
def combwithrepl(maxlen, maxval):
cwrreq(maxlen, maxval, "")
combwithrepl(3, 6)
generates 84 combinations
000
100
110
111
200
...
663
664
665
666
Full list for (3,3).
Meaning: there are three indistinguishable boxes and three kinds of paints (say red, green, blue).
000 all boxes are hollow
100 one box is red
110
111 all boxes are red
200
210 one box is green, another is red
211
220
221
222
300
310
311
320
321 all boxes have distinct colors
322
330
331
332
333 all boxes are blue

private static List<int[]> samples(int n, int k) {
if (k < 0 || n < 0) throw new IllegalArgumentException();
if (k == 0) return Collections.emptyList();
List<Integer> set = new ArrayList<>();
for (int i = 0; i < n; i++) set.add(i);
if (k == 1) return set.stream().map(i -> new int[]{i}).collect(Collectors.toList());
List<int[]> previous = samples(n, k - 1);
List<int[]> out = new ArrayList<>();
for (int i : set) for (int[] array : previous) out.add(glue(i, array));
return out;
}
private static int[] glue(int i, int[] array) {
int[] out = new int[array.length + 1];
out[0] = i;
System.arraycopy(array, 0, out, 1, array.length);
return out;
}
e.g.,
for (int[] sample : samples(2, 3)) {
System.out.println(Arrays.toString(sample));
}
yields
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]
and
for (int[] sample : samples(4, 2)) {
System.out.println(Arrays.toString(sample));
}
yields
[0, 0]
[0, 1]
[0, 2]
[0, 3]
[1, 0]
[1, 1]
[1, 2]
[1, 3]
[2, 0]
[2, 1]
[2, 2]
[2, 3]
[3, 0]
[3, 1]
[3, 2]
[3, 3]

Related

Base Negation given an inDigits

I have the following problem:
Given an input in base (the input is given as an array of its digits in that base), write the negation of the number in "base's"-complement notatation in outDigits.
The "base's complement" notation of a number is a generalization of "two's complement": if we treat (-x) as an unsigned number in base and add it to x, we should get 0 (modulo base^digit-size). I cannot call other function (even Math.pow)
I keep getting an error with my tests. My code:
public static void baseNegate(int base, int[] inDigits, int[] outDigits) {
outDigits[0] = 1;
for (int i = outDigits.length - 1; i >= 0; i--) {
outDigits[i] += base - (1 + inDigits[i]);
if (i < outDigits.length - 1) {
outDigits[i + 1] = outDigits[i] / base;
}
outDigits[i] %= base;
}
}
I cannot find the error in my calculations, please help.
my test:
------------------------------------ Negate number 365 in base 10 ------------------------------------
Test case have FAILED.
Base: 10
Input number: [5, 6, 3]
Expected: [5, 3, 6]
Output: [5, 0, 0]
-------------------------------- Negate number b1010110011 in base 2 --------------------------------
Test case have FAILED.
Base: 2
Input number: [1, 1, 0, 0, 1, 1, 0, 1, 0, 1]
Expected: [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
Output: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
-------------------------------------- Negate 0x7AF0 in base 16 --------------------------------------
Test case have FAILED.
Base: 16
Input number: [0, 15, 10, 7]
Expected: [0, 1, 5, 8]
Output: [0, 1, 0, 0]
Your problem is that you may seem to be trying to do the negation of the complement while calculating the complement and it is complicating your solution.
You could try to simplify your solution by splitting it into two phases:
First compute the complement.
Second add the +1 to the computed complement.
The following method is a working version of this:
public static void baseNegate(int base, int[] inDigits, int[] outDigits) {
// Compute the complement of the digits
for (int i = outDigits.length - 1; i >= 0; i--)
outDigits[i] = base - (1 + inDigits[i]);
// Negate the complement by adding +1 to the computed number (collection of digits)
for (int i = 0; i < outDigits.length; i++) {
if (outDigits[i] == base - 1) {
// Max out digit. Set it to zero and try with the higher order next.
outDigits[i] = 0;
} else {
// Digit that has room for +1. Finally add the 1 and DONE!
outDigits[i]++;
break;
}
}
}
This approach is clearer, better performing and the code is self explanatory; but I added comments in the code to follow the logic used.
Complete code on GitHub
Hope this helps.
Since the "expected" values show that index 0 is the lowest order digit, it means that for number 123₁₀ the array would be [3, 2, 1], i.e. the digits are in reverse order of what you'd expect as a human. To a computer, it makes sense that value at index i is the value that must be multiplied by baseⁱ.
That means you need the i loop to iterate up, not down, so you can track the carry-over. Otherwise you code works fine:
public static void baseNegate(int base, int[] inDigits, int[] outDigits) {
outDigits[0] = 1;
for (int i = 0; i < outDigits.length; i++) { // <== reversed iteration
outDigits[i] += base - (1 + inDigits[i]);
if (i < outDigits.length - 1) {
outDigits[i + 1] = outDigits[i] / base;
}
outDigits[i] %= base;
}
}
Personally, writing it like this makes more sense, especially since it doesn't rely on outDigits array to be pre-initialized to all 0's:
public static void baseNegate(int base, int[] inDigits, int[] outDigits) {
int carry = 0;
for (int i = 0; i < outDigits.length; i++) {
outDigits[i] = (base - inDigits[i] - carry) % base;
carry = (inDigits[i] + outDigits[i] + carry) / base;
}
}
For better performance, you don't want to use % and /, so something like this might be better:
public static void baseNegate(int base, int[] inDigits, int[] outDigits) {
boolean carry = false;
for (int i = 0; i < outDigits.length; i++) {
if (carry) {
outDigits[i] = base - inDigits[i] - 1;
} else if (inDigits[i] != 0) {
outDigits[i] = base - inDigits[i];
carry = true;
}
}
}
Test
All 3 will give the same result:
public static void main(String[] args) {
test(10, 5,6,3);
test(2, 1,1,0,0,1,1,0,1,0,1);
test(16, 0,15,10,7);
test(8, 0,0,0); // 0 -> 0 (000)
test(8, 1,0,0); // 1 -> -1 (777)
test(8, 7,7,3); // 255 -> -255 (104)
test(8, 0,0,4); // -256 -> -256 (004)
}
static void test(int base, int... inDigits) {
int[] outDigits = new int[inDigits.length];
baseNegate(base, inDigits, outDigits);
System.out.printf("%d: %s -> %s%n", base, Arrays.toString(inDigits),
Arrays.toString(outDigits));
}
Output
10: [5, 6, 3] -> [5, 3, 6]
2: [1, 1, 0, 0, 1, 1, 0, 1, 0, 1] -> [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
16: [0, 15, 10, 7] -> [0, 1, 5, 8]
8: [0, 0, 0] -> [0, 0, 0]
8: [1, 0, 0] -> [7, 7, 7]
8: [7, 7, 3] -> [1, 0, 4]
8: [0, 0, 4] -> [0, 0, 4]
I think there's a problem around here:
if (i < outDigits.length - 1) {
outDigits[i + 1] = outDigits[i] / base;
}
Let's say you're using base 10. Since a digit can only be 0 through 9, dividing by 10 would mean the result of this calculation is always 0. I don't think you intended this.

connecting points in n-dimensional hyper cube

I am experimenting with creating 3d like sketches in processing without using the p3d renderer. I have managed to make a cube but for it I hardcoded all the coordinates and connections and once you want to add another dimension it begins to get a little boring. So I have created a function to create all the coordinates:
float[][] cube(int dims, float w) {
int outputSize = (int)pow(2, dims);
float[] temp = new float[dims];
float[][] res = new float[outputSize][dims];
Arrays.fill(temp, w);
res[0] = temp.clone();
for (int i = 0; i < outputSize - 1; i++) {
for (int j = dims - 1; true; j--) {
temp[j] *= -1;
if (temp[j] < 0) {
break;
}
}
res[i + 1] = temp.clone();
}
return res;
}
It simply works by using binary so the inputs (2, 1) cube would be:
[[1, 1], [1, -1], [-1, 1], [-1, -1]]
It works fine but the problem is that It only returns the corners but not witch corner to connect but I can't find an efficient way to do that. I need another function that returns what to indices to connect.
Here is an example of what the function should do given the array above:
[[0, 1], [1, 3], [3, 2], [2, 0]]
(the inner arrays may be in a different order)
Is there any known algorithm to connect the corners of a n-dimensional cube?
I am ok with changing the other function if some other point generation helps.
Here is a way to iteratively generate the coordinates and indices together:
Start with a cube of dimension n
Make two copies of the cube, and place one at each of the extremal coordinates (e.g. -1 and +1) on the n + 1-th axis
Make edges to join each pair of corresponding vertices on the cubes
You already know that the number of vertices V(n) = 2^n. Since the number of edges added to an n + 1 cube is equal to this (all corresponding vertex pairs), plus those of the copied n cube, the recurrence relation for the number of edges is:
E(n) = 2 * E(n - 1) + V(n - 1) // copies + joining edges
E(1) = 1 // base case for n = 1
--> E(n) = n * 2^(n - 1)
n | E(n)
-------------
1 | 1
2 | 4
3 | 12
4 | 32
5 | 80
This allows one to pre-allocate the number of required edges and calculate index offsets when copying the new cube / adding new edges.
Code:
// edge index
class Edge
{
public int A, B;
public Edge(int a, int b)
{
A = a; B = b;
}
public Edge shift(int n)
{
return new Edge(A + n, B + n);
}
}
// cube class
class Cube
{
// I'll leave out the get-functions etc here
private float[][] m_verts;
private Edge[] m_edges;
public Cube(float[][] v, Edge[] e)
{
m_verts = v;
m_edges = e;
}
}
Cube cube_N(int dims, float w)
{
// base case
if (dims < 1)
return null;
// calculate buffer sizes
int dpow2 = 1 << dims;
int numVerts = dpow2;
int numEdges = dims * (dpow2 / 2);
// buffers
float[] temp = new float[dims];
float[][] verts = new float[numVerts][];
Edge[] edges = new Edge[numEdges];
// base case creation
Arrays.fill(temp, w);
verts[0] = temp.clone();
edges[0] = new Edge(0, 1);
// iterative step
for (int i = 0; i < dims; i++)
{
int nV = 1 << i;
int nE = i * (nV / 2);
// copy + shift vertices
for (int j = 0; j < nV; j++)
{
float[] v = verts[j].clone();
v[i] = -w;
verts[nV + j] = v;
}
// copy + shift previous edges
for (int j = 0; j < nE; j++)
{
edges[nE + j] = edges[j].shift(nV);
}
// create new edges to join cube copies
int off = nE * 2;
for (int j = 0; j < nV; j++)
{
edges[off + j] = new Edge(j, nV + j);
}
}
return new Cube(verts, edges);
}
Results for n = 3:
verts:
[1, 1, 1], [-1, 1, 1], [1, -1, 1], [-1, -1, 1],
[1, 1, -1], [-1, 1, -1], [1, -1, -1], [-1, -1, -1]
edges:
[0, 1], [2, 3], [0, 2], [1, 3], [4, 5], [6, 7],
[4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]
Results for n = 4:
verts:
[1, 1, 1, 1], [-1, 1, 1, 1], [1, -1, 1, 1], [-1, -1, 1, 1],
[1, 1, -1, 1], [-1, 1, -1, 1], [1, -1, -1, 1], [-1, -1, -1, 1],
[1, 1, 1, -1], [-1, 1, 1, -1], [1, -1, 1, -1], [-1, -1, 1, -1],
[1, 1, -1, -1], [-1, 1, -1, -1], [1, -1, -1, -1], [-1, -1, -1, -1]
edges:
[0 , 1], [2 , 3], [0 , 2], [1 , 3], [4, 5], [6 , 7], [4 , 6], [5 , 7],
[0 , 4], [1 , 5], [2 , 6], [3 , 7], [8, 9], [10, 11], [8 , 10], [9 , 11],
[12, 13], [14, 15], [12, 14], [13, 15], [8, 12], [9 , 13], [10, 14], [11, 15],
[0 , 8], [1 , 9], [2 , 10], [3 , 11], [4, 12], [5 , 13], [6 , 14], [7 , 15]

Read and use nested lists from file in java

I have data in this format [ [[22,34,56],[12,31,44],[74,18,53]], [[15,16,18],[90,89,74],[44,32,13]], [[13,15,17],[1,4,7],[88,73,10]]...] inside a text file and I want to read it use the numbers in each of the inner list. So far I am able to read each inner list of list with the code below, how can I extend it to get the numbers? This question only handles the case of reading strings into an array list and I have not found anotheer that deals with my case.
File f = new File("route.txt");
Scanner s = new Scanner(new FileInputStream(f));
s.useDelimiter("]],");
while (s.hasNext()) {
String r = s.next();
System.out.println(r);
}
As mentioned if it is a JSON array, you could do that. But then would need to delve in the resulting data structure for processiong. A do-it-yourself solution:
Path path = Paths.get("route.txt");
byte[] bytes = Files.readAllBytes(path);
String content = new String(bytes, StandardCharsets.ISO_8859_1);
content = content.replace(" ", ""); // So spaces at odd places do not need to be handled.
String[] sequences = content.split("[^\\d,]+"); // Delimit by not: digit or comma.
for (String sequence : sequences) {
if (!sequence.isEmpty()) {
String[] words = sequence.split(",");
int[] numbers = Stream.of(words).mapToInt(Integer::parseInt).toArray();
.. process the sequence(numbers);
}
}
Parse it as a JSON array. I recommend JSON-P.
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonReader;
// ...
public static void main(String[] args) {
String data = "[ [[22,34,56],[12,31,44],[74,18,53]], "
+ "[[15,16,18],[90,89,74],[44,32,13]], "
+ "[[13,15,17],[1,4,7],[88,73,10]]]";
JsonReader jsonReader = Json.createReader(new StringReader(data));
JsonArray array = jsonReader.readArray();
for (int i = 0; i < array.size(); i++) {
JsonArray subArray = array.getJsonArray(i);
for (int j = 0; j < subArray.size(); j++) {
JsonArray subSubArray = subArray.getJsonArray(j);
for (int k = 0; k < subSubArray.size(); k++) {
System.out.println(String.format("[%d, %d, %d] %d",
i, j, k, subSubArray.getInt(k)));
}
}
}
}
Output:
[0, 0, 0] 22
[0, 0, 1] 34
[0, 0, 2] 56
[0, 1, 0] 12
[0, 1, 1] 31
[0, 1, 2] 44
[0, 2, 0] 74
[0, 2, 1] 18
[0, 2, 2] 53
[1, 0, 0] 15
[1, 0, 1] 16
[1, 0, 2] 18
[1, 1, 0] 90
[1, 1, 1] 89
[1, 1, 2] 74
[1, 2, 0] 44
[1, 2, 1] 32
[1, 2, 2] 13
[2, 0, 0] 13
[2, 0, 1] 15
[2, 0, 2] 17
[2, 1, 0] 1
[2, 1, 1] 4
[2, 1, 2] 7
[2, 2, 0] 88
[2, 2, 1] 73
[2, 2, 2] 10

how to write n-level embeded loop with java

Given M integers (N1, N2, Nm), I want to write a N-level embedded loop like following :
for (int a = 0; a < N1; a++)
for (int b = 0; b < N2; b++)
for (int c = 0; c < N3; c++)
....
for (int m = 0; m < Nm; m++)
operation
Since M is a variable, I cannot write fixed number level for-loop. What tricks could help ?
No need for recursion. Instead, think of each iterator as a digit in a number. When you increment the number, you increment the last digit, and if it exceeds the limit (normally 10), it's set to 0, and the digit to the left of it is increased, repeating the overflow logic.
In this case, each "digit" is an independent counter, each with their own "limit", i.e. the value of the given Nx. If you store those limit values in an array, and keep the counters in a same-size array, the logic is fairly simple.
Here is an example of an optimized version, that uses a label to exit directly from a nested loop:
int[] n = { 3, 4, 5 }; // m = 3: N1 = 3, N2 = 4, N3 = 5
int[] i = new int[n.length]; // All "digits" are 0
LOOP: for (;;) {
// operation using i[] here, e.g.
System.out.println(Arrays.toString(i));
// Process "digits" from last to first
for (int j = i.length - 1; ; j--) {
if (j < 0) // Exit main loop if first "digit" overflowed
break LOOP;
if (++i[j] < n[j]) // Increment "digit", and if not overflowed:
break; // exit digit-loop, i.e. loop back to process
i[j] = 0; // Reset "digit" to 0, then process next (to left) "digit"
}
}
Output
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 0, 3]
[0, 0, 4]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
[0, 1, 3]
[0, 1, 4]
[0, 2, 0]
[0, 2, 1]
[0, 2, 2]
[0, 2, 3]
[0, 2, 4]
[0, 3, 0]
[0, 3, 1]
[0, 3, 2]
[0, 3, 3]
[0, 3, 4]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 0, 3]
[1, 0, 4]
[1, 1, 0]
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 1, 4]
[1, 2, 0]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 2, 4]
[1, 3, 0]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[1, 3, 4]
[2, 0, 0]
[2, 0, 1]
[2, 0, 2]
[2, 0, 3]
[2, 0, 4]
[2, 1, 0]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 1, 4]
[2, 2, 0]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 2, 4]
[2, 3, 0]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[2, 3, 4]
The same solution as #Andreeas', just with more explanations (he was faster in posting the answer, I'm adding mine to give myself a reason for the time I spent with the explanations):
import java.util.Arrays;
public class Multiloop {
static public void doSomething(int... maxIndexes) {
// quick check: if any of the maxIndexes is zeo or less
// there's a snowball in a furnace chance for the most inner loop
// to get executed
for(int i=0; i<maxIndexes.length; i++) {
if(maxIndexes[i]<=0) {
return; // nothing to do
}
}
// java guarantees all of then are zero after allocation
int multiIndex[]=new int[maxIndexes.length];
int currIndexPos=maxIndexes.length-1; // start looping with the last
do {
// this happens when the current index reached its correspondent maximum
// which of course is maxIndexes[currIndexPos]-1
while(
currIndexPos>=0 &&
multiIndex[currIndexPos]>=maxIndexes[currIndexPos]-1
) {
currIndexPos--; // search for the prev one to increment
}
if(currIndexPos<0) {
// all the indexes have reached their max value, we are done
break;
}
// if it's not the last index, we need to increment the current one
// and reset to zero all the others following it
if(currIndexPos<maxIndexes.length-1) {
// if not at the max value, then it's safe to increment it
multiIndex[currIndexPos]++;
Arrays.fill(multiIndex, currIndexPos+1, maxIndexes.length, 0);
}
// and now all multicycle indexes are to their proper value
// we reset the currIndexPos to the max and the do what we need to do
currIndexPos=maxIndexes.length-1;
/// Cut along the dotted lines and place your own code
/// ✂...✂...✂...✂...✂...✂...✂...✂...✂...✂
{ // the inner-most cycle, using the multicycle indexes as necessary
// replace it with what you need here
// **** Just don't screw up the currIndexPos!!!
// **** unless you know what you are doing
// **** (e.g. breaking any of the "cycles" on the way)
char nameForIndex='a';
for(int i=0; i<maxIndexes.length; i++) {
if(i>0) {
System.out.print(',');
}
System.out.print(nameForIndex+"="+multiIndex[i]);
nameForIndex++;
}
System.out.println();
}
// ✂...✂...✂...✂...✂...✂...✂...✂...✂...
multiIndex[currIndexPos]++;
} while(true); // the exit condition is handled at the cycle start anyway
}
static public void main(String args[]) {
// a triple cycle with their respective max indexes
doSomething(2,4,3);
}
}
Output:
a=0,b=0,c=0
a=0,b=0,c=1
a=0,b=0,c=2
a=0,b=1,c=0
a=0,b=1,c=1
a=0,b=1,c=2
a=0,b=2,c=0
a=0,b=2,c=1
a=0,b=2,c=2
a=0,b=3,c=0
a=0,b=3,c=1
a=0,b=3,c=2
a=1,b=0,c=0
a=1,b=0,c=1
a=1,b=0,c=2
a=1,b=1,c=0
a=1,b=1,c=1
a=1,b=1,c=2
a=1,b=2,c=0
a=1,b=2,c=1
a=1,b=2,c=2
a=1,b=3,c=0
a=1,b=3,c=1
a=1,b=3,c=2
How about using recursive:
int recursive_loop(int var_M){
if(var_M == destination) return 0;
else{
for(int i=0;i<var_M || recursive_loop(var_M+1) ; i++){
// Do operation here
}
}
}
I tested with C , it work.

Java Recursive function with two for loops in it

Inside my program, the most important part is filling an array with some number of objects.
These objects are bishops on a chess board. There are alternative functions to place the bishop and the place it attacked places or decide if the board is filled properly are made.
At the end of the first couple of loops the board is filled and falls back into its previous state where the next attempt is done.
The problem is that somehow it uses not the old array but the last one so that the array keeps piling up numbers in stead of making all the possible options. What am i doing wrong?
EDIT:
Think i have to mention that in the outcome the two is how they are should be. after that this one is counted with no zero in it and CheckGoedeStand returns a one.
int SolveBoard(int m,int n,int d,int l) {
int[][] field = new int[m][n]; // this is the m*n schaakbord. int is standaard 0.
// probleem opgelost
System.out.println("aantal lopers: " + l);
int GoedeStand = Recursie(field,0,0, m, n, d, l);
PrintFieldImage(field);
return GoedeStand;
}
//deze fuctie is alleen gekoppeld saan SolveBoard()
int Recursie(int[][] field, int LopersSet, int AGB, int m, int n, int d, int l) {
int mcount, ncount;
int[][] fieldC = field;
//de rekenlus
// 0 is leeg, 1 is aangevallen, 2 is lopers plaats, 3 is dame haar plaats
if (LopersSet < l) {
LopersSet++;
for (mcount = 0; mcount < m; mcount++) {
for (ncount = 0; ncount < n; ncount++) {
//if (field[mcount][ncount] <= 1) {
fieldC = PlaatsLoper(fieldC, m, n, mcount, ncount);
//nu de recursie, eerst kopie maken van bord zodat deze niet verloren gaat
AGB = Recursie(fieldC, LopersSet, AGB, m, n, d, l);
//}
}
}
} else {
PrintFieldImage(field);
}
if (CheckGoedeStand(field, m, n) == 1 && LopersSet == l) {
//PrintFieldImage(field);
AGB++;
//field = new int[m][n];
}
return AGB;
}
As you can see, I start with an empty array, d is unused, and for the testing I have m, n, and l set as 2.
This is my output:
[2, 0]
[0, 1]
[2, 2]
[1, 1]
[2, 2]
[2, 1]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
[2, 2]
AGB= 19

Categories