2013亚马逊校园招聘在线笔试题MM-Chess

Question 2 / 2 (Amazon Campus(9): MM-Chess)


There is an interesting game called MM-Chess. The size of the board is 1*N, every grid has a score (non-negative).  The first grid is the start and the Nth grid is the end. The game requires players to control the chess starting from the starting point to the end.


There're four types of cards in the game and the total number is M. Each type of card are labeled one integer number in [1,4]. After using a card with number x on it, the chess will move x steps forward. Each time, player will choose one unused card to move the chess forward, and each card can only be used once. In the game, the chess gains the score at the starting point automatically.  When the chess arrives at a new grid, it also gets the score on that point. The goal of the game is to get the most score.

Input:

                The first line contains two integers N (the size of board) and M (the number of the cards).

                The second line contains N integers, meaning the scores on the the board (the i-th integer corresponds to the score on the i-th grid).

                The third line contains M integers, meaning the numbers on the M cards.

                The sum of the number of M cards equals to N-1.

                You can assume that 1 <= N <= 350, 1 <= M <= 120, and that the number of cards are less than 40 for each kind.

Output:

                One integer. The most score that player can get.


Sample Input 1
4 2
1 2 1 2
1 2

Sample Output 1

5


Given two cards with number 1 and number 2 each, we have two choices: path one is 1 -> 2 -> 2, path two is 1 -> 1 -> 2. The maximum score is 5, which is the output.

Sample Input 2
5 3
1 2 1 2 1

1 2 1


Sample Output 2
6

Given three cards (one can move 2 steps, two can move 1 steps), we have three choices: path one is 1 -> 2 -> 1 -> 1, path two is 1 -> 2 -> 2 -> 1, path three is 1 -> 1 -> 2 -> 1. The maximum score is 6, which is the output.


题目大意:给定一个1*N的棋谱,每一格都给定一个得分。现在有4种步长的牌,步长分别为1到4,举例来说,步长为1的牌表示棋子向前走1格,依次类推。现在给你M张牌,每张牌只能用一次,这M张牌的步长之和为N-1,也就是说正好能让你从第1格跳到最后一格。


问题:找一种出牌顺序使得跳转后棋子落点的得分之和最高。输出最高得分。


解题思路:
1) 暴力解法,给出M张牌的全排列,共M!中排列,然后求出M!种排列的得分,取最大值输出。
2) 动态规划。由题可知M张牌最多就4中走法:1步,2步,3步,4步。
假设
(1) 1*N的棋谱上的得分用数组scores[N]来表示(为了方便解释,这个数组从1开始,而非0)。

(2)M张牌中各步长的牌数分布为:

{1:m1, 2:m2, 3:m3, 4:m4}, 
m1+m2+m3+m4=M

(3)用MAX(X)表示从第1个跳到第X格的最高得分,我们要求的是MAX(N)

我们试想一下:

要想跳到第N格,那跳转之前棋子一定在第{N-1,N-2,N-3,N-4}格中的某个格子上。由于第N格的得分scores[N]一定,所以我们只需确定从第1格到上面4格的最高得分,即可计算出全局的最大得分。因此,MAX(N)可表示成:
MAX(N) = max{MAX(N-1), MAX(N-2), MAX(N-3), MAX(N-4)}

按照上面场景以此向前递推,直到返回到第1个格子位置。则本题动态规划递推式表示如下:
第1格到第K格的最高的分:
MAX(K) = max{MAX(K-i)|i=1,2,3,4 , 如果mi=0,则没有相应的MAX(K-i)}


下面我的程序代码:
        /**
	 * MM_Chess
	 * @param N		棋谱长
	 * @param M		步长数组长度
	 * @param scores	棋谱得分数组
	 * @param steps		步长数组
	 * @author Harry Huang
	 */
	public static void MM_Chess(int N, int M, int[]scores, List<Integer>steps){
		
		//统计步长分布
		Map<Integer, Integer> stepMap = new HashMap<Integer, Integer>();
		for(Integer step : steps){
			if(stepMap.containsKey(step))
				stepMap.put(step, stepMap.get(step)+1);
			else
				stepMap.put(step, 1);
		}
		
		//动归搜全局最优解
		int maxResult = getMAX_X(N-1, scores, stepMap);
		System.out.println("动态规划答案:" + maxResult);
		
	}
	
	/**
	 * 求MAX(N)
	 * @param N		当前尾格索引	
	 * @param scores	得分数组
	 * @param stepMap	当前步长分布
	 * @return	从第1格到第N格的最大得分
	 * @author Harry Huang
	 */
	public static int getMAX_X(int N, int[]scores, Map<Integer, Integer> stepMap){
		//递归结束条件,返回到了第1格
		if(N==0){
			return scores[0];
		}
		
		int maxResult = 0;
		for(Entry<Integer, Integer> entry : stepMap.entrySet()){
			if(entry.getValue() != 0){	//判断是否还有该步长的牌
				//还有牌的话,可以继续向前递归,更新步长分布
				Map<Integer, Integer> newMap = new HashMap<Integer, Integer>(stepMap);
				newMap.put(entry.getKey(), entry.getValue()-1);
				//动归
				int result = getMAX_X(N-entry.getKey(), scores, newMap);
				//判断取max{MAX[N-1],MAX[N-2],MAX[N-3],MAX[N-4]}
				if((scores[N]+result) > maxResult){
					maxResult = scores[N]+result;
				}
			}
		}
		return maxResult;
	}



测试用例结果:
4 2
1 2 1 2
1 2
标准答案(暴力解法):5
动态规划答案:5


5 3
1 2 1 2 1
1 2 1
标准答案(暴力解法):6
动态规划答案:6


18 7
1 4 5 4 5 1 2 8 3 10 1 2 3 4 6 9 1 3
3 2 1 4 1 3 3
标准答案(暴力解法):42
动态规划答案:42

你可能感兴趣的:(动态规划,校园招聘,Amazon,亚马逊)