为什么更改副本时原始数组会更改?

时间:2020-06-15 13:47:48

标签: javascript arrays

我正在尝试使用回溯来创建数独求解器,但遇到了问题。每当我更改网格数组的副本时,原始数组也会更改。

有人可以帮我吗?

算法:

let grid = [
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
];

// make a copy of the grid
let newGrid = [...grid];

// a vqriable that becomes true if we find a solution to the puzzle
let found = false;

// this function checks if the current choice for grid[row][col] is valid
const valid = (row, col) => {
   // check the row for the same number
   for (let j = 0; j < 9; j++)
      if (newGrid[row][col] === newGrid[row][j] && j !== col) return false;

   // check the column
   for (let i = 0; i < 9; i++)
      if (newGrid[row][col] === newGrid[i][col] && i !== row) return false;

   // check the smaller "box"

   // the number of the box that the current element is in
   const verticalNumber = Math.floor(row / 3);
   const horizontalNumber = Math.floor(col / 3);

   // iterate through the whole grid
   for (let i = 0; i < 9; i++)
      for (let j = 0; j < 9; j++) {
         const vertical = Math.floor(i / 3);
         const horizontal = Math.floor(j / 3);

         // if the elements are not in the same box or the element is the current element, skip it
         if (
            vertical !== verticalNumber ||
            horizontal !== horizontalNumber ||
            (i === row && j === col)
         )
            continue;

         if (newGrid[i][j] === newGrid[row][col]) return false;
      }

   // otherwise it's okay
   return true;
};

// this function checks if the algorithm is finished
const stop = (row, col) => {
   return row > 8;
};

const backtracking = (row, col) => {
   if (found) return;
   // if the algorithm finished, print the completed puzzle
   if (stop(row, col)) {
      console.log(newGrid);
      found = true;
      return;
   }

   // if the current cell already has a number, skip it
   if (grid[row][col]) {
      if (col === 8) backtracking(row + 1, 0);
      else backtracking(row, col + 1);

      // otherwise check every single posibility, and if it is valid go to the next cell
   } else {
      for (let i = 1; i <= 9 && !found; i++) {
         newGrid[row][col] = i;
         console.log(newGrid[row][col] === grid[row][col]);
         if (valid(row, col)) {
            if (col === 8) backtracking(row + 1, 0);
            else backtracking(row, col + 1);
         }
      }
   }
};


backtracking(0,0);

我在网上查询了一下,除了“使用...或切片”,找不到其他答案,如您所见。

3 个答案:

答案 0 :(得分:5)

因为

let newGrid = [...grid];

进行浅表复制,并且不复制子数组。您需要一个深层副本;对于像这样的简单数组,一种丑陋且缓慢(但简单有效)的方法是通过JSON往返:

let newGrid = JSON.parse(JSON.stringify(grid));

如果您不想这样做,例如Lodash带有cloneDeep()函数。

答案 1 :(得分:2)

问题在于newGrid中的数组仍然是原始数组,您只是在更改它们分开的数组。尝试使用.map() ...

newGrid = grid.map(r => [...r])

答案 2 :(得分:1)

对于您的特定问题,请尝试以下操作(每行的浅表副本足以满足您的用例):

let grid = [
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0],
];

// make a copy of the grid
let newGrid = grid.map(row => [...row]);

//mutate original grid:
grid[0][1] = 1;

//newGrid is not affected by above mutation
console.log("old", grid, "new", newGrid);