TicTacToe - 生成解决方案

时间:2012-03-13 17:24:52

标签: java math tic-tac-toe

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

1 个答案:

答案 0 :(得分: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(","));
}