Skip to content

Instantly share code, notes, and snippets.

@luckypapa
Last active September 29, 2015 05:14
Show Gist options
  • Save luckypapa/a7f28133d68bdd49d675 to your computer and use it in GitHub Desktop.
Save luckypapa/a7f28133d68bdd49d675 to your computer and use it in GitHub Desktop.
//https://algospot.com/judge/problem/read/DIAMONDPATH
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
int diamond[201][101];
int cache[201][101];
int N;
int main(void) {
int T;
cin >> T;
while (T-- > 0) {
memset(cache, 0, sizeof(cache));
cin >> N;
for (int i = 0; i < 2*N-1; i++) {
if (i < N) {
for (int j = 0; j < i+1; j++) {
cin >> diamond[i][j];
if (i == 0)
cache[i][j] = diamond[i][j];
else
cache[i][j] = diamond[i][j] + max(cache[i-1][j], cache[i-1][j-1]);
}
} else {
for (int j = 0; j < 2*N-i-1; j++) {
cin >> diamond[i][j];
cache[i][j] = diamond[i][j] + max(cache[i-1][j], cache[i-1][j+1]);
}
}
}
cout << cache[2*N-2][0] << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment