karlangadas

LAB1 Prob2 - Algoritmia 2016-2

Sep 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define M 10
  4. /*
  5.  * Aquí se usará recursión. Se supondrá que ya se a alcanzado el maximo puntaje en el movimiento anterior
  6.  */
  7.  
  8. int maxpuntaje(int tablero[M][M],int n,int m);
  9.  
  10. int main(int argc, char** argv) {
  11.     int n,m,i,j;
  12.     int tablero[M][M];
  13.    
  14.     tablero[M][M]=0;
  15.     //Recibimos los valores
  16.     while(scanf("%d %d",&n,&m)!=EOF){
  17.     for(i=0;i<n;i++){
  18.         for(j=0;j<m;j++)
  19.             scanf("%d",&tablero[i][j]);
  20.     }
  21.    
  22.     printf("%d\n",maxpuntaje(tablero,n,m));
  23.     }
  24.  
  25.     return (EXIT_SUCCESS);
  26. }
  27.  
  28. int maxpuntaje(int tablero[M][M],int n,int m){
  29.     int a,b;
  30.     //caso base
  31.     if((n==1)&&(m==1)){
  32.         return tablero[0][0];
  33.     }
  34.    
  35.     //cuando solo queda o una fila o una columna
  36.     if (n==1){
  37.        return tablero[n-1][m-1]+maxpuntaje(tablero,n,m-1);
  38.     }
  39.      if (m==1){
  40.        return tablero[n-1][m-1]+maxpuntaje(tablero,n-1,m);
  41.     }
  42.    
  43.     a=tablero[n-2][m-1];
  44.     b=tablero[n-1][m-2];
  45.    
  46.     if (a>=b)
  47.         return tablero[n-1][m-1]+maxpuntaje(tablero,n-1,m);
  48.     else
  49.         return tablero[n-1][m-1]+maxpuntaje(tablero,n,m-1);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment