UP.cpp DP(有漏洞)

直接代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1005;
int dp[maxn][25],a[25],n,m,zero,minn;
int DP(int x,int y){
    if(~dp[x][y])
        return dp[x][y];
    if(x==n)
        return dp[x][y]=0;
    dp[x][y]=minn=10000000;
    for(int i=0;i<m;i++){
        if(x+a[i]>n||x+a[i]<1)  continue;
        minn=min(minn,DP(x+a[i],i)+abs(a[i])*2+abs(y-i));
    }
    return dp[x][y]=minn;
}
int main(){
    //freopen("up.in","r",stdin);
    //freopen("up.out","w",stdout);
    memset(dp,-1,sizeof(dp)); 
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++,zero=a[i]==0?i:zero)
        scanf("%d",&a[i]);
    printf("%d",DP(1,zero));
    return 0;
}

By YOUSIKI

你可能感兴趣的:(UP.cpp DP(有漏洞))