标签:
5
1
2 3
4 5 6
10 1 7 8
1 1 4 5 6
10
/* 每一次它都可以朝左、右、左上、右上四个方向走 !!!!!!!!!!!!!!!! 每一次它都可以朝左、右、左上、上四个方向走 不是右上是上!!!!!!!!!!!!!!!! */ #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; const int maxn = 987654321; int n,h[1050][1050],dp[1050][1050]; int main(){ cin>>n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= i;j++){ scanf("%d",&h[i][j]); } } for(int i = 0;i <= n + 1;i++){ for(int j = 0;j <= 1000;j++){ dp[i][j] = maxn; } } dp[n][1] = 0; for(int i = n;i >= 1;i--){ for(int j = 1;j <= i;j++){ if(j == 1) dp[i][j] = min(min(dp[i][j],dp[i+1][j] + h[i+1][j]),min(dp[i+1][i+1] + h[i+1][i+1],dp[i+1][j+1] + h[i+1][j+1])); else if(j == i) dp[i][j] = min(dp[i][j],min(dp[i+1][1] + h[i+1][1],min(dp[i+1][j+1] + h[i+1][j+1],dp[i+1][j] + h[i+1][j]))); else dp[i][j] = min(dp[i][j],min(dp[i+1][j+1] + h[i+1][j+1],dp[i+1][j] + h[i+1][j])); } dp[i][1] = min(dp[i][1],dp[i][i] + h[i][i]); for(int j = 2;j <= i;j++) dp[i][j] = min(dp[i][j],dp[i][j-1] + h[i][j-1]); dp[i][n] = min(dp[i][n],dp[i][1] + h[i][1]); for(int j = n - 1;j >= 1;j--) dp[i][j] = min(dp[i][j],dp[i][j+1] + h[i][j+1]); } cout<<dp[1][1] + h[1][1]; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5791395.html