【发布时间】:2016-08-28 07:50:03
【问题描述】:
我最近一直在努力编写一种“快速约会风格”算法。基本目标是让一个组(男性)的每个成员在他们的餐桌上与另一组(女性)的每个成员会面一次。
条件是:
- 餐桌的数量等于女性的数量。
- 每个男人都被分配到一个女人坐的桌子上(1v1 对话)。
- 在下一轮中,每个人都被切换到他从未去过的另一张桌子上。
- 如果组的大小不同,则任何成员(男性或女性)都不得连续两轮暂停(缺少伙伴)。
当男性组的成员多于女性组时,就会出现困难,反之亦然。
例子:
var men = [
'm1', 'm2', 'm3', 'm4', 'm5',
],
women = [
'w1', 'w2', 'w3'
];
┃ ROUND 1 ┃ ROUND 2
┌─────────────┬─────────────┐ ┌─────────────┬─────────────┐
│ men │ women │ │ men │ women │
├─────────────┴─────────────┤ ├─────────────┴─────────────┤
│ m1 >>> w1 │ │ m4 >>> w1 │
│ m2 >>> w2 │ │ m5 >>> w2 │
│ m3 >>> w3 │ │ m1 >>> w3 │
│ m4 pause │ │ m2 pause │
│ m5 pause │ │ m3 pause │
└───────────────────────────┘ └───────────────────────────┘
┃ ROUND 3
┌─────────────┬─────────────┐
│ men │ women │
├─────────────┴─────────────┤
│ m2 >>> w1 │
│ m3 >>> w2 │
│ m4 >>> w3 │
│ m5 pause │
│ m1 pause │
└───────────────────────────┘
因此,比赛是:
var results = [
'm1' = [
'w1', 'w3', null
],
'm2' = [
'w2', null, 'w1'
],
'm3' = [
'w3', null, 'w2'
],
'm4' = [
null, 'w1', 'w3'
],
'm5' = [
null, 'w2', null
],
];
到目前为止,尽管在本网站和其他网站上查看了类似的问题(参见下面的屏幕截图),但我尝试解决此问题的尝试均未成功。在这次尝试中,我跑了 15/15 轮,结果仍然有人暂停 2 轮或更多轮,等等。
* 截图中的名字是假的;由Faker生成
我当前(非工作)在 Javascript 中的尝试
我基本上有四个数组
- 一群人
- 女性群体
- 本轮可用表格的数组
- 每个男人的未满足女性的数组
var men_array = [
'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10'
];
var women_array = [
'w1', 'w2', 'w3', 'w4', 'w5'
];
var available_tables_this_round = [];
var unmet_women = [];
// Run round
function start_round(){
console.log('START OF ROUND ----------------');
// Set available tables this round
// One table per woman
available_tables_this_round = women_array;
// Selected table
var selected_table;
// Finding table partner for each man
men_array.forEach(function(item){
var current_man = item;
// Checking if this user has unmet women on record
if(typeof unmet_women[current_man] == 'undefined'){
// Unmet women array not set. Setting...
unmet_women[current_man] = women_array;
}
// Loop through available tables to see which
// of those the man can join (has not visited yet).
for(var i = 0; i < available_tables_this_round.length; i++){
var current_woman = available_tables_this_round[i];
// If woman among the available has not been met
if($.inArray(current_woman, available_tables_this_round) !== -1){
selected_table = current_woman;
// Removing table from those available this round
available_tables_this_round = $.grep(available_tables_this_round, function(value) {
return value != selected_table;
});
// Removing woman from unmet by this man
unmet_women[current_man] = $.grep(unmet_women[current_man], function(value) {
return value != selected_table;
});
console.log(current_man, '>>>', selected_table);
// Exiting loop since we've found a match for the man (for this round!)
break;
}
}
});
console.log('END OF ROUND ----------------');
}
// Button handling
$(document).on('click', 'button#start_round_btn', start_round);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="start_round_btn">Start Round</button>
【问题讨论】:
-
如果您添加一些代码以查看您当前的算法实现实际做了什么,这将很有用。
-
感谢您的反馈;我会继续这样做:)
-
添加了一些代码,可以让我了解我尝试解决这个问题的想法。非常感谢任何输入!
标签: javascript jquery algorithm