Difficulty: popularize +/improve
Topic Type: Dynamic planning
Number of submissions: 2
Related knowledge: Multidimensional dynamic regulation
Topic background
Xiao Ming's birthday, his father gave him a pair of turtle chess as a gift.
Title Description
The chess board is a row of n squares, one score per lattice (non-negative integer). The 1th grid is the only starting point, nth grid is the end, the game requires the player to control a turtle piece from the starting point to go to the end.
The M-card crawl card in Tortoise chess, divided into 4 different types (m card does not necessarily contain all 4 types of cards, see sample), each type of card is labeled with 1, 2, 3, 44 digits, indicating that after the use of this card, the turtle pieces will crawl forward the corresponding number of squares. In the game, each time the player needs to select a crawl card from all the crawl cards, and control the corresponding number of the turtle pieces forward, each card can only be used once.
In the game, the turtle piece automatically obtains the starting point lattice the fraction, and in the subsequent crawl each arrives a lattice, obtains the corresponding score of the lattice. The player's final game score is the sum of all the squares of the turtle pieces from the beginning to the end of the process.
Obviously, the use of different crawl cards in the order will make the final game score different, Xiaoming wants to find a card in order to make the final game score the most.
Now, tell the score of each lattice on the board and all the crawling cards, can you tell xiaoming how many points he can get?
Code:
1#include <iostream>2 using namespacestd;3 intscore[ -];4 intcard[4];5 intf[ A][ A][ A][ A];6 intN, M;7 intans;8 intMain () {9Cin>>n>>m;Ten intI, J, K, L; One for(i =1; I <= N; i++) ACin>>Score[i]; - for(i =1; I <= m; i++){ - intnum; theCin>>num; -card[num]++; - } - for(i =0; I <= card[1]; i++) + for(j =0; J <= card[2]; J + +) - for(k =0; K <= card[3]; k++) + for(L =0; L <= card[4]; l++){ A intMaxx =0; at if(i!=0&&f[i-1][j][k][l]>Maxx) -Maxx = f[i-1][j][k][l]; - if(j!=0&&f[i][j-1][k][l]>Maxx) -Maxx = f[i][j-1][k][l]; - if(k!=0&&f[i][j][k-1][l]>Maxx) -Maxx = f[i][j][k-1][l]; in if(l!=0&&f[i][j][k][l-1]>Maxx) -Maxx = f[i][j][k][l-1]; toF[i][j][k][l] = Maxx + score[1+i*1+j*2+k*3+l*4]; + } -cout<<f[card[1]][card[2]][card[3]][card[4]]; the return 0; *}
Note:
First wrote the DFS, the first sample of the past, but also feel at least half of the points can be cheated. The result was only one point.
Read the puzzle, I heard that this is a backpack moving rules, is probably a multi-backpack bar. F[i][j][k][l] means taking the I 1 card, J 2 Card, K 3 card and L 4 card when the maximum score, it is not difficult to find f[i][j][k][l] is equal to four kinds of cards, respectively, the maximum value of a single sheet plus the value of the current lattice. Do not consider the order, understandable, and seems a bit mysterious.
1541 Tortoise Chess