Java – array recursion
I have a task I can't figure out. Any pointer will be very grateful. It is like this:
A series of bulbs are represented as a true / false array. Each bulb has a switch. By clicking any bulb, you can switch it and two adjacent bulbs (one on the left and one on the right); If you click the switch, the bulb is at the edge - of course, there is only one adjacent switch)
What needs to be done is a method that accepts a series of arrays of turn on / off bulbs, and another method represents another state of the so-called first array after clicking some switches Therefore, recursion must be used to determine whether there is a toggle click combination that converts array 1 to array 2
This is the signature of the method:
public static boolean disco(boolean[] init,boolean[] target)
If array init can be converted to target, it returns true; otherwise, it returns false The method must be static. Do not use loops and amplifiers Any other static and global variables, only local variables
Example:
boolean[] init = {true,false,true,false}; boolean[] target = {false,true};
For more than two arrays, disco (init, target) will return true, because switching the first and fourth bulbs will produce the target state (remember that adjacent bulbs will also be switched)
Solution
New version
public static boolean disco(boolean[] init,boolean[] target) { return recurse(init,boolean,0); } public static boolean recurse(boolean[] init,boolean[] target,int min) { if (min == init.length) if (init == target) return true; else return false; boolean[] temp = "init with a change at min"; boolean a = recurse(init,target,min+1); boolean b = recurse(temp,min+1); return a||b; }
New version
I break it down into three cases:
Case 1: length% 3 = 0 by changing the first bulb and the second bulb, you can affect the change only on the third bulb Then changing to 4 and 5 will make the sixth unique change We see that we can change each bulb, and its index can be divided by three Working backwards (from the end) we can do the same thing, but this time it tells us that we can change the bulb whose index (I 1) is divided by 3 Combining the two, we see that if we want to change the index 0,1 mod 3, we can But to change 2, we only need to change an adjacent 0,1 pair and then make a change in the middle So in all cases, these can be solved
Case 2: length% 3 = 1 is the same as the first case, but we can affect a single change of 0,2 mod 3 to squeeze the case of 1 mod 3
Case 3: length% 3 = 2. Working forward and backward only produces 0 mod 3 We made two changes to the bulb (because we can ignore any changes to position 0) Changing 1 or 2 reverses the position surrounded by two zeros, while changing 0 exchanges parity in adjacent blocks of 1,2, with zeros between them (it would be easier to draw) But what I have shown so far is that 0 can be corrected, and in any adjacent 1 or 2, you can fix both if they are wrong without changing anything else If there are errors, the changes are propagated to the adjacent 1,2 pairs You can move this change if necessary This means that any even number of errors in 1,2 locations can be fixed, but odd numbers cannot All errors at position 0 can be fixed
public static boolean disco(boolean[] init,boolean[] target) { if (init.length%3 == 0 || init.length%3 == 1) return true; return recurse(init,true); } public static boolean recurse(boolean[] init,int index,boolean even) { if (index = init.length) return even; if (init[index] != target[index] && index%3 != 0) even = !even; return recurse(init,index+1,even); }