Results 1 to 17 of 17
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default How to approach this problem?

    Hello, I am working on a new Euler Project Problem, but I'm not exactly sure how to approach it.

    The problem:
    In the 2020 grid below, four numbers along a diagonal line have been marked in red.

    08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
    49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
    81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
    52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
    22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
    24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
    32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
    67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
    24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
    21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
    78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
    16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
    86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
    19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
    04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
    88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
    04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
    20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
    20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
    01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
    The product of these numbers is 26 63 78 14 = 1788696.

    What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?
    This is what I have so far:
    Java Code:
    public class Problem011 {
    	public static void main (String[] args){
    		int x, y, z, a, b;
    		b = 0;
    		
    		int row1[]={8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8};
    		
    		for (x=0;x<row1.length-4;x++){
    			z=1;
    			y=0;
    			for (a=0;a<4;a++){
    				
    				y=x;
    				y+=a;
    				z*=row1[y];
    				
    			}
    			
    			if (z>b){
    				b=z;
    			}
    		}
    		System.out.println("b: " + b);
    	}
    
    }
    It finds the greatest product of four adjacent numbers along the row.

    But I don't know how to apply it to columns or diagonals?

    Cheers!

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: How to approach this problem?

    If you have an 20 by 20 two dimensional array, and you are working on the number at x=6, y=6, what values for x and y give you the adjacent numbers in each of the 8 directions? Now break it down, how do you get the next number in the same direction for a given direction?

    For what you've deduced so far, you've only worked on a row, which only varies the value of the x-index in one direction or the other (x-1 = left and x+1 = right).

  3. #3
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    Let shorten the problem for simplicity to a 3x3 square and you want the greatest product of two numbers...

    2 4 6
    7 6 5
    3 5 8


    Create an array to hold it:-

    Java Code:
    int[][] grid = new int[][] {  {2, 4, 6,}, {7,6,5}, {3,5,8}  } ;
    If you go through each number in the grid

    Java Code:
     for (int x=0; x<grid.length; x+){
       for (int y=0; y<grid.length; y+){
         //code goes here
       }
     }
    and then you multiply each number by that to it's right, it's bottom right, it's bottom and it's bottom left

    (why it's decided to add some google rubbish in here I don't know)
    Java Code:
     long pR = grid[x][y] * grid[x+1][y];
     long pBR = grid[x][y] * grid[x+1][y+1];
     long pB = grid[x][y] * grid[x][y+1];
     long pBL = grid[x][y] * grid[x-1][y+1];
    keep an overall max outside the loop and which direction the line goes, lets say 1=right, 2=down right, 3=down, 5 down left which you update everytime one of your products is greater than max

    Java Code:
     long max = 0;
     int maxX = -1;
     int maxY = -1;
     int maxD = -1;
    ...then add some error trapping so that you dont measure past the edges of the grid.

    But the easiest way to do this is recursivley.
    Last edited by k1ng; 04-26-2012 at 05:08 PM. Reason: explain the google thing

  4. #4
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Thanks jlczuk! I didn't know you could make multidimensional arrays!

    I have made new code to check all rows:

    Java Code:
    public class Problem011 {
    	public static void main (String[] args){
    		int x, y, z, a, b, c;
    		b = 0;
    		
    		int NumGrid[] []= new int [] [] 
    				{{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
    				{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
    				{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
    				{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
    				{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
    				{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
    				{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
    				{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
    				{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
    				{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
    				{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
    				{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
    				{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
    				{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
    				{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
    				{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
    				{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
    				{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
    				{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
    				{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48,}};
    		
    		for (c=0;c<20;c++){
    		
    			for (x=0;x<NumGrid.length-3;x++){
    				z=1;
    				y=0;
    				System.out.println(x);
    				
    				for (a=0;a<4;a++){
    					
    					y=x;
    					y+=a;
    					z*=NumGrid[a][y];
    					System.out.println("y: " + y + "\t NumGrid: " + NumGrid[0][y]);
    					
    				}
    				
    				System.out.println("z: " + z);
    				if (z>b){
    					b=z;
    				}
    			}
    			System.out.println("b: " + b);
    		}
    	}
    }
    I think I will have to make some new methods or else it will get too complicated.

    I am still not sure how to check for diagonals though. If I start at NumGrid [0][0] and then set up a loop to go to NumGrid [1][1], NumGrid[2][2], NumGrid[3][3], and get it to loop down the rows, it wouldn't get all the diagonal fours.

    I would have to check starting from each corner, going inwards, to not miss any.

    That seems pretty complicated and inefficient?

    Cheers!

    Edit:

    @King: Thanks! I missed your post before (had the page sitting open for awhile). Sorry I don't understand how you are saying I can get the diagonal 4 numbers? And can you explain what you mean by doing it recursively? I sort of know what recursion is(method that calls itself), but I've never done it so not sure how to implement it.

    Thanks!
    Last edited by Zigster; 04-26-2012 at 05:42 PM.

  5. #5
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    If you start at the top left and for every grid item you check TR,R,BR and B then you'll cover everything ( i said BL in my example, instead of TR but it doesn't matter)

    For every square you check 4 steps in each direction. Your naming is a little confusing, I'd just use x,y to step through the grid and a to keep track of the muliples step like so

    Java Code:
    int TR=1, R=1, BR=1, B=1;
    for (a=0;a<4;a++){
      TR *= NumGrid[x+a][y-a];
      R *= NumGrid[x+a][y];
      BR *= NumGrid[x+a][y+a];
      B *= NumGrid[x+a][y];
    }

  6. #6
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    As for recursion, it gets a little complex but it makes things simpler once you understand. It's probably too much to take in if you're stuggling with this problem but it would look something like...

    Java Code:
    computeProductLine(int x, int y, int xStep, int yStep, int noSteps){
      if (noSteps==0)
        return NumGrid[x][y];
     else
        return computerProductLine(x+xStep, y+yStep, xStep, yStep, noSteps-1) * NumGrid[x][y];
    }
    where you would only need to say

    Java Code:
     TR = computeProductLine(x,y,1,-1,4);
     R = computeProductLine(x,y,1,0,4);
     BR = computeProductLine(x,y,1,1,4);
     B = computeProductLine(x,y,0,1,4);
    it's handy, but wait till you can grasp this stuff before moving onto recursion ;)

  7. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Oohhhhh, I get you now!

    How do I "error trap"? With some nested if statements?

    Cheers!

  8. #8
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    yea, just make sure your new value (i.e. x+a, y-a, etc) isn't greater than the size of the board or less than zero.

  9. #9
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    I'm getting pretty confused:

    Java Code:
    public class Problem011 {
    	public static void main (String[] args){
    		int x, y, R, BR = 0, a, b, c;
    		int counter1, counter2; 
    		b = 0;
    		
    		int NumGrid[] []= new int [] [] 
    				{{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
    				{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
    				{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
    				{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
    				{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
    				{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
    				{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
    				{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
    				{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
    				{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
    				{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
    				{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
    				{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
    				{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
    				{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
    				{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
    				{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
    				{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
    				{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
    				{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48,}};
    		
    		for (c=0;c<20;c++){
    		
    			for (x=0;x<NumGrid.length;x++){
    				R=1;
    				y=0;
    				
    				for (a=0;a<4;a++){
    					
    					y=x;
    					y+=a;
    					
    					if(x<17){
    					
    					R*=NumGrid[c][y];
    					
    					}
    					
    					if (x<17 && c<17){
    						BR*=NumGrid[a+1][y];
    						System.out.println(NumGrid[a+1][y]);
    					}
    					
    				}
    				System.out.println("\n");
    				if (R>b){
    					b=R;
    				}
    			}
    			System.out.println("b: " + b);
    		}
    	}
    }
    BR stops when it prints: 4, 13, 36, 80. Which means it's stopping after the second row. I can't figure out why it's stopping?

    EDIT: Ok I figured it out, I was printing the wrong thing. Should have been NumGrid[x+a][y]. I can see the light at the end of the tunnel!

    EDIT: Ok, Think I have got it, but Project Euler website is down so I can't test it.
    Last edited by Zigster; 04-26-2012 at 08:18 PM.

  10. #10
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    Ah good. :)

  11. #11
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Well, the answer is wrong, but I can't see where I have made a mistake?

    Java Code:
    public class Problem011 {
    	public static void main (String[] args){
    		int R, BR, B, BL;
    		BR = 1;
    		B = 1;
    		BL = 1;
    		int y, a, b;
    		int Row, Column; 
    		b = 0;
    		
    		int NumGrid[] []= new int [] [] 
    				{{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
    				{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
    				{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
    				{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
    				{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
    				{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
    				{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
    				{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
    				{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
    				{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
    				{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
    				{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
    				{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
    				{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
    				{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
    				{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
    				{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
    				{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
    				{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
    				{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48,}};
    		
    		for (Row=0;Row<20;Row++){
    		
    			for (Column=0;Column<NumGrid.length;Column++){
    				R=1;
    				y=0;
    				
    				for (a=0;a<4;a++){
    					
    					y=Column;
    					y+=a;
    					
    					if(Column<17)
    					{
    					R*=NumGrid[Row][y];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[Row][y]);
    					}
    					
    					if (Column<17 && Row<17)
    					{
    					BR*=NumGrid[Row + a][y];
    					//System.out.println("a: " + a + "\ty: " + y + "\tNumGriday: " + NumGrid[Row + a][y]);
    					}
    					
    					if(Column<17)
    					{
    					B *= NumGrid[y][Row];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[y][Row]);
    					}
    					if(Row>3 && Column<17){
    					BL*=NumGrid[y][Row-a];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[Row - a][Column + a]);
    					}
    					
    				}
    				System.out.println("\n");
    				if (R>b){
    					b=R;
    				}
    				if (BR>b){
    					b=BR;
    				}
    				if (B>b){
    					b=B;
    				}
    				if (BL>b){
    					b=BL;
    				}
    			}
    			System.out.println("b: " + b);
    		}
    	}
    }

  12. #12
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: How to approach this problem?

    My suggestion is to add the debug tracing back in. For each product, identify the starting number's coordinates in the grid and print out the numbers being multiplied and the answer. You will end up with one line for each product. From there you can inspect to see if you are grabbing the correct numbers for each product. It should give you some clues.

  13. #13
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Thanks! I was so caught up in get the array stuff working I was ignoring the multiplication.

    Because I was not resetting B, BR, BL, R back to 1 at the beginning of the loop, they weren't resetting and kept adding up. Once I set them to 1 at the beginning of the loop I got the right answer.

    Java Code:
    public class Problem011 {
    	public static void main (String[] args){
    		long R, BR, B, BL, b;
    		int y, a;
    		int Row, Column; 
    		b = 0;
    		
    		int NumGrid[] []= new int [] [] 
    				{{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
    				{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
    				{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
    				{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
    				{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
    				{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
    				{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
    				{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
    				{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
    				{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
    				{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
    				{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
    				{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
    				{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
    				{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
    				{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
    				{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
    				{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
    				{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
    				{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48,}};
    		
    		for (Row=0;Row<20;Row++){
    		
    			for (Column=0;Column<NumGrid.length;Column++){
    				R=1;
    				y=0;
    				BL=1;		//This is what I changed
    				BR=1;
    				B=1;
    				R=1;
    				
    				for (a=0;a<4;a++){
    					
    					y=Column;
    					y+=a;
    					
    					if(Column<17)
    					{
    					R*=NumGrid[Row][y];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[Row][y]);
    					}
    					
    					if (Column<17 && Row<17)
    					{
    					BR*=NumGrid[Row + a][y];
    					//System.out.println("a: " + a + "\ty: " + y + "\tNumGriday: " + NumGrid[Row + a][y]);
    					}
    					
    					if(Column<17)
    					{
    					B *= NumGrid[y][Row];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[y][Row]);
    					}
    					
    					if(Row>3 && Column<17){
    					BL*=NumGrid[Row - a][Column + a];
    					//System.out.println("a: " + a + "\ty: " + "\tNumGriday: " + NumGrid[Row - a][Column + a] + "\tBL: " + BL);
    					}
    					
    				}
    				System.out.println("\n");
    				if (R>b){
    					b=R;
    				}
    				if (BR>b){
    					b=BR;
    				}
    				if (B>b){
    					b=B;
    				}
    				if (BL>b){
    					b=BL;
    				}
    			}
    			System.out.println("b: " + b);
    		}
    	}
    }
    Last edited by Zigster; 04-27-2012 at 08:26 AM.

  14. #14
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    Firstly, I recommend cutting down repetition of code. For example you have...

    Java Code:
     if(Column<17)
     {
        //R
      }
      if (Column<17 && Row<17)
      {
        //BR
      }
      if(Column<17)
      {
         //B
      }
      if(Row>3 && Column<17){
         //BL
      }
    when you could have

    Java Code:
     if (Column<17)
     {
       //R
       //B
    
       if (Row<17)
       {
         //BR
       }else if (Row>3){
         //BL
       }
     }

    Secondly, this...

    Java Code:
    BL*=NumGrid[Row - a][Column + a];
    isn't that the same as?

    Java Code:
    BL*=NumGrid[Row - a][y];

  15. #15
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Thanks King! Yes Column + a is the same as y, so I don't need y, and those if statements are definitely cleaner.

  16. #16
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: How to approach this problem?

    Quote Originally Posted by Zigster View Post
    Thanks King! Yes Column + a is the same as y, so I don't need y, and those if statements are definitely cleaner.

    Actually, my code had a vital error in it I just noticed, :P. Should have been...

    Java Code:
    if (Column<17)
    {
      //R
      //B
     
      if (Row<17)
      {
        //BR
      }
    
      if (Row>3){
        //BL
      }
    }

  17. #17
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to approach this problem?

    Yeah, lol. Was just gonna say, I tried it but it gave the wrong answer.

    Cheers!

Similar Threads

  1. Confused About How To Approach Java
    By AmpedNavy in forum Java Gaming
    Replies: 5
    Last Post: 07-03-2012, 11:18 PM
  2. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 09:26 AM
  3. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 05:53 PM
  4. Checking ResultSet (second approach)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:39 PM
  5. Best Jsp book with practical approach
    By Saurabh321 in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •