POJ1651 D - Multiplication Puzzle (DP动态规划)
Description
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
Input
Output
#include <stdio.h>
#define MAX_CARDS 100
int cardArr[MAX_CARDS+1];
int dp[MAX_CARDS+1][MAX_CARDS+1] = {0}; //³¤¶È´Óiµ½j
int main()
{
//freopen("input.txt","r",stdin);
int cardNumber = 0;
int i = 0;
int j = 0;
int k = 0;
scanf("%d",&cardNumber);
for(i=1;i<=cardNumber;i++)
{
scanf(" %d",&cardArr[i]);
}
for(i=1;i<cardNumber-1;i++)
{
dp[i][i+2] = cardArr[i] * cardArr[i+1] * cardArr[i+2];
}
for(i=4;i<=cardNumber;i++) //序列的长度
{
for(j=1;j<=cardNumber-i+1;j++)
{
int min = 0x7fffffff;
int temp;
for(k=j+1;k<j+i-1;k++)
{
temp = dp[j][k]+dp[k][j+i-1] + cardArr[j]*cardArr[k]*cardArr[j+i-1];
if(temp<min)
{
dp[j][j+i-1] = temp;
min = temp;
}
}
}
}
printf("%d\n",dp[1][cardNumber]);
return 0;
}