将自行车分配给人们-第一优先(最接近的自行车最接近的人) [英] Allocate bikes to people - First Priority (Closest bike to closest person)

查看:78
本文介绍了将自行车分配给人们-第一优先(最接近的自行车最接近的人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将网格传递给具有自行车和人员的功能

  ['c','_',' A','_','_','_'] 
['_','_','a','_','_','_']
[' _','_','_','_','b','_']
['_','_','_','_','_','_ ']
['D','d','_','_','_','B']
['_','_','_','C ','_','_']

输出:类似此 [A:1,B:3,C:8,D:1]


这里A是人,而1是踏上自行车所需的步骤。


条件:


  1. 最接近自行车的人,将自行车放在第一位。

  2. 不能将单个自行车分配给2个人

  3. 一辆自行车到一个人的距离永远不会等于同一辆自行车到另一个人的距离。

  4. 距离可以相等,但是两个不同的自行车和两个不同的人

我觉得图形表示可能更有意义,因此








我的方法:


  1. 找到Bikes and Person的位置并将它们存储在数组中。



    person = [[0,2],[4,0],[4,5],[5,3]],自行车= [[0,0],[1,2],[2,4] ,[4,1]];


  2. As最短路径为1,开始从最短路径为1的数组
    中删除自行车和人,然后将最短路径递增1。
    并将人和自行车存储到结果数组中。 / p>


  3. 需要继续执行步骤2,直到人的数组为空


 函数findBikesForPeople(grid){let row_length = grid.length;让col_length = grid [0] .length; var自行车= [],人= []; for(var row = 0; row< row_length; row ++)){for(var col = 0; col< col_length; col ++){if(grid [row] [col] ==='B'){bikes.push ([row,col]); }如果(grid [row] [col] ==='P'){person.push([row,col]); }距离=(自行车,人)=> {var dist = []; person.map((single)=> {var inner = []; bikes.map((bike)=> {inner.push(check_distance(single,bike));})dist.push(inner);} )return dist; } //这不正确var AllocateBikes =(距离)=> {// var result = []; // var min = 1; // var增量= 0; //让people = distances.length; //让bikeCount = distances [0] .length; // while(people> 0){// if(Math.min(... distances []))//}返回距离; } function check_distance(a,b){return Math.abs(b [1]-a [1])+ Math.abs(b [0]-a [0]);令let distance_between =距离(自行车,人); console.log(AllocateBikes(distance_between));} var grid = [['P','_','B','_','_'],['_','_','_', '_','B'],['_','_','_','_','_'],['_','P','_','_','_ '],['_','_','_','_','B']]; findBikesForPeople(grid);  

解决方案

如果我正确理解,您就快到了。实际上,您需要做的就是找到人和自行车的所有组合,并测量它们的距离。然后,您可以根据距离对它们进行排序,然后可以遍历它们,并在遇到一个人还没有自行车且自行车仍然免费的组合时,将自行车分配给人们。这将为每个人分配不同的自行车,并首先使用最短的距离。在javascript中,可能类似于:



  function findBikesForPeople(grid){var行= grid.length; cols = grid [0] .length; var自行车= [],人= []; for(var row = 0; row< rows; row ++){{for(var col = 0; col< cols; col ++){{if(grid [row] [col] ==='B'){bikes.push ({y:row,x:col}); }如果(grid [row] [col] ===‘P’){people.push({y:row,x:col}); }}} var combis = [];对于(人中的var p){对于(自行车中的var b){var d =距离(人[p],自行车[b]); combis.push({person:p,bike:b,distance:d}); }} combis.sort(function(a,b){返回a.distance-b.distance}); var hasBike = [],isTaken = [],赋值= []; for(var c in combis){var person = combis [c] .person,bike = combis [c] .bike; if(!hasBike [person]&&!isTaken [bike]){assignment.push({person:person,px:people [person] .x,py:people [person] .y,bike:bike,bx :bikes [bike] .x,作者:bikes [bike] .y}); hasBike [person] = true; isTaken [bike] = true;返回赋值;函数distance(a,b){return Math.abs(b.x-a.x)+ Math.abs(b.y-a.y); }} var grid = [['B','_','P','_','_','_'],['_','_','B','_',' _','_'],['_','_','_','_','B','_'],['_','_','_','_' ,'_','_'],['P','B','_','_','_','P'],['_','_','_',' P','_','_']]]; document.write(JSON.stringify(findBikesForPeople(grid)));  



注意:我正在解释代码中显示的网格,其中x =水平,y =垂直,即grid [y] [x],其中(0 ,0)是左上角。


Passing in a grid to a function with bikes and person at locations

[ 'c' , '_' ,'A' ,'_', '_' , '_']
[ '_' , '_' ,'a' ,'_', '_' , '_']
[ '_' , '_' ,'_' ,'_', 'b' , '_']
[ '_' , '_' ,'_' ,'_', '_' , '_']
[ 'D' , 'd' ,'_' ,'_', '_' , 'B']
[ '_' , '_' ,'_' ,'C', '_' , '_']

Output: Something like this [A:1, B:3, C:8, D:1]

Where A is the person and 1 is the step required to travel to get to the bike.

Criterias:

  1. Closest person to the bike, get the bike at the first priority.
  2. Single bike can't be assigned to 2 individuals
  3. Distance of a bike from one individual will never be equal to distance of the same bike from a different individual.
  4. Distances can be equal, but 2 different bikes and 2 different individuals

I feel like Graphical representation might make more sense hence


My Approach:

  1. Find the location of Bikes and Person and store them in an Array.

    person = [[0,2],[4,0],[4,5],[5,3]], bikes = [[0,0],[1,2],[2,4],[4,1]];

  2. As shortest path will be 1, start removing the bikes and person from the Array who has the shortest path as 1 and keep incrementing the shortest path by 1. And store the person and bike into results array.

  3. Need to keep doing step # 2 till our Person's Array is empty

function findBikesForPeople(grid) {

  let row_length = grid.length;
  let col_length = grid[0].length;
  var bikes = [],
    person = [];

  for (var row = 0; row < row_length; row++) {
    for (var col = 0; col < col_length; col++) {
      if (grid[row][col] === 'B') {
        bikes.push([row, col]);
      }
      if (grid[row][col] === 'P') {
        person.push([row, col]);
      }
    }
  }

  var distances = (bikes, person) => {
    var dist = [];
    person.map((single) => {
      var inner = [];
      bikes.map((bike) => {
        inner.push(check_distance(single, bike));
      })
      dist.push(inner);
    })
    return dist;
  }


  //This isn't right
  var AllocateBikes = (distances) => {
    //var result = [];
    //var min = 1;
    //var increment = 0;
    //  let people = distances.length;
    //let bikeCount = distances[0].length;
    //while (people > 0) {
    //  if (Math.min(...distances[]))
    // }
    return distances;
  }

  function check_distance(a, b) {
    return Math.abs(b[1] - a[1]) + Math.abs(b[0] - a[0]);
  }

  let distance_between = distances(bikes, person);
  console.log(AllocateBikes(distance_between));

}
var grid = [
  ['P', '_', 'B', '_', '_'],
  ['_', '_', '_', '_', 'B'],
  ['_', '_', '_', '_', '_'],
  ['_', 'P', '_', '_', '_'],
  ['_', '_', '_', '_', 'B']
];

findBikesForPeople(grid);

解决方案

If I understand correctly, you're almost there. What you need to do is indeed find all the combinations of people and bikes, and measure their distance. Then, you sort these based on distance, and then you can iterate over them and assign the bikes to the people whenever you come across a combination where the person doesn't have a bike yet and the bike is still free. This will assign a different bike to each person, and use the shortest distances first. In javascript that could look something like:

function findBikesForPeople(grid) {
    var rows = grid.length, cols = grid[0].length;
    var bikes = [], people = [];
    for (var row = 0; row < rows; row++) {
        for (var col = 0; col < cols; col++) {
            if (grid[row][col] === 'B') {
                bikes.push({y: row, x:col});
            }
            if (grid[row][col] === 'P') {
                people.push({y:row, x:col});
            }
        }
    }
    var combis = [];
    for (var p in people) {
        for (var b in bikes) {
            var d = distance(people[p], bikes[b]);
            combis.push({person:p, bike:b, distance:d});
        }
    }
    combis.sort(function(a,b) {return a.distance - b.distance});
    var hasBike = [], isTaken = [], assignment = [];
    for (var c in combis) {
        var person = combis[c].person, bike = combis[c].bike;
        if (!hasBike[person] && !isTaken[bike]) {
            assignment.push({person:person, 
                             px:people[person].x, py:people[person].y,
                             bike:bike,
                             bx:bikes[bike].x, by:bikes[bike].y});
            hasBike[person] = true;
            isTaken[bike] = true;
        }
    }
    return assignment;

    function distance(a, b) {
        return Math.abs(b.x - a.x) + Math.abs(b.y - a.y);
    }
}

var grid = [['B', '_', 'P', '_', '_', '_'],
            ['_', '_', 'B', '_', '_', '_'],
            ['_', '_', '_', '_', 'B', '_'],
            ['_', '_', '_', '_', '_', '_'],
            ['P', 'B', '_', '_', '_', 'P'],
            ['_', '_', '_', 'P', '_', '_']];
document.write(JSON.stringify(findBikesForPeople(grid)));

Note: I'm interpreting the grid as displayed in the code, with x = horizontal and y = vertical, i.e. grid[y][x], with (0,0) being the top left corner.

这篇关于将自行车分配给人们-第一优先(最接近的自行车最接近的人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆