Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  3.  * @version 4 APRIL 2013
  4.  *
  5.  * Reference : Muhammad Norzariman Bin Razari
  6.  *           : http://code.jasonbhill.com/python/project-euler-problem-67/
  7.  *              The sums traveling from top to bottom will be the same as the sums traveling from bottom to top. Start at the second to bottom row, and for each number N in that row, do the following: Take the maximum of the numbers directly down and left or down and right from N, and add that to N. Now do the same for the third-to-last row, then fourth-to-last, and so on. We’re modifying the triangle as we go to produce maximum partial sums from the bottom, and the last stage will be to replace to top number in the triangle, which will then be the maximum sum. So easy.
  8. */
  9. import java.io.*;//File & BufferedReader
  10. import java.util.*;//Scanner
  11. public class solution67
  12. {      
  13.     public static void main(String [] args) throws IOException
  14.     {
  15.         //time start
  16.         long begin = System.currentTimeMillis();
  17.        
  18.         //get input
  19.         //String inputFile = "testinput2.txt";
  20.         String inputFile = "triangle.txt";
  21.         Scanner sc = new Scanner(new FileReader(inputFile));
  22.         ArrayList list = new ArrayList();
  23.  
  24.         while(sc.hasNext())
  25.         {
  26.             list.add(sc.nextInt());
  27.         }
  28.         //there seems to be waste of space, but I can't think of a way to look for size of the triangle.
  29.         final int MAX = list.size();
  30.         int triangle[][] = new int[MAX][MAX];
  31.         for (int i=0, count=0; count<MAX;i++)
  32.         {
  33.             for (int j=0; j<=i&&count<MAX; j++)
  34.             {  
  35.                 triangle[i][j] = (int)list.get(count);
  36.                 count++;
  37.             }
  38.         }
  39.         //display array
  40.         /*for (int i=0; i<list.size(); i++)
  41.         {
  42.             for (int j=0; j<list.size(); j++)
  43.             {   if(triangle[i][j]!=0)
  44.                 System.out.print(triangle[i][j]+" ");}
  45.             System.out.println();
  46.         }*/
  47.  
  48.         //perform the triangle max thingy
  49.  
  50.         for (int i=MAX-2; i>=0; i--)
  51.         {
  52.             for(int j=0; j<=i; j++)
  53.             {
  54.                 //triangle[i][j] = triangle[i][j] + Math.max(triangle[i+1][j] , triangle[i+1][j+1]);
  55.                 triangle[i][j] += Math.max(triangle[i+1][j] , triangle[i+1][j+1]);
  56.             }
  57.         }
  58.         //display final triangle as answer.
  59.         System.out.println(triangle[0][0]);
  60.        
  61.         //time end
  62.         long end = System.currentTimeMillis();
  63.         System.out.println("Time:" + (end-begin)+" ms");
  64.     }
  65. }