Topic links
Analysis:
F[I][J] Represents the minimum number of steps to take to stack a stack of cards I through J.
D[I][J] Indicates the number of steps (the initial given state) to move the card I to the card J.
Take a bunch of cards 3~8 For example, we need to put the card 3 on the card 4, and in the optimal mobile scheme, the position of the card 4 is not sure, so we enumerate the position of the card 4 (because there are 10 cards, the enumeration is OK), so that the state transfer equation: f[3][8] = min (f[3][ 8], F[4][k] + f[k][8] + d[3][k]); (4 <= k <= 8). F[i][j] = min (F[i][j], f[i+1][k] + f[k][j] + d[i][k]);
For example: the initial order of cards is 1, 4, 6, 8, 3, 2, 5, 7, 9, 10
F[1][4]. The most order should be: first put the card 2 to the card 3, the card is moved to the card 4. Finally move the card 1 to the card 2. And this time Card 2 is already in the position of the card 4. (F[1][4] = f[2][4] + f[4][4] + d[1][4] = 5).
#include <iostream>#include <cstdio>#include <cstring>#include <string.h>#include <algorithm>#include <math.h>using namespace STD;intT, a[ the], d[ the][ the], f[ the][ the];voidDP () { for(inti =0; I <Ten; i++)//The interval is increasing, the interval of small distance is obtained first, and the solution of the optimal sub-problem is derived .{ for(intj =1; J <=Ten; J + +)//The minimum number of steps required to stack a bunch of cards J to i+j into a stack. {if(i + J >Ten)Continue; for(intK = j +1; K <= i + j; k++)//Enumerate where the previous card is locatedF[j][i+j] = min (f[j][i+j], f[j+1][K] + f[k][i+j] + d[j][k]); } }}voidInit () { for(inti =1; I <=Ten; i++)scanf("%d", &a[i]);memset(d,0,sizeof(d)); for(inti =1; I <=Ten; i++)//Pre-treatment of all distances{ for(intj =1; J <=Ten; J + +) {intx = A[i], y = a[j]; D[x][y] =CB1(I-J); D[Y][X] = D[x][y]; } }}intMain () {Cin>> T; while(t--) { for(inti =1; I <=Ten; i++) { for(intj =1; J <=Ten; J + +) {F[i][j] =10e8;if(i = = j) F[i][j] =0; }} Init (); DP ();printf("%d\n", f[1][Ten]); }return 0;}
hdoj1584 Spider-Card interval DP