如何解决盒子爪移动器,以便将所有盒子均匀地分布在所有可用的堆栈上?

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

我正在尝试解决机器人爪杠杆谜题,该谜题应该将所有盒子均匀地分布在所有可用的堆栈上。

enter image description here

问题是,当它到达最后一个盒子时,它先向左移动,然后向右移动,无限循环。:

function solve(clawPos, boxes, boxInClaw) {
  // Calculate a maximum number of boxes per stack
  let max = (boxes.reduce((a, b) => a + b) + ~~boxInClaw) / boxes.length

  // Current stack number
  const current = boxes[clawPos]

  // Helpers
  const bigger = current > max
  const lower = current < max
  const equal = current === max
  const last = clawPos === boxes.length - 1

  // Command to return for a claw
  let command = ''

  // Actions for claw
  const R = () => command = 'RIGHT'
  const L = () => command = 'LEFT'
  const U = () => command = 'PICK'
  const D = () => command = 'PLACE'

  // Automatically select where to move the claw
  const autoDirection = () => {
    const value = boxes[clawPos]
    const left = boxes.slice(0, clawPos)
    const right = boxes.slice(clawPos, -1)

    const target = max - 1

    if (boxInClaw) {
      if (left.includes(target)) L()
      else if (right.includes(target)) R()
    } else {
      R()
    }
  }

  autoDirection()

  if (boxInClaw) {
    if (lower) D()
  } else {
    if (bigger) U()
  }

  return command;
}

我尝试了许多不同的方法来使其动态化,有没有更聪明的方法来知道它应该朝哪个方向发展?

这是直接链接(请不要提交):https://www.codingame.eu/evaluate/18917274?id=427696529803c1cd24e9258b89d01a98a72126e

javascript
2个回答
4
投票

这是我的解决方案:

function createStack(length, totalBoxes) {
  const boxPerStack = Math.floor(totalBoxes / length);
  let newStack = new Array(length).fill(boxPerStack);

  const remainder = totalBoxes % length;
  if (remainder !== 0) {
    for (let i = 0; i < remainder; i++) {
      newStack[i]++;
    }
  }

  return newStack;
}

function solve(clawPos, boxes, boxInClaw) {
  // Write your code here
  const totalBoxes = boxes.reduce((prev, acc) => prev + acc);
  let targetPos;

  if (boxInClaw) {
    const targetStack = createStack(boxes.length, totalBoxes + 1);

    // Move to place
    for (let i = 0; i < boxes.length; i++) {
      if (boxes[i] < targetStack[i]) {
        targetPos = i;
        break;
      }
    }

    if (clawPos === targetPos) return 'PLACE';
    else if (clawPos < targetPos) return 'RIGHT';
    else return 'LEFT';
  } else {
    const targetStack = createStack(boxes.length, totalBoxes);

    // Move to pick
    for (let i = 0; i < boxes.length; i++) {
      if (boxes[i] > targetStack[i]) {
        targetPos = i;
        break;
      }
    }

    if (clawPos === targetPos) return 'PICK';
    else if (clawPos < targetPos) return 'RIGHT';
    else return 'LEFT';
  }

  return '';
}


0
投票

这是工作:

  function solve(clawPos, boxes, boxInClaw) {
    // Write your code here
    // To debug: console.error('Debug messages...');

    // #1 Get all available boxes
    let sumOfBoxes = 0;
    let numOfStacks = boxes.length;
    boxes.map(item => sumOfBoxes += item);
    if(boxInClaw) sumOfBoxes += 1;
    
    
    // #2 Create a target - How the boxes should eventually be/look like
    let target = new Array(numOfStacks).fill(0);
    while(sumOfBoxes != 0){
        for(var i=0; i<numOfStacks; i++){
            if(sumOfBoxes != 0){
                target[i] += 1;
                sumOfBoxes -= 1;
            }
        }
    }

    // #3 At any position check if a boxed to be placed
    if(boxInClaw && boxes[clawPos] < target[clawPos]){
        return 'PLACE';
    }
    
    // #4 At any position check if a boxed to be picked
    if(!boxInClaw && boxes[clawPos] > target[clawPos]){
        return 'PICK';
    }

    
    // #5 Check if should go right 
    for(var i=clawPos; i<numOfStacks; i++){
        if(boxInClaw){
            if(boxes[i] < target[i]){
                return 'RIGHT'
            }
        }else{
            if(boxes[i] > target[i]){
                return 'RIGHT'
            }
        }
    }
    
    // #6 Check if should go left 
    for(var i=clawPos; i<numOfStacks; i--){
        if(boxInClaw){
            if(boxes[i] < target[i]){
                return 'LEFT'
            }
        }else{
            if(boxes[i] > target[i]){
                return 'LEFT'
            }
        }
    }


    
    return '';
}

/* Ignore and do not change the code below */

// game loop
while (true) {
    const clawPos = parseInt(readline());
    const boxInClaw = readline() !== '0';
    const stacks = parseInt(readline());
    const boxes = readline().split(' ').map(j => parseInt(j)).slice(0, stacks);
    const oldWrite = process.stdout.write;
    process.stdout.write = chunk => { console.error(chunk); return true }
    const action = solve(clawPos, boxes, boxInClaw);
    process.stdout.write = oldWrite;
    console.log(action);
}
© www.soinside.com 2019 - 2024. All rights reserved.