-3

给定一个 nxn 方格的网格,其中每个方格都有一个 id,第一个(左上)方格的 id 为 0(因此 5x5 网格的 ids 为 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 也是一个解决方案,但如果我得到正常的对角线工作,我可能对对角线也可以这样做。

4

1 回答 1

1

这是你的幸运日......我更新了答案,作为所有网格大小和长度的通用解决方案

在咖啡脚本中,这是很好的伪代码

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(","));
}
于 2012-03-13T17:46:34.107 回答