hdu6739 dp

Portal: hdu6739
topic is that there are many skills you want to use some kind of skills needed to collect certain combinations (unordered combination) contains QWE entitled to ask you a press a sequence of skills, called you at least a few clicks secondary skills can press these skills (each episode together a combination of skills needed to release press R);
this is a dynamic programming problem, do not be fooled by the title, though it only needs the skills Yaoan a disorderly combination , but because of you this order by different result in different times by later, they still want to see him as an orderly, that is a skill such as to collect QWE to release.
Put him as, QWE, QEW, WQE, WEQ , EQW, EWQ is a skill that can put six combinations can be put out by him.
Because then you can only topic explained that there are three skills, then a will for the first time by the skills squeezed out. So we designed this state.
f [i] [x] [ y] [z], i represents the current into the first few skills, x represents three skill who currently inside the first press is, y represents the current three skill inside the first press who is, z who currently represent three skills inside the first press is that the value of the entire array is to reach this skill, skill set is the order of these skills Yaoan several times.
Expressed by 1 Q, represented by 2 W, represented by E 3;
such as F [2] [1] [2] [3]; second skill is put out, is first put skill Q, the second skills are put W, the third time is the number of E put the skills needed.
For each skill has six kinds of skill sets pressing him out, and then we went on a corresponding state is like, we have to complete the combination, there are press 0, press once, twice, press three times. (Refers to skills did not count R);
the example above, the press, we press is the third to put the skills E, according to press down first before squeezing out SKILLS
Therefore, f [i] [1] [ 2] [3] = min (f [i-1] [x] [1] [2]); X enumeration went on it, because we press a skill is finished crowding out first before, but since we press only once, so the second will be the first press is required by the state before the state of press time, before the second is required by the state of the state the second time you click. So to transfer it,
in fact, say it is dynamic programming, in fact, is not a violent process of enumeration of all states is not it?
Finally, run 468ms, it still is more.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int max_ = 1000000 + 50;
inline int read() {
	int s = 0, f = 1;
	char ch = getchar();
	while (ch<'0' || ch>'9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9') {
		s = s * 10 + ch - '0';
		ch = getchar();
	}
	return s * f;
}
void fuzhi(int &a, int &b, int &c ,char ch) {
	if (ch == 'Y')a = 1, b = 1, c = 1;
	if (ch == 'V')a = 1, b = 1, c = 2;
	if (ch == 'G')a = 1, b = 1, c = 3;
	if (ch == 'C')a = 2, b = 2, c = 2;
	if (ch == 'X') a = 1, b = 2, c = 2;
	if (ch == 'Z')a = 2, b = 2, c = 3;
	if (ch == 'T')a = 3, b = 3, c = 3;
	if (ch == 'F')a = 1, b = 3, c = 3;
	if (ch == 'D')a = 2, b = 3, c = 3;
	if (ch == 'B')a = 1, b = 2, c = 3;
}
char yuan[max_];
int f[max_][5][5][5];
inline void funtion(int i,int a, int b, int c, int aim) {
	if (aim == 1) {
		//按一次
		for (int x = 1; x <= 3; x++)
			f[i][a][b][c] = min(f[i - 1][x][a][b] + 2, f[i][a][b][c]);
	}
	if (aim == 2) {
		//按两次
		for (int x = 1; x <= 3; x++) {
			for (int y = 1; y <= 3; y++) {
				f[i][a][b][c] = min(f[i][a][b][c], f[i - 1][x][y][a] + 3);
			}
		}
	}
	if (aim == 3) {
		//按三次
		for (int x = 1; x <= 3; x++) {
			for (int y = 1; y <= 3; y++) {
				for (int z = 1; z <= 3; z++) {
					f[i][a][b][c] = min(f[i][a][b][c], f[i - 1][x][y][z] + 4);
				}
			}
		}
	}
}
int main() {
	while (~scanf_s("%s", yuan)) {
		int a, b, c, len = strlen(yuan) - 1;
		for (int i = 0; i <= len + 1; i++)
			for (int x = 1; x <= 3; x++)
				for (int y = 1; y <= 3; y++)
					for (int z = 1; z <= 3; z++)
						f[i][x][y][z] = 1e7;
		fuzhi(a, b, c, yuan[0]);
		f[0][a][b][c] = 4; f[0][a][c][b] = 4; f[0][b][a][c] = 4; f[0][b][c][a] = 4; f[0][c][a][b] = 4; f[0][c][b][a] = 4;
		for (int i = 1; i <= len; i++) {
			fuzhi(a, b, c, yuan[i]);
			funtion(i, a, b, c, 1); funtion(i, a, b, c, 2); funtion(i, a, b, c, 3); f[i][a][b][c] = min(f[i][a][b][c], f[i - 1][a][b][c] + 1);
			funtion(i, a, c, b, 1); funtion(i, a, c, b, 2); funtion(i, a, c, b, 3); f[i][a][c][b] = min(f[i][a][c][b], f[i - 1][a][c][b] + 1);
			funtion(i, b, a, c, 1); funtion(i, b, a, c, 2); funtion(i, b, a, c, 3); f[i][b][a][c] = min(f[i][b][a][c], f[i - 1][b][a][c] + 1);
			funtion(i, b, c, a, 1); funtion(i, b, c, a, 2); funtion(i, b, c, a, 3); f[i][b][c][a] = min(f[i][b][c][a], f[i - 1][b][c][a] + 1);
			funtion(i, c, a, b, 1); funtion(i, c, a, b, 2); funtion(i, c, a, b, 3); f[i][c][a][b] = min(f[i][c][a][b], f[i - 1][c][a][b] + 1);
			funtion(i, c, b, a, 1); funtion(i, c, b, a, 2); funtion(i, c, b, a, 3); f[i][c][b][a] = min(f[i][c][b][a], f[i - 1][c][b][a] + 1);
		}
		fuzhi(a, b, c, yuan[len]);
		int ans = 1e9;
		ans = min(min(min(f[len][a][b][c], f[len][a][c][b]), min(f[len][b][a][c], f[len][b][c][a])), min(f[len][c][a][b], f[len][c][b][a]));
		cout << ans << endl;
	}

	
	return 0;

}
Published 32 original articles · won praise 3 · Views 661

Guess you like

Origin blog.csdn.net/qq_43804974/article/details/102622177