문제

NXN 사각형의 그리드가 주어지는 각각의 정사각형은 ID가있는 첫 번째 (왼쪽 상단) 사각형을 사용하므로 아래와 같이 5x5 그리드는 0-24 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)
.

이러한 솔루션을 어떻게 생성 할 수 있는지 아이디어는 무엇입니까?

이것은 지금까지 얻은 것입니다 (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;
.

이것은 모든 대각선 솔루션을 제공하지만 나에게 3,9,15와 같은 나쁜 것들을 준다.나는 그것들을 제거하는 데 어려움을 겪고있다.

Anti-Diagonals도 해결책이므로 2,6,10도 해결책이지만 정상적인 대각선을 작동 시키면 Anti-Diagonals에 대해 동일하게 할 수 있습니다.

도움이 되었습니까?

해결책

그것은 당신의 행운의 날입니다 ... 나는 모든 그리드 크기와 길이에 대한 일반적인 솔루션으로 일하기 위해 답을 업데이트했습니다

멋진 의사 코드를 만드는 커피 스크립트

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
.

자바 스크립트를 컴파일합니다 (그래서 테스트 할 수 있음)

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