通过网格将功能传递给自行车和人在某个位置
[ 'c' , '_' ,'A' ,'_', '_' , '_']
[ '_' , '_' ,'a' ,'_', '_' , '_']
[ '_' , '_' ,'_' ,'_', 'b' , '_']
[ '_' , '_' ,'_' ,'_', '_' , '_']
[ 'D' , 'd' ,'_' ,'_', '_' , 'B']
[ '_' , '_' ,'_' ,'C', '_' , '_']
输出:像这样的
[A:1, B:3, C:8, D:1]
准则:
我觉得图形表示可能更有意义
我的方法:
person = [[0,2],[4,0],[4,5],[5,3]], bikes = [[0,0],[1,2],[2,4],[4,1]];
谁将最短路径设为1,并将最短路径递增1。
并将人和自行车存储到结果数组中。
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);
最佳答案
如果我理解正确,那么您就快到了。实际上,您需要做的就是找到人和自行车的所有组合,并测量它们的距离。然后,您可以根据距离对它们进行排序,然后可以遍历它们,并在遇到一个人还没有自行车且自行车仍然免费的组合时,将自行车分配给人们。这将为每个人分配不同的自行车,并首先使用最短的距离。在javascript中,可能类似于:
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)));
注意:我正在解释代码中显示的网格,其中x =水平,y =垂直,即grid [y] [x],其中(0,0)是左上 Angular 。