Amazon Campus(2013-Sep-22)Question 2 / 2 (Amazon Campus(9): MM-Chess)
来源:互联网 发布:历史风速数据下载 编辑:程序博客网 时间:2024/05/21 20:32
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.
static int MMchess(int[] Nscores, int[] Mnumbers) { // Nscores : the scores on the the board (the i-th integer corresponds to the score on the i-th grid) // Mnumbers : the numbers on the M cards // return value : the most score that player can get if(Mnumbers.length == 0) return Nscores[0]; int max = 0; for(int i=0; i<Mnumbers.length; i++) { int score = Nscores[0]; int []tempNscores = new int[Nscores.length-Mnumbers[i]]; for(int j=Mnumbers[i],k=0; j<Nscores.length; j++) { tempNscores[k] = Nscores[j]; k++; } int []tempMnumbers = new int[Mnumbers.length-1]; for(int j=0,k=0; j<Mnumbers.length; j++) { if(j==i) continue; tempMnumbers[k] = Mnumbers[j]; k++; } score += MMchess(tempNscores, tempMnumbers); if(max < score) max = score; } return max; }
上述采用递归,大数据量时TLE。可采用动态规划:
dp[type1][type2][type3][type4]=max{dp[type1-1][type2][type3][type4]+sorce,dp[type1][type2-1][type3][type4]+score,dp[type1][type2][type3-1][type4]+score,dp[type1][type2][type3][type4-1]+score}具体实现参考:http://www.cnblogs.com/sourcecode2013/p/3276167.html
- Amazon Campus(2013-Sep-22)Question 2 / 2 (Amazon Campus(9): MM-Chess)
- Amazon Campus(2013-Sep-22)Question 1 / 2 (Amazon Campus(5): completely inside interval)
- Amazon Campus(2013-Sep-24)Question 1 / 2 (Amazon Campus(15): Clock wise walk)
- Amazon Campus(2013-Sep-24)Question 2 / 2 (Amazon Campus(17):Find the differences of items in amazon)
- Amazon Campus(2013-Sep-25)
- Amazon Hiring Campus 2013
- 亚马逊在线技术笔试(Amazon Hiring Campus 2013 - Final 6)
- Campus
- Amazon云产品介绍(2) - Amazon SimpleDB
- Amazon
- Google 2013 campus recruitment
- Amazon 發表Kindle 2電子書閱讀器
- Amazon面经2016/2
- Cisco 2013 Campus Hiring Position
- hdu 4804 Campus Design (2013 南京 轮廓线dp)
- 苹果新总部Apple Campus 2内部设计图曝光(多图)
- POJ--3697[USTC campus network] BFS+hash(O(N^2))
- Amazon Simple Queue Service(Amazon SQS)
- 关于Xcode4.3+禁用UseAutolayout
- 3d 建模工具
- 无限互联iOS项目视频教程 新浪微博2.8.实现主题Label_在线视频观看
- 边框粗细Weight属性的值怎么不是1、2、3呢
- Sql Server 去除表中数据的空格 脚本
- Amazon Campus(2013-Sep-22)Question 2 / 2 (Amazon Campus(9): MM-Chess)
- paip.c++ 正则表达式的应用跟普通正则表达式的区别以及特别注意点总
- 使用JIRA搭建企业问题跟踪系统
- 使用head中的base标签使保存的网页显示完整内容
- Umeng日志记录和版本升级
- 【可爱动物壁纸☆U☆好盟好天真哦】
- JSP URL重写-urlrewrite
- Windows GDI、DC
- 越狱的iphone4升级IOS7报3194问题解决过程