/**
* There is a 5*5 matrix; the elements in this matrix are different integer from
* 0 to 24. The elements in this matrix are disordered. 0 is a special element.
* The upper element, under element, left element and right element of 0 can be
* exchanged with 0. Such exchange operations are 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 its left element.
*
* Operation "R" means 0 exchanged with its right element.
*
*
*
* For example, the original matrix is
*
* [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]
*
* With the operation sequence “URRDDL”, the new matrix will 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 the
* operations made on the original matrix. Please provide the correct sequence
* for the operations.
*
* The input will be the original matrix, the target matrix and an 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 are different.
* The elements in the original matrix are random ordered.
* The max length of operations is 15.
* If "0" is already on the boundary, it is not possible to do further movement.
* for example, if 0 is in the top row, then there is no more "U".
* The input will be the original matrix, the target matrix and an operation sequence with
* wrong order. The output will be a correct operation sequence.
* In case there is no way to get the target matrix with the input operations, please output
* “None”
* Don’t include any 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, 14
*
* 23, 16, 1, 2, 5
*
* 21, 17, 8, 3, 6]
*
* The target matrix is
*
* [20, 18, 7, 19, 10
*
* 24, 4, 0, 11, 9
*
* 13, 22, 15, 12, 14
*
* 23, 16, 1, 2, 5
*
* 21, 17, 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
*
* 13, 0, 22, 12, 14
*
* 23, 16, 1, 2, 5
*
* 21, 17, 8, 3, 6]
*
* The target matrix is
*
* [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]
*
* The input operation sequence is “RRLUDD”
*
* The output operation sequence should be “URRDDL”
*
*
*/
public class MatrixMoving {
public static char[] calculateTheRightSequence(final int originalMatrix[][],
int newMatrix[][], char[] input) {
final 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;
}
//深度克隆和浅度克隆的区别。
// 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 = false;
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);
}
}矩阵正确的移动序列
最新推荐文章于 2025-05-07 21:13:41 发布
3331

被折叠的 条评论
为什么被折叠?



