您的位置:首页 > 其它

nyist 740 “炫舞家“ST(DP)

2016-05-05 11:27 295 查看
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=740

思路:dp[k][i][j]表示第k步一只脚在i这个数字上,一只脚在j这个数字上

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int cost[5][5];
int dp[10010][5][5];
int a[10010];
void init()
{
for(int i=1; i<=4; i++)
{
cost[0][i] = 2;
cost[i][i] = 1;
}
cost[1][3] = cost[3][1] = 4;
cost[2][4] = cost[4][2] = 4;
cost[1][2] = cost[2][1] = 3;
cost[2][3] = cost[3][2] = 3;
cost[3][4] = cost[4][3] = 3;
cost[1][4] = cost[4][1] = 3;
}
int main()
{
int x;
init();
while(scanf("%d",&x) && x)
{
int k = 1;
a[k++] = x;
while(scanf("%d",&x) && x)
a[k++] = x;
int m = k-1;
memset(dp,inf,sizeof(dp));
dp[1][a[1]][0] = dp[1][0][a[1]] = 2;
for(int k=2; k<=m; k++)
{
for(int j=0; j<=4; j++)
{
if(dp[k-1][a[k-1]][j] != 0)
{
dp[k][a[k]][j] = min(dp[k][a[k]][j],dp[k-1][a[k-1]][j] + cost[a[k-1]][a[k]]);//打印出来看,后面的值有可能覆盖前面的值,所以要每次取最小值
dp[k][a[k-1]][a[k]] =min(dp[k][a[k-1]][a[k]],dp[k-1][a[k-1]][j] + cost[j][a[k]]);
}
if(dp[k-1][j][a[k-1]] != 0)
{
dp[k][j][a[k]] = min(dp[k][j][a[k]],dp[k-1][j][a[k-1]] + cost[a[k-1]][a[k]]);
dp[k][a[k]][a[k-1]] = min(dp[k][a[k]][a[k-1]],dp[k-1][j][a[k-1]] + cost[j][a[k]]);
}
}
}
int max1 = inf;
for(int i=0; i<=4; i++)
{
max1 = min(max1,dp[m][a[m]][i]);
max1 = min(max1,dp[m][i][a[m]]);
}
printf("%d\n",max1);
}
return 0;
}
错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int cost[5][5];
int dp[10010][4][4];
int a[10010];
void init()
{
for(int i=1; i<=4; i++)
{
cost[0][i] = 2;
cost[i][i] = 1;
}
cost[1][3] = cost[3][1] = 4;
cost[2][4] = cost[4][2] = 4;
cost[1][2] = cost[2][1] = 3;
cost[2][3] = cost[3][2] = 3;
cost[3][4] = cost[4][3] = 3;
cost[1][4] = cost[4][1] = 3;
}
int main()
{
int x;
init();
while(scanf("%d",&x) && x)
{
int k = 1;
a[0] = 0;
a[k++] = x;
while(scanf("%d",&x) && x)
a[k++] = x;
int m = k-1;
memset(dp,0,sizeof(dp));
dp[1][a[1]][0] = dp[1][0][a[1]] = 2;
for(int k=2; k<=m; k++)
{
for(int j=0; j<=4; j++)
{
if(!dp[k-1][a[k-1]][j])
continue;
dp[k][a[k]][j] = dp[k-1][a[k-1]][j] + cost[a[k-1]][a[k]];
printf("%d %d %d %d\n",k,a[k],j, dp[k][a[k]][j]);
dp[k][a[k-1]][a[k]] = dp[k-1][a[k-1]][j] + cost[j][a[k]];
printf("%d %d %d %d\n",k,a[k-1],a[k], dp[k][a[k-1]][a[k]]);
if(!dp[k-1][j][a[k-1]])
continue;
dp[k][j][a[k]] = dp[k-1][j][a[k-1]] + cost[a[k-1]][a[k]];
printf("%d %d %d %d\n",k,j,a[k],dp[k][j][a[k]] );
dp[k][a[k]][a[k-1]] = dp[k-1][j][a[k-1]] + cost[j][a[k]];
printf("%d %d %d %d\n",k,a[k],a[k-1],dp[k][a[k]][a[k-1]]);
}
}
int max1 = inf;
for(int i=0; i<=4; i++)
{
if(!dp[m][a[m]][i])
continue;
printf("aaaa%d\n",dp[m][a[m]][i]);
max1 = min(max1,dp[m][a[m]][i]);
if(!dp[m][i][a[m]])
continue;
printf("aaaa%d\n",dp[m][i][a[m]]);
max1 = min(max1,dp[m][i][a[m]]);
}
printf("%d\n",max1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: