【问题标题】:Building Sudoku board using underscore templates使用下划线模板构建数独板
【发布时间】:2014-07-07 19:51:47
【问题描述】:

我正在使用下划线模板构建数独板。我在计算推导表结构的数学时遇到了一些麻烦。

我的底层结构是一个一维数组(大小为 81)。我相信解决这个问题的最好方法是用 3x3 的小立方体来构建它。

[1,2,3,4....] -> [1,2,3 [1,2,3 .... 4,5,6 4,5,6 7,8,9] 7,8,9]

但我不知道如何获取第一个立方体的值,然后是下一个,然后是下一个。

应该是这样的

<% 
    //Defining each cube
    for (var x = 0; x < 9; x++) {
        %>
        <div class="parentCube">
        <%
        //Defining each cell in cube
        for (var y = 0; y < 9; y++) {
        console.log('x: ' + x + ' y: ' + y);
            %>
                <div class="childCube">
                    <%= 
                        //Heres where im having trouble, how do you look up the 1d array index using the outlying for loop
                        matrix.indexOf(??); 
                    %>
                </div>
            <%
        }
        %>
        </div>
        <%
    }
%>

//should follow this equation

 0,0    0,1    0,2    0,3    0,4    0,5    0,6    0,7    0,8
[0,0], [1,0], [2,0], [0,1], [1,1], [2,1], [0,2], [1,2], [2,2]

 1,0    1,1    1,2    1,3    1,4    1,5    1,6    1,7    1,8
[3,0], [4,0], [5,0], [3,1], [4,1], [5,1], [3,2], [4,2], [5,2]  

 2,0    2,1    2,2    2,3    2,4    2,5    2,6    2,7    2,8
[6,0], [7,0], [8,0], [6,1], [7,1], [8,1], [6,2], [7,2], [8,2] 

 3,0    3,1    3,2    3,3    3,4    3,5    3,6    3,7    3,8
[0,3], [1,3], [2,3], [0,4], [1,4], [2,4], [0,5], [1,5], [2,5] 

// Or this structure of X/Y's 

X

0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8, 

0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8, 

0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8, 

    Y

0,0,0,1,1,1,2,2,2, 0,0,0,1,1,1,2,2,2, 0,0,0,1,1,1,2,2,2,

3,3,3,4,4,4,5,5,5, 3,3,3,4,4,4,5,5,5, 3,3,3,4,4,4,5,5,5,

6,6,6,7,7,7,8,8,8, 6,6,6,7,7,7,8,8,8, 6,6,6,7,7,7,8,8,8,

我已经解出了 x 方程,(y%3 + (x*3)) % 9;我现在正在研究 y,因为它的包装方式有点困难。

【问题讨论】:

    标签: javascript templates underscore.js


    【解决方案1】:

    我不知道这是否会对您有所帮助,但如果您将值存储在一个平面数组中,那么您可能希望将索引集合保留在该数组中,用于行、列和小方块.这应该给你:

    var _09 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    var _03 = [0, 1, 2];
    
    rows = _09.map(function(i) {return _09.map(function(j) {return 9 * i + j;});});
    cols = _09.map(function(i) {return _09.map(function(j) {return i + 9 * j;});});
    squares = _03.map(function(i) {return _03.map(function(j) {return 27 * i + 3 * j;});}).reduce(
        function(a, b) {return a.concat(b);}
    ).map(function(corner) {return _03.map(function(i) {return _03.map(
        function(j) {return corner + 9 * i + j;});}).reduce(
            function(a, b) {return a.concat(b);}
        );}
    );
    

    【讨论】:

      【解决方案2】:

      我终于回答了这个问题。

      <% 
          //Defining each cube
          for (var x = 0; x < 9; x++) {
              %>
              <div class="parentCube">
              <%
              //Defining each cell in cube
              for (var y = 0; y < 9; y++) {
                  var row = (y%3 + (x*3)) % 9;
                  var column = (Math.floor(y/3)) + (Math.floor(x/3)*3);
                  console.log('[' + x + ', ' + y + ']', ' -> ' , '['  + column + ']');
                  %>
                      <div class="childCube">
                          <%= 
                              matrix.indexOf(row, column)
                          %>
                      </div>
                  <%
              }
              %>
              </div>
              <%
          }
      %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 2013-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多