/** * There is a 5*5 matrix; The elements in this matrix is different integer from * 0 to 24. The elements in this matrix is disordered.
0 is a special element. * The upper element, under element, left element and right element of 0 can is * exchanged with 0.
Such Exchange Operations is named as ' U ', ' D ', ' L ' and * ' R '.
* * operation "U" means 0 exchanged with its upper element.
* * operation "D" means 0 exchanged with its under element.
* * operation "L" means 0 exchanged with it left element.
* * operation "R" means 0 exchanged with it right element. * * * * For example, the original matrix is * * [20, 18, 7, 19, 10 * * 24, 4, 15, 11, 9 * * 13, 0, 22, 12,
* *, 1, 2, 5 * *, +, 8, 3, 6] * * With the operation sequence "Urrddl", the new matrix would be *
* [20, 18, 7, 19, 10 * * 24, 15, 11, 12, 9 * * 13, 4, 22, 2, 14 * * 23, 16, 0, 1, 5 * * 21, 17, 8, 3, 6] * * Now, we know the original matrix, the matrix AfteR the operations and all of the * operations made on the original matrix.
Provide the correct sequence * for the operations.
* The input would be the original matrix, the target matrix and a operation * sequence with wrong order. * * If There is a correct sequence for this input, then print the correct * sequence.
Otherwise, print "None".
* * * * Rules and Example: * * The elements in the original matrix is different.
* The elements in the original matrix is random ordered.
* The max length of operations is 15.
* If "0" is already on the boundary, it's not possible to do further movement.
* For example, if 0 are in the top row and then there are no more "U". * The input would be the original matrix, the target matrix and a operation sequence with * wrong order.
The output would be a correct operation sequence. * In case there is no-to-get the target matrix with the input operations, please output * "None" * Don ' t include an Y space in the GeneratEd operation sequence. for * examples, the original matrix is Example 1: * * [20, 18, 7, 19, 10 * * 24, 4, 15, 11, 9 * * 13, 0, 22, 12, * *, 1, 2, 5 * *, +, 8, 3, 6] * * The target matrix is * * [20, 18, 7, 19, 10 * * 24, 4, 0,
One, 9 * * *, (+), 1, 2, 5 * *, +, 8, 3, 6] * * The input operation sequence is "UR"
* The output operation sequence should be "RU" * * Example 2: * * [20, 18, 7, 19, 10 * * 24, 4, 15, 11, 9 * * *, 0, (+), 1, 2, 5 * *, +, 8, 3, 6] * * The target matrix is * * [20, 18, 7, 19, Ten * *, 9 *, 4, 2, 0, 1, 5 * *, +, 8, 3, 6] * * The input operat. * * * *
Ion sequence is ' rrludd ' * * The output operation sequence should be "URRDDL" * * */public class Matrixmoving { public static char[] Calculatetherightsequence (final int originalmatrix[][], int newmatrix[][], char[] input) {FINA L int N = Input.length;
int originalx = 0;
int originaly = 0;
for (int i=0, i<5; i++) {for (int j=0; j<5; J + +) {if (originalmatrix[i][j] = = 0) {originalx = i;
originaly = j;
}}} Hashmap<integer, integer> map = new Hashmap<integer, integer> ();
for (int i=0; i<4; i++) {map.put (i,0);
} for (int i=0; i<n; i++) {if (input[i] = = ' U ') {map.put (0,map.get (0) +1);
}else if (input[i] = = ' D ') {map.put (1,map.get (1) +1);
}else if (input[i] = = ' L ') {Map.put (2,map.get (2) +1);
}else if (input[i] = = ' R ') {Map.put (3,map.get (3) +1);
}else{System.out.println ("Invalid input operations.");
return null;
}} final int a = map.get (0);
Final int b = map.get (1);
Final int c = Map.get (2);
final int d = map.get (3);
int value = 1;
int start = n*2-1;
while (Start > 0) {value <<= 1;
value++;
start--;
} int[] out = new Int[n];
while (value > 0) {map.put (0,a); Map.put (1,B);
Map.put (2,C);
Map.put (3,D);
for (int i=0; i<n; i++) {out[i] = 10;
} int times = 0;
int vv = value;
Boolean valid = true;
while (Times < N) {int key = vv&3;
if (Map.get (key) > 0) {map.put (key, Map.get (key)-1);
VV >>= 2;
Out[times] = key;
times++;
}else{valid = false;
Break
}} if (!valid) {value--;
Continue
}//The difference between deep clones and shallow clones.
int[][] Currentm = Originalmatrix.clone ();
int[][] Currentm = new INT[5][5];
for (int i=0, i<5; i++) {for (int j=0; j<5; J + +) {Currentm[i][j] = originalmatrix[i][j];
}} int currentx = ORIGINALX;
int currenty = originaly;
for (int i=n-1; i>=0; i--) {int targetx = 0;
int targety = 0;
if (out[i] = = 0) {targetx = currentX-1;
Targety = CurrentY;
}else if (out[i] = = 1) {Targetx = CurrentX + 1;
Targety = CurrentY;
}else if (out[i] = = 2) {targetx = CurrentX; Targety = CURrentY-1;
}else if (out[i] = = 3) {targetx = CurrentX;
Targety = currenty + 1;
} if (targetx<0 | | targetx>4 | | targety<0 | | targety>4) {valid = false;
Break
}else{int temp = currentm[targetx][targety];
Currentm[targetx][targety] = Currentm[currentx][currenty];
Currentm[currentx][currenty] = temp;
CurrentX = Targetx;
CurrentY = targety;
}} if (!valid) {value--;
Continue } for (int i=0, i<5; i++) {for (int l=0; l<5; l++) {if (currentm[i][l]! = Newmatrix[i][l]) {valid = f
Alse;
Break
}}} if (!valid) {value--;
Continue
}else{char[] result = new Char[n];
for (int m=0; m<n; m++) {if (out[n-m-1] = = 0) {Result[m] = ' U ';
}else if (out[n-m-1] = = 1) {Result[m] = ' D ';
}else if (out[n-m-1] = = 2) {Result[m] = ' L ';
}else if (out[n-m-1] = = 3) {Result[m] = ' R ';
} System.out.print (Result[m]); } return result;
}} System.out.println ("None");
return null; }/** * @param args */public static void main (string[] args) {int[][] a = {{20, 18, 7, 19, 10},{24, 4, 15, 11,
9},{13, 0, 22, 12, 14},{23, 16, 1, 2, 5},{21, 17, 8, 3, 6};
Int[][] B = {{20, 18, 7, 19, 10},{24, 15, 11, 12, 9},{13, 4, 22, 2, 14},{23, 16, 0, 1, 5},{21, 17, 8, 3, 6}};
char[] o = {' R ', ' R ', ' L ', ' U ', ' d ', ' d '};
Matrixmoving.calculatetherightsequence (A, B, O); }
}