Skip to content

Instantly share code, notes, and snippets.

@schweigert
Created August 23, 2015 20:12
Show Gist options
  • Select an option

  • Save schweigert/e38fccd89eaa4429e3f6 to your computer and use it in GitHub Desktop.

Select an option

Save schweigert/e38fccd89eaa4429e3f6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int Mat[100][100] = {0};
int Sun[100][100] = {0};
int Tam = 0;
void ResetSun (){
int i, j;
for (i = 0; i < 100; i++){
for (j = 0; j < 100; j++){
Sun [i][j] = -1;
}
}
}
int MaiorSoma (int i, int j){
if (i != 0 && Sun[i][j] != -1){
return Sun[i][j];
}
if (i < Tam){
int esq = Mat[i][j] + MaiorSoma(i+1,j);
int dir = Mat[i][j] + MaiorSoma(i+1,j+1);
if (esq > dir){
Sun [i][j] = esq;
return esq;
} else {
Sun [i][j] = dir;
return dir;
}
}else {
return 0;
}
}
int main (void) {
int i, j, k, s;
scanf ("%d", &s);
while (s--){
scanf ("%d", &k);
for (i = 0; i < k; i++){
for (j = 0; j <= i; j++){
scanf ("%d", &Mat[i][j]);
}
}
ResetSun ();
Tam = k;
printf ("%d\n", MaiorSoma(0,0));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment