Показать сообщение отдельно
  #16 (permalink)  
Старый 20.12.2017, 15:43
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,108

судоку проверка
вопрос как проще сформровать массив k в 12 строке, последовательность строк не имеет значения, вариант формирования смотреть пост №10 (строки 12-15)

<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <style type="text/css">
  </style>

  <script>
  var chec = function() {
  var k = [
  /* строки */
    [0, 1, 2, 3, 4, 5, 6, 7, 8],
    [9, 10, 11, 12, 13, 14, 15, 16, 17],
    [18, 19, 20, 21, 22, 23, 24, 25, 26],
    [27, 28, 29, 30, 31, 32, 33, 34, 35],
    [36, 37, 38, 39, 40, 41, 42, 43, 44],
    [45, 46, 47, 48, 49, 50, 51, 52, 53],
    [54, 55, 56, 57, 58, 59, 60, 61, 62],
    [63, 64, 65, 66, 67, 68, 69, 70, 71],
    [72, 73, 74, 75, 76, 77, 78, 79, 80],
 /* блоки 3 х 3 */
    [0, 1, 2, 9, 10, 11, 18, 19, 20],
    [3, 4, 5, 12, 13, 14, 21, 22, 23],
    [6, 7, 8, 15, 16, 17, 24, 25, 26],
    [27, 28, 29, 36, 37, 38, 45, 46, 47],
    [30, 31, 32, 39, 40, 41, 48, 49, 50],
    [33, 34, 35, 42, 43, 44, 51, 52, 53],
    [54, 55, 56, 63, 64, 65, 72, 73, 74],
    [57, 58, 59, 66, 67, 68, 75, 76, 77],
    [60, 61, 62, 69, 70, 71, 78, 79, 80],
/* столбцы */
    [0, 9, 18, 27, 36, 45, 54, 63, 72],
    [1, 10, 19, 28, 37, 46, 55, 64, 73],
    [2, 11, 20, 29, 38, 47, 56, 65, 74],
    [3, 12, 21, 30, 39, 48, 57, 66, 75],
    [4, 13, 22, 31, 40, 49, 58, 67, 76],
    [5, 14, 23, 32, 41, 50, 59, 68, 77],
    [6, 15, 24, 33, 42, 51, 60, 69, 78],
    [7, 16, 25, 34, 43, 52, 61, 70, 79],
    [8, 17, 26, 35, 44, 53, 62, 71, 80]
];
  return function(a) {
    a = [].concat.apply([], a);
    return k.every(function(b) {
      var c = {};
      return b.every(function(b) {
        b = a[b];
        c[b] = (c[b] || 0) + 1;
        return 2 > c[b];
      });
    });
  };
}();

var board =  [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
  [ 2, 3, 4, 5, 6, 7, 8, 9, 1 ],
  [ 3, 4, 5, 6, 7, 8, 9, 1, 2 ],
  [ 4, 5, 6, 7, 8, 9, 1, 2, 3 ],
  [ 5, 6, 7, 8, 9, 1, 2, 3, 4 ],
  [ 6, 7, 8, 9, 1, 2, 3, 4, 5 ],
  [ 7, 8, 9, 1, 2, 3, 4, 5, 6 ],
  [ 8, 9, 1, 2, 3, 4, 5, 6, 7 ],
  [ 9, 1, 2, 3, 4, 5, 6, 7, 8 ] ];

alert(chec(board)); //false
board =  [
    [8, 4, 5, 6, 1, 9, 3, 2, 7],
    [1, 3, 2, 4, 5, 7, 6, 9, 8],
    [7, 6, 9, 8, 3, 2, 5, 4, 1],
    [5, 9, 6, 2, 8, 1, 7, 3, 4],
    [4, 8, 3, 5, 7, 6, 2, 1, 9],
    [2, 7, 1, 3, 9, 4, 8, 5, 6],
    [3, 5, 7, 9, 4, 8, 1, 6, 2],
    [6, 1, 4, 7, 2, 5, 9, 8, 3],
    [9, 2, 8, 1, 6, 3, 4, 7, 5]
]
alert(chec(board));//true
  </script>
</head>

<body>
<div id="slider"></div>

</body>
</html>
Ответить с цитированием