The pieces are replaced with n pieces a,n a piece B, in a row on the board. There is an empty space between them, with the "." Say, for example: AAA. BBB now needs all the A and B pieces to swap positions. The rules for moving pieces are: 1. A piece can only move to the right, B pieces can only move to the left. 2. Each piece can be moved to an adjacent vacancy. 3. Each piece can skip a different piece and fall into the empty space (a skip B or b skip a). Aaa. BBB can go: Move a ==> AA. ABBB move B ==> Aaab. BB jumped off the example: AA. ABBB ==> Aaba. BB below the program completed AB transposition function, please read the analysis of the source code, fill out the missing content of the underlined part.
public class Main {static void Move (char[] data, int from, int to) {Data[to] = Data[from];
Data[from] = '. ';
Static Boolean valid (char[] data, int k) {if (k<0 | | | k>=data.length) return FALSE;
return true;
static void F (char[] Data {while (true) {Boolean tag = false;
for (int i=0; i<data.length; i++) {int dd = 0;//move direction if (data[i]== '. ') continue;
if (data[i]== ' A ') dd = 1;
if (data[i]== ' B ') dd =-1; if (valid (data, I+DD) && valid (DATA,I+DD+DD) && data[i+dd]!=data[i] && data[i+dd+dd]== '. ')
{//If can jump ... move (data, I, I+DD+DD);
System.out.println ("Hop:" +new String (data));
Tag = true;
Break
} if (tag) continue;
for (int i=0; i<data.length; i++) {int dd = 0;//' Move direction if (data[i]== '. ') continue;
if (data[i]== ' A ') dd = 1;
if (data[i]== ' B ') dd =-1; if (valid (data, I+DD) && data[i+dd]== '. ') {//If you can move ... if (valID (DATA,I+DD+DD) && (valid (data,i+dd*-1) && data[i+dd+dd]==data[i+dd*-1)) continue;
Fill in the blank position move (data, I, I+DD);
System.out.println ("Move:" +new String (data));
Tag = true;
Break
} if (tag==false) break; } public static void Main (string[] args) {char[] data = "AAA."
BBB ". ToCharArray ();
f (data);
}
}