lunes, 20 de febrero de 2012

Problem Euler 18 y 67

Empezando con DP
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class problem18 
{
 public static void main(String[] args) throws FileNotFoundException {
  int m[][]=readArch();
  for (int i = m.length-2; i >=0; i--) 
  { 
   for (int j = 0; j <=i; j++) {
    
    m[i][j]+=Math.max(m[i+1][j+1], m[i+1][j]);
   }
  }
  System.out.println(m[0][0]);
 }

 private static int[][] readArch() throws FileNotFoundException 
 {
  int triangulo[][]= new int [150][150];
  Scanner sc = new Scanner (new File("C:\\triangle1.txt"));
  int co=0,fi=0;
  while(sc.hasNextLine())
  {
   String linea = sc.nextLine();
   String v[]= linea.split(" ");
   for (int i = 0; i < v.length; i++) 
   { 
    co=i;
    triangulo[fi][co]=Integer.parseInt(v[i]);  
   } 
   fi++;
  }
  return triangulo;
 }
}

No hay comentarios:

Publicar un comentario