亚马逊笔试题2

2.

Question:

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 lenght of operatoins 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”

-----------------------------------------------

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void calculateTheRightSequence(int originalMatrix[][5], int newMatrix[][5], 
							   char inputSequence[], char outputSequence[]);

int Getnput( char operation[], int originalMatrix[][5], int newMatrix[][5]);
int main()
{
	int original[5][5];
	int newMatrix[5][5];
	char inputOperation[100]={0};
	char outputOperation[100]= {0};
	while( Getnput(inputOperation, original, newMatrix) )
	{
		calculateTheRightSequence(original, newMatrix, inputOperation, outputOperation);
		printf("%s\n", outputOperation);
	}
	return 0;
}

//your code is here
void calculateTheRightSequence(int originalMatrix[][5], int newMatrix[][5], 
							   char inputSequence[], char outputSequence[])
{
	string s(inputSequence);
	int count = s.size();
	int countL = 0, countR = 0, countU = 0, countD = 0;
	for (int i = 0; i< count; i++)
	{
		if(s[i] == 'L')
			countL++;
		else if(s[i] == 'R')
			countR++;
		else if(s[i] == 'U')
			countU++;
		else if(s[i] == 'D')
			countD++;
	}
	int j = 0;
	while (j < countL)
		s[j++] = '1';//Left
	while(j < countL + countR)
		s[j++] = '2';//Right
	while(j < countL + countR + countU)
		s[j++] = '3';//Up
	while(j < count)
		s[j++] = '4';//Down
	
	//get the row and column of number 0 in the originalMatrix
	int row = 0, column = 0;
	bool isBreak = false;
	for (row = 0; row < 5; row++)
	{		
		for(column = 0;column < 5;column++)			
		{
			if(originalMatrix[row][column] == 0)
			{
				isBreak = true;
				break;
			}
		}
		if(isBreak)
			break;
	}

	
	bool isNext = true;
	bool isFind = false;
	do 
	{
		//check is the right order
		int rMatrix[5][5];
		for (int m = 0; m < 5; m++)
		{
			for (int n = 0; n < 5; n++)
			{
				rMatrix[m][n] = originalMatrix[m][n];
			}
		}
		
		int rRow = row;
		int rColumn = column;
		for (int i = 0; i < count; i++)
		{
			if(s[i] == '1')//left
			{
				swap(rMatrix[rRow][rColumn],rMatrix[rRow][rColumn - 1]);
				rColumn--;
			}
			else if (s[i] == '2')//right
			{
				swap(rMatrix[rRow][rColumn],rMatrix[rRow][rColumn + 1]);
				rColumn++;
			}
			else if (s[i] == '3')//up
			{
				swap(rMatrix[rRow][rColumn],rMatrix[rRow - 1][rColumn]);
				rRow--;
			}
			else if (s[i] == '4')//down
			{
				swap(rMatrix[rRow][rColumn],rMatrix[rRow + 1][rColumn]);
				rRow++;
			}
		}
		int rCount = 0;
		for (int m = 0; m < 5; m++)
		{
			for (int n = 0; n < 5; n++)
			{
				if(rMatrix[m][n] == newMatrix[m][n])
					rCount++;
			}
		}
		if(rCount == 25)
		{
			isFind = true;
			break;
		}
		
		//get the next order
		//使用全排列非递归方法 http://blog.csdn.net/morewindows/article/details/7370155/
		isNext =false;
		for (j = count - 1; j > 0; j--)
		{
			if (s[j - 1] < s[j])
			{
				isNext = true;
				int k = j - 1;
				int min  = s[j];
				int indexMin = j;
				for (int t = j; t < count; t++)
				{
					if(s[k] < s[t] && s[t] <= min)
					{
						min = s[t];
						indexMin = t;
					}
				}
				swap(s[k],s[indexMin]);
				int r = count - 1;
				int l = k + 1;
				while(l < r)
					swap(s[l++],s[r--]);
				break;
			}
		}

	} while (isNext);

	string rs;
	
	if (isFind)
	{
		for (int i = 0; i < count; i++)
		{
			if(s[i] == '1')
				rs.append("L");
			else if(s[i] == '2')
				rs.append("R");
			else if(s[i] == '3')
				rs.append("U");
			else if(s[i] == '4')
				rs.append("D");
		}
	} 
	else
	{
		rs = "None";
	}
	
	int i = 0;
	for(; i < rs.size(); i++)
		outputSequence[i] = rs[i];
	outputSequence[i] = '\0';

	return;
}

int Getnput( char operation[], int originalMatrix[][5], int newMatrix[][5])
{
	int i = 0, j = 0;
	for( i = 0; i < 5; ++i )
		for(j = 0; j < 5; ++j)
		{
			if( scanf(" %d ", &originalMatrix[i][j]) != 1 )
				break;
		}
	if( j < 5 ) return 0;
	for( i = 0; i < 5; ++i )
		for(j = 0; j < 5; ++j)
		{
			scanf(" %d ", &newMatrix[i][j]);
		}

		scanf( "%s ", operation );  
		return 1;	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值