如何无缝组合一组“块”

问题描述 投票:0回答:0

我需要编写一个将数组“块”作为参数的函数。 “块”呈现为包含块标识符和形式的几何表示的对象,其中 0 - 间隙,1 - 内容。 所有块在一个集合中具有相同的宽度,任何集合都包含可以无间隙放置的块。

函数应该返回没有间隙的块集。可以旋转块并更改它们在集合中的顺序。 我希望下面的一些例子会有所帮助。

更新: 块从上到下流动,即结果位置 1 的块低于块位置 2

例一

const blocks = [{
    "id": 738,
    "form": [
      [1, 0],
      [1, 1]
    ]
  },
  {
    "id": 841,
    "form": [
      [1, 1],
      [0, 1]
    ]
}];

const result = [
  {
    "blockId": 738,
    "position": 1,
    "isRotated": false
  },
  {
    "blockId": 841,
    "position": 2,
    "isRotated": false
  }
];

例二

const blocks = [{
    "id": 443,
    "form": [
      [1, 0, 1],
      [1, 1, 1]
    ]
  },
  {
    "id": 327,
    "form": [
      [0, 1, 0],
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 0],
      [0, 1, 0]
    ]
  },
  {
    "id": 891,
    "form": [
      [0, 0, 1],
      [1, 0, 1],
      [1, 1, 1]
    ]
}];

const result = [
  {
    "blockId": 443,
    "position": 1,
    "isRotated": false
  },
  {
    "blockId": 327,
    "position": 2,
    "isRotated": true
  },
  {
    "blockId": 891,
    "position": 3,
    "isRotated": true
  }
];

示例 3

    const blocks = [{
        "id": 4892,
        "form": [
          [0, 0, 1],
          [1, 0, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1]
        ]
      },
      {
        "id": 1839,
        "form": [
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 0, 0]
        ]
      },
      {
        "id": 8183,
        "form": [
          [0, 1, 1],
          [1, 1, 1],
          [1, 1, 1],
          [1, 1, 0],
          [0, 1, 0]
        ]
    }];

const result = [
  {
    "blockId": 4892,
    "position": 1,
    "isRotated": false
  },
  {
    "blockId": 8183,
    "position": 2,
    "isRotated": false
  },
  {
    "blockId": 1839,
    "position": 3,
    "isRotated": false
  }
];

示例 4

const blocks = [{
    "id": 1,
    "form": [
      [1, 0, 1],
      [1, 1, 1],
      [1, 1, 1]
    ]
  },
  {
    "id": 2,
    "form": [
      [0, 0, 1],
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ]
  },
  {
    "id": 3,
    "form": [
      [0, 1, 1],
      [1, 1, 1],
      [0, 1, 0]
    ]
}];

const result = [
  {
    "blockId": 1,
    "position": 1,
    "isRotated": false
  },
  {
    "blockId": 3,
    "position": 2,
    "isRotated": false
  },
  {
    "blockId": 2,
    "position": 3,
    "isRotated": true
  }
];

我试过把表单数组当成矩阵来处理,但是没找到解决方法

我的代码:

const first ={
    "id": 443,
    "form": [
      [1, 0, 1],
      [1, 1, 1]
    ]
  }

  const second ={
    "id": 113,
    "form": [
      [1, 1, 1],
      [1, 1, 0]
    ]
  }

function rotateBlock(block){
    rotatedBlock = {...block, "form": block["form"].reverse()};
    return block;
}

console.log(rotateBlock(first))
console.log(rotateBlock(second))
//output:
//{ id: 443, form: [ [ 1, 1, 1 ], [ 1, 0, 1 ] ] }
//{ id: 113, form: [ [ 1, 1, 0 ], [ 1, 1, 1 ] ] }


const first ={
    "id": 443,
    "form": [
      [1, 0, 1],
      [1, 1, 1]
    ]
  }

  const second ={
    "id": 113,
    "form": [
      [1, 1, 1],
      [1, 1, 0]
    ]
  }

    function compareBlocks(block1, block2){
    return JSON.stringify(block1) === JSON.stringify(block2);
    }
    
    console.log(compareBlocks(first, second));
    //output:
    //false
javascript combinations
© www.soinside.com 2019 - 2024. All rights reserved.