"NOIP2010" "P1317" turtle chess

Source: Internet
Author: User

Seems like a search for DP (should also be written in search)

Original question:

Xiao Ming's birthday, his father gave him a pair of turtle chess as a gift.
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?

For 100% of the data is 1≤n≤350,1≤m≤120, and 4 kinds of crawling cards, each card number of cards will not
More than 40;0≤ai≤100,1≤i≤n;1≤bi≤4,1≤i≤m.

At first, I thought it was a backpack, but this problem can not be solved with a backpack, because the value of the items in the backpack is fixed, in order to the total value has no effect, but here if the card as an item, the value of this item will be based on the previous card use order changes, so can not use the backpack

The idea is to open a four-dimensional F, the Quad Loop enumerates how many current cards are used, and then transfers the maximum value from the previous layer, plus a (i+j*2+p*3+q*4+1).

should also be able to use search to solve, but even if the search is essentially DP, but the above with for is based on the results obtained from the optimization of the present, the search is based on the current value optimization, the two code complexity and time complexity should be similar

Code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 intf[ -][ -][ -][ -];8 intn,m,a[1100],num[5];9 intMain () {//freopen ("ddd.in", "R", stdin);TenCin>>n>>m; One      for(intI=1; i<=n;i++) cin>>A[i]; A     int_id; -      for(intI=1; i<=m;i++) {cin>>_id; num[_id]++;} -      for(intI=1; i<=num[1]+1; i++) the          for(intj=1; j<=num[2]+1; j + +) -              for(intp=1;p <=num[3]+1;p + +) -                  for(intq=1; q<=num[4]+1; q++){ -F[i][j][p][q]=max (f[i][j][p][q],f[i-1][j][p][q]); +F[i][j][p][q]=max (f[i][j][p][q],f[i][j-1][p][q]); -F[i][j][p][q]=max (f[i][j][p][q],f[i][j][p-1][q]); +F[i][j][p][q]=max (f[i][j][p][q],f[i][j][p][q-1]); Af[i][j][p][q]+=a[i-1+ (J-1)*2+ (P-1)*3+ (Q-1)*4+1]; at                 } -cout<<f[num[1]+1][num[2]+1][num[3]+1][num[4]+1]<<Endl; -     return 0; -}
View Code

"NOIP2010" "P1317" turtle chess

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.