Last active
September 29, 2015 05:14
-
-
Save luckypapa/a7f28133d68bdd49d675 to your computer and use it in GitHub Desktop.
soulution of https://algospot.com/judge/problem/read/DIAMONDPATH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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