给出了NXN方块的网格,其中每个正方形具有ID,第一个(左上角)正方形具有ID 0(所以5×5个网格将具有下面的ID 0-24):

00 01 02 03 04

05 06 07 08 09

10 11 12 13 14

15 16 17 18 19

20 21 22 23 24
.

我需要生成所有长度Y的对角线解决方案。所以如果Y是3,那么一些解决方案将是:

0,6,12
.

11,17,23
.

但显然不是

3,9,15 (because the 15 does not follow on diagonally)
.

任何想法如何生成这些解决方案?

这是我到目前为止的(尺寸= 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;
.

这给了我所有对角线解决方案,但也给了我一个错误的解决方案,就像3,9,15一样。我无法消除那些。

反对对角线也是解决方案,所以2,6,10也是一个解决方案,但如果我得到正常的对角线,我可以为反对对角线做同样的事情。

有帮助吗?

解决方案

是你的幸运日......我更新了作为所有网格尺寸和长度的一般解决方案的答案

在咖啡脚本中,这使得伪伪代码

w = 5 # width of the grid
h = 5 # height of the grid
l = 3 # length to capture

m = [] # matches

for i in [0..(w*h)-1] # loop through each square
    col = i % w # calculate what column we are in
    row = Math.floor i / w # calculate what row we are in
    nums = [] # re-set matched lines array
    if col < w - (l-1) and row < h - (l-1) # if there is space for diagonal right
        for j in [0..l-1] # loop until length is reached
            nums.push i+(j*w)+j # push it onto the array of squares
        m.push nums # push into the array of matched lines
    nums = []
    if col > l-2 and row < h-l+1 # if there is space for diagonal left
        for j in [0..l-1]
            nums.push i+(j*w)-j
        m.push nums

console.dir m
# or
console.log i.join "," for i in m
.

将其编译为JavaScript(因此您可以测试它)

var col, h, i, j, l, m, nums, row, w, _i, _len, _ref, _ref2, _ref3;
w = 5;
h = 5;
l = 3;
m = [];
for (i = 0, _ref = (w * h) - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
  col = i % w;
  row = Math.floor(i / w);
  nums = [];
  if (col < w - (l - 1) && row < h - (l - 1)) {
    for (j = 0, _ref2 = l - 1; 0 <= _ref2 ? j <= _ref2 : j >= _ref2; 0 <= _ref2 ? j++ : j--) {
      nums.push(i + (j * w) + j);
    }
    m.push(nums);
  }
  nums = [];
  if (col > l - 2 && row < h - l + 1) {
    for (j = 0, _ref3 = l - 1; 0 <= _ref3 ? j <= _ref3 : j >= _ref3; 0 <= _ref3 ? j++ : j--) {
      nums.push(i + (j * w) - j);
    }
    m.push(nums);
  }
}
console.dir(m);
for (_i = 0, _len = m.length; _i < _len; _i++) {
  i = m[_i];
  console.log(i.join(","));
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top