请看一下这个问题" Dungeon Princess",https://www.interviewbit.com/problems/dungeon-princess/ 我试图通过自下而上的方法来解决它,但在少数情况下陷入困境,任何人都可以帮助我吗? 这是我的代码:
int min(int a, int b)
{
return a<b?a:b;
}
int max(int a, int b) {
return a>b?a:b;
}
int calculateMinimumHP(int** A, int n11, int n12) {
int arr[n11][n12], i, j, ans=0, minm[n11][n12], t1, t2;
memset(arr, 0, sizeof(arr));
memset(minm, 0, sizeof(minm));
arr[0][0]= A[0][0];
minm[0][0] = min(ans, A[0][0]);
for(i=1; i<n11; i++)
{
arr[i][0] = arr[i-1][0]+A[i][0];
minm[i][0]=min(arr[i][0], minm[i-1][0]);
}
for(i=1; i<n12; i++)
{
arr[0][i] = arr[0][i-1]+A[0][i];
minm[0][i]=min(arr[0][i], minm[0][i-1]);
}
for(i=1; i<n11; i++)
{
for(j=1; j<n12; j++)
{
t1 = min(minm[i-1][j], arr[i-1][j]+A[i][j]);
t2 = min(minm[i][j-1], arr[i][j-1]+A[i][j]);
if(t1 > t2)
{
minm[i][j] = t1;
arr[i][j] = A[i][j] + arr[i-1][j];
}
else
{
minm[i][j] = t2;
arr[i][j] = A[i][j] + arr[i][j-1];
}
}
}
ans = min(ans, minm[n11-1][n12-1]);
if(ans == -923)
return 923;
return abs(ans)+1;
}