hdu 6739: Invoker(动态规划)
Problem Description
In dota2, there is a hero named Invoker. He has 3 basic skills in the game, which are Quas, Wex and Exort. Once he launches a basic skill, he will gain the corresponding element, where Quas gives "Q", Wex gives "W" and Exort gives "E".
Invoker can't have more than 3 elements simultaneously. If he launches a basic skill when he already owns 3 elements, he will get the corresponding element and lose the element he gained the earliest.
As can be seen, there are 10 unordered combinations of 3 elements in 3 types, each represents a special skill, which are as follows:
- Cold Snap: unordered element combination "QQQ", denoted by "Y"
- Ghost Walk: unordered element combination "QQW", denoted by "V"
- Ice Wall: unordered element combination "QQE", denoted by "G"
- EMP: unordered element combination "WWW", denoted by "C"
- Tornado: unordered element combination "QWW", denoted by "X"
- Alacrity: unordered element combination "WWE", denoted by "Z"
- Sun Strike: unordered element combination "EEE", denoted by "T"
- Forge Spirit: unordered element combination "QEE", denoted by "F"
- Chaos Meteor: unordered element combination "WEE", denoted by "D"
- Deafening Blast: unordered element combination "QWE", denoted by "B"
- When Invoker owns 3 elements, he can launch the invoking skill, denoted by "R", to gain the special skill according to the elements he currently owns. After invoking, the elements won't disappear, and the chronological order of the 3 elements won't change.
Now given a sequence of special skills, you want to invoke them one by one with using the minimum number of basic skills(Q,W,E) and invoking skill(R). Print the minimum number in a single line.
At the beginning, Invoker owns no elements. And you should re-invoke the special skills even if you have already invoked the same skills just now.
Input
Input a single line containing a string s (1 ≤ |s| ≤ 100 000) that only contains uppercase letters in {B, C, D, F, G, T, V, X, Y, Z}, denoting the sequence of special skills.
Output
Output a single line containing a positive integer, denoting the minimum number of skills to launch.
Sample Input
XDTBVV
Sample Output
15
Hint
One possible scheme is QWWREERERWQRQRR.
题意分析:
一个英雄每次按Q,W,E键就会出现一个相应的魔法球,三个魔法球会组合成技能,按R键释放,释放技能后魔法球不会消失,魔法球最多存在3个,召唤新的魔法球后,最先出现的魔法球会消失,
解题思路:
每种技能有3个魔法球组成,而不分先后顺序,用0,1,2代表这3个字母,那么按键顺序一共有6种:
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0
如果前两个位置的按键确定了,第三个按键也就确定了.
用dp[i][0][1]代表第i个技能的魔法球顺序是 0, 1, 2. dp[i][0][1]的值就是 第 i-1 个技能6个不同释放顺序加上对应的还需要按键的最小值,程序中的f(a, b, c, d, e, f)就是求第i个技能的魔法球顺序是a, b, c, 第i-1个技能的魔法球顺序是d, e, f 时还需要按键多少次,更新第i个技能的6种施放顺序, 最后加上每个技能施放要按len次R键, 注意多实例.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+120;
char str[N];
int i, dp[N][3][3];
map<char, string>maps;
int f(int a, int b, int c, int d, int e, int f){
if(maps[str[i-1]][d] == maps[str[i]][a] &&
maps[str[i-1]][e] == maps[str[i]][b] &&
maps[str[i-1]][f] == maps[str[i]][c] )
return 0;
if(
maps[str[i-1]][e] == maps[str[i]][a] &&
maps[str[i-1]][f] == maps[str[i]][b] )
return 1;
if(maps[str[i-1]][f] == maps[str[i]][a])
return 2;
return 3;
}
int main()
{
maps['Y']="QQQ";
maps['V']="QQW";
maps['G']="QQE";
maps['C']="WWW";
maps['X']="QWW";
maps['Z']="WWE";
maps['T']="EEE";
maps['F']="QEE";
maps['D']="WEE";
maps['B']="QWE";
while(scanf("%s", str)!=EOF) {
int lens=strlen(str);
dp[0][0][1] = dp[0][1][0] = dp[0][0][2] =
dp[0][2][0] = dp[0][1][2] = dp[0][2][1] = 3;
for(i=1; i<lens; i++) {
int mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(0, 1, 2, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(0, 1, 2, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(0, 1, 2, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(0, 1, 2, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(0, 1, 2, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(0, 1, 2, 2, 0, 1));
dp[i][0][1] = mini;
mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(0, 2, 1, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(0, 2, 1, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(0, 2, 1, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(0, 2, 1, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(0, 2, 1, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(0, 2, 1, 2, 0, 1));
dp[i][0][2] = mini;
mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(1, 0, 2, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(1, 0, 2, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(1, 0, 2, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(1, 0, 2, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(1, 0, 2, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(1, 0, 2, 2, 0, 1));
dp[i][1][0] = mini;
mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(1, 2, 0, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(1, 2, 0, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(1, 2, 0, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(1, 2, 0, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(1, 2, 0, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(1, 2, 0, 2, 0, 1));
dp[i][1][2] = mini;
mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(2, 1, 0, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(2, 1, 0, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(2, 1, 0, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(2, 1, 0, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(2, 1, 0, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(2, 1, 0, 2, 0, 1));
dp[i][2][1] = mini;
mini=99999999;
mini=min(mini, dp[i-1][0][1] + f(2, 0, 1, 0, 1, 2));
mini=min(mini, dp[i-1][0][2] + f(2, 0, 1, 0, 2, 1));
mini=min(mini, dp[i-1][1][0] + f(2, 0, 1, 1, 0, 2));
mini=min(mini, dp[i-1][1][2] + f(2, 0, 1, 1, 2, 0));
mini=min(mini, dp[i-1][2][1] + f(2, 0, 1, 2, 1, 0));
mini=min(mini, dp[i-1][2][0] + f(2, 0, 1, 2, 0, 1));
dp[i][2][0] = mini;
}
i--;
int ans=99999999;
ans = min(ans, dp[i][0][1]);
ans = min(ans, dp[i][0][2]);
ans = min(ans, dp[i][1][0]);
ans = min(ans, dp[i][1][2]);
ans = min(ans, dp[i][2][1]);
ans = min(ans, dp[i][2][0]);
printf("%d\n", ans+lens);
}
return 0;
}