#define max(a,b) a > b ? a : b
이렇게 algorithm의 std::max를 정의 해서 사용을 해보았는데요.
결과값이 맞지 않게 나오네요 디버깅 해서 쫒아가봤는데
왜 cache[3][1]이 7로 들어가는지 모르겠습니다 .ㅠㅠ
**정의하지 않고 algorithm의 max를 사용할 경우는 결과값이 맞게 나옵니다.
#include<cstdio>
#include<cstring>
#define max(a,b) a > b ? a : b
int n, triangle[100][100];
int cache[100][100];
int path(int y, int x){
if (y == n - 1) return triangle[y][x];
int& ret = cache[y][x];
if (ret != -1) return ret;
return ret =max(path(y + 1, x), path(y + 1, x + 1)) + triangle[y][x];
}
int main(){
int testCase; scanf("%d", &testCase);
while (testCase--){
memset(cache, -1, sizeof(cache));
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
scanf("%d", &triangle[i][j]);
printf("%d\n", path(0, 0));
}
}
vi1409
[[problem:TRIANGLEPATH]]
#define max(a,b) a > b ? a : b
이렇게 algorithm의 std::max를 정의 해서 사용을 해보았는데요.
결과값이 맞지 않게 나오네요 디버깅 해서 쫒아가봤는데
왜 cache[3][1]이 7로 들어가는지 모르겠습니다 .ㅠㅠ
**정의하지 않고 algorithm의 max를 사용할 경우는 결과값이 맞게 나옵니다.
12년 전