Hi guys, hopefully this is an easy one for you.

Given a grid of nxn squares, where each square has an id, the first(top left) square has id 0 (so a 5x5 grid will have ids 0-24) like below:

Attachment 1102

I need to generate all diagonal solutions of length Y. So if Y is 3, then some of the solutions will be:

Attachment 1103

and

Attachment 1104

and

Attachment 1105

but obviously NOT

Attachment 1106

Any ideas how these solutions can be generated?

This is what Iv got so far (dimension = 5, inARow = 3):

public ArrayList<int[]> getSolutions(int dimension, int inARow) {

ArrayList<int[]> solutions = new ArrayList<int[]>();

//create row solutions

for(int i=0; i<dimension*dimension; i = i+dimension) {

for(int j=i; j<=i+dimension - inARow; j++){

int[] row = new int[inARow];

int counter = 0;

for(int k=j; k<j+inARow; k++){

row[counter++] = k;

}

solutions.add(row);

}

}

//create column solutions

for(int i=0;i<dimension;i++){

for(int j=i; j<(dimension*dimension)-(dimension*inARow)+dimension;j=j+dimension){

int[] col = new int[inARow];

int counter = 0;

for(int k=j;k<j+(dimension*inARow);k=k+dimension){

col[counter++] = k;

}

solutions.add(col);

}

}

//create diagonals

for(int i=0; i<dimension*dimension; i++){

for(int j=i; j<i+(dimension * inARow); j = j+dimension+1){

System.out.println(j);

}

}

return solutions;

This gives me all the diagonal solutions but also gives me the bad ones like 3,9,15. Im having trouble eliminating those.

Anti-diagonals are also solutions so 2,6,10 would also be a solution but if I get normal diagonals working I can probably do the same for anti-diagonals.