UVA 1291-Dance Dance Revolution(DP)

题目大意:有4个位置,每次从一个位置移动到另一个位置需要特定花费,求一串指定序列的最短花费。每次只能一只脚移动,或移动并踩下。

用d[i][j][u]表示前i个位置完成且在(j,u)的状态最小花费是多少,(j,u)其中之一必须和a[i]相等,假设是j和a[i]相等,那么可能上一个状态也是(j,u),或者上一个状态是(k,u),注意上一个状态不能是(j,k),因为这样需要左脚踩踏,右脚移动,不合法。至于移动的到底是左脚还是右脚,不用关心,因为总是可以进行转移。

状态转移方程:

d[i][j][u]=min { d[i-1][j][u]+1,min {d[i-1][k][u]+cost[j][k] } }(j==a[i])

d[i][j][u]=min { d[i-1][j][u]+1,min {d[i-1][j][k]+cost[k][u] } }(u==a[i])

#include<stdio.h>
#include<stdlib.h>
int d[2][7][7];
int cost[5][5]={{1,2,2,2,2},{2,1,3,4,3},{2,3,1,3,4},{2,4,3,1,3},{2,3,4,3,1}};
int main(void)
{
	int i,j,u,cur,min,minp;
	scanf("%d",&u);
	while(u!=0)
	{
		for(i=0;i<5;i++)
		{
			for(j=0;j<5;j++)
			{
				d[0][i][j]=(1<<29);
			}
		}
		cur=0;
		d[0][0][u]=d[0][u][0]=2;
		scanf("%d",&u);
		while(u!=0)
		{
			cur^=1;
			for(i=0;i<5;i++)
			{
				for(j=0;j<5;j++)
				{
					d[cur][i][j]=(1<<29);
				}
			}
			for(i=0;i<5;i++)
			{
				if(i!=u)
				{
					minp=d[cur^1][i][u]+1;
					for(j=0;j<5;j++)
					{
						if(j!=i)
						{
							minp=((d[cur^1][i][j]+cost[j][u]<minp)?(d[cur^1][i][j]+cost[j][u]):minp);
						}
					}
					d[cur][i][u]=d[cur][u][i]=minp;
				}
			}
			d[cur][u][u]=(1<<29);
			scanf("%d",&u);
		}
		min=(1<<29);
		for(i=0;i<5;i++)
		{
			for(j=0;j<5;j++)
			{
				if(d[cur][i][j]<min)
				{
					min=d[cur][i][j];
				}
			}
		}
		printf("%d\n",min);
		scanf("%d",&u);
	}
	return 0;
}
时间: 2024-07-28 16:34:46

UVA 1291-Dance Dance Revolution(DP)的相关文章

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

UVA 10564-Paths through the Hourglass(DP)

题目大意:给出一个沙漏,包含一个倒三角和一个三角,每个方格有一个0到9的数字,从顶上某格走到底下某格,求得到一个特殊值的路径有多少条,并输出字典序最小的.路径用一个起点和一系列'L'和'R'的字符表示. 用a[i][j]表示第i行第j列的数字.由于要求字典序最小的,所以从下往上递推.用d[i][j][u]表示在(i,j)且和为u的路径有多少条,在上半部分是倒三角,可以由d[i+1][j][u-a[i][j]]和d[i+1][j+1][u-a[i][j]]相加递推而来,在下半部分是正三角,可以由d

uva 10313 Pay the Price (DP)

uva 10313 Pay the Price 题目大意:现在有300种面额的硬币(1~300),给出一个金额数,问这300种面额的硬币组成该金额数的方式有多少种.注意:该题的输入有三种模式,1个数n:n为金额数:2个数n, a:n为金额数,a为硬币个数上限:3个数n, a,b:n为金额数,a b为硬币个数的下限和上限. 解题思路:dp[i][j]表示面额i的金额,在硬币数不超过j的情况下,有几种组成方式.这个题目涉及到一个结论,用不超过i个硬币凑出面值j的方案种数,是和用面值不超过i的硬币凑出

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

Uva 437-The Tower of Babylon(DP)

题目链接:点击打开链接 DAG上最长路. 题意:给出n种长方体,(每种无限),要求能摞的最大高度.连边是大的连小的. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio&g

UVA 10891 Game of Sum(DP)

This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at

uva 507 Jill Rides Again (DP)

uva 507 Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her

uva 10739 String to Palindrome (dp)

uva 10739 String to Palindrome In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below: Here you'd have the ultimate freedom. You are allowed to: Add any character at a

uva 11584 Partitioning by Palindromes(dp)

题目链接 题意:给定一个字符串,分解成多个子串,每个子串都是回文串,问最少能分成多少个子串. 题解: dp[i]表示前i个字符串分割成最少回文子串的数量: 0<=j<=i;如果字符串从j到i是回文串,那么dp[i]=min(dp[i],dp[j-1]+1); #include <iostream> using namespace std; int dp[1005]; string s; bool ok(int j,int i) { while(j<=i) { if(s[j]!