- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑 N = 4k
玩家、k
table 和多个氏族,这样每个成员都可以属于一个氏族。一个氏族最多可以包含 k
个玩家。
我们想要组织一场比赛的 3 轮比赛,使得每张 table 恰好有 4 名玩家入座,没有 2 名坐在那儿的玩家属于同一战队,并且在后面的几轮比赛中,没有 2 名坐在那儿的玩家已经坐下之前同 table 。所有玩家玩所有回合。
如果 N
可以大约 ~80
大,我们如何才能有效地做到这一点?
我想到了这个:
for each table T:
repeat until 4 players have been seated at T:
pick a random player X that is not currently seated anywhere
if X has not sat at the same table as anyone currently at T AND
X is not from the same clan as anyone currently at T
seat X at T
break
我不确定这是否总是会完成,或者即使有有效的分配也会卡住。即使这行得通,还有更好的方法吗?
最佳答案
如果每个部落总是恰好有 k 个玩家(即 4 个部落),您知道一张 table 上应该总是有 1 个部落成员。在那种情况下,我认为有可能提出一些预定义的轮换方案,其中每个玩家根据他来自的氏族,进一步移动固定数量的 table 。
如果氏族的数量超过 4 个,我认为这是不可能的(或者我没看到,无论如何)
我觉得你的算法很不错。您可以防止它永不终止(如果没有有效的解决方案)同时仍然保持随机行为的一种方法是不要无限地选择随机玩家,而是将未就座的玩家列表洗牌一次,然后处理此列表中的每个玩家转:
编辑:我忘了遍历轮次,也将其包含在算法中。
for each round R in {1, 2, 3}
for each table T:
UP = a randomly shuffled list of unseated players
for each player X from UP
if there are less than 4 people seated at T AND
X has not previously sat with any of the players currently at T AND
X is not from the same clan as anyone currently at T
seat X at T
//No more players left to try for this table
if T has less than 4 people seated
abort; //No solution possible (at least, with the decisions taken so far)
//All tables filled with players, prepare for the next round.
for each table T:
for each player X on T:
Register on X that he has sat with each of the other 3 players at T
Unseat all players at T
这样,对于一个回合,算法最多尝试每张 table 一次所有玩家,因此单次运行最多尝试 3*T*N 次让玩家入座,然后终止(有或没有解决方案) .换句话说,单次运行应该很快完成。
请注意,因为它仍然是随机的,所以多次运行相同的算法是完全可以接受的,因为它每次都会尝试不同的座位组合(使其成为所谓的拉斯维加斯算法)。
编辑 2:在 5 个部落(每个部落 16 名玩家)中尝试并测试了该算法。通常,它需要大约 400 次运行才能找到第一个解,但总运行时间仍然只有 1 秒左右。
关于algorithm - 分配玩家到 table ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078397/
我有一个应用程序,它会抛出 GKSession 并在各种条件下(连接超时、 session 失败等)创建一个新的 GKSession。不过,我遇到了内存泄漏问题,并且有时会在重新连接几次循环后崩溃。
比如我在宿主代码中有一个浮点指针 float *p 是否可以确定他指向的内存类型(设备/主机)? 最佳答案 在 UVA system 中, 运行时 API 函数 cudaPointerGetAttri
我已将项目转换为 .Net 4.0 并且以下代码不起作用: typeof(RuntimeTypeHandle).GetMethod("Allocate", BindingFlags.Instance
当我声明 char ch = 'ab' 时,ch 只包含 'b',为什么它不存储 'a'? #include int main() { char ch = 'ab'; printf("%c"
我对 Disk Sector 和 Block 有疑问。扇区是一个单位,通常为 512 字节或 1k、2k、4k 等取决于硬件。文件系统 block 大小是一组扇区大小。 假设我正在存储一个 5KB 的
假设我有 8 个人和5000 个苹果。 我想将所有苹果分发给所有 8 个人,这样我就没有苹果了。 但每个人都应该得到不同数量 将它们全部分发出去的最佳方式是什么? 我是这样开始的: let peopl
我正在构建的网站顶部有一个搜索栏。与 Trello 或 Gmail 类似,我希望当用户按下“/”键时,他们的焦点就会转到该搜索框。 我的 JavaScript 看起来像这样: document.onk
我有一小段代码: if (PZ_APP.dom.isAnyDomElement($textInputs)){ $textInputs.on("focus", function(){
我观察到以下行为。 接受了两个属性变量。 @property (nonatomic, retain) NSString *stringOne; @property (nonatomic, assign
我正在解决这样的问题 - 实现一个计算由以下内容组成的表达式的函数以下操作数:“(”、“)”、“+”、“-”、“*”、“/”。中的每个数字表达式可能很大(与由字符串表示的一样大)1000 位)。 “/
我有一组主机和一组任务。 每个主机都有 cpu、mem 和任务容量,每个任务都有 cpu、mem 要求。 每个主机都属于一个延迟类别,并且可以与具有特定延迟值的其他主机通信。 每个任务可能需要以等于或
该程序的作用:从文件中读取一个包含 nrRows 行和 nrColomns 列的矩阵(二维数组)。矩阵的所有元素都是 [0,100) 之间的整数。程序必须重新排列矩阵内的所有元素,使每个元素等于其所在
世界!我有个问题。今天我尝试创建一个代码,它可以找到加泰罗尼亚语号码。但是在我的程序中可以是长数字。我找到了分子和分母。但我不能分割长数字!此外,只有标准库必须在此程序中使用。请帮帮我。这是我的代码
我确定我遗漏了一些明显的东西,但我想在 Objective C 中创建一个 NSInteger 指针的实例。 -(NSInteger*) getIntegerPointer{ NSInteger
这个问题在这里已经有了答案: Difference between self.ivar and ivar? (4 个答案) 关闭 9 年前。
我如何将 v[i] 分配给一系列整数(v 的类型是 vector )而无需最初填充 最佳答案 你的意思是将 std::vector 初始化为一系列整数? int i[] = {1, 2, 3, 4,
我想寻求分配方面的帮助....我把这个作业带到了学校......我必须编写程序来加载一个 G 矩阵和第二个 G 矩阵,并搜索第二个 G 矩阵以获取存在数第一个 G 矩阵的......但是,当我尝试运行
我必须管理资源。它基本上是一个唯一的编号,用于标识交换机中的第 2 层连接。可以有 16k 个这样的连接,因此每次用户希望配置连接时,他/她都需要分配一个唯一索引。同样,当用户希望删除连接时,资源(号
是否有任何通用的命名约定来区分已分配和未分配的字符串?我正在寻找的是希望类似于 us/s 来自 Making Wrong Code Look Wrong ,但我宁愿使用常见的东西也不愿自己动手。 最佳
我需要读取一个 .txt 文件并将文件中的每个单词分配到一个结构中,该结构从结构 vector 指向。我将在下面更好地解释。 感谢您的帮助。 我的程序只分配文件的第一个字... 我知道问题出在函数 i
我是一名优秀的程序员,十分优秀!