毛里求斯国旗问题
我已经为荷兰国旗问题找到了解决方案。
但这一次,我想尝试一些更困难的事情:毛里求斯国旗问题 - 4 种颜色,而不是 3 种。对于有效的算法有什么建议吗?
基本上,毛里求斯国旗问题的重点是如何根据毛里求斯国旗的颜色顺序(红、蓝、黄、绿)对给定的对列表进行排序。并且数字也必须按升序排序。
方案编程示例 输入:(
(R . 3) (G . 6) (Y . 1) (B . 2) (Y . 7) (G . 3) (R . 1) (B . 8) )
输出
:( (R.1) (R.3) (B.2) (B.8) (Y.1) (Y.7) (G.3) (G.6) )
I've made a solution for the Dutch national flag problem already.
But this time, I want to try something more difficult: the Mauritus national flag problem - 4 colours, instead of 3. Any suggestions for an effective algorithm?
Basically, The Mauritius National Flag Problem focuses on how you would be able to sort the given list of pairs based on the order of colors in the Mauritius National Flag (Red, Blue, Yellow, Green). And the numbers must be sorted in ascending order too.
Scheme Programming Sample Input:
( (R . 3) (G . 6) (Y . 1) (B . 2) (Y . 7) (G . 3) (R . 1) (B . 8) )
Output:
( (R . 1) (R . 3) (B . 2) (B . 8) (Y . 1) (Y . 7) (G . 3) (G . 6) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我想出的。我使用数字而不是颜色。
同样,我们可以写五个标志。
Here is what I came up with. Instead of colors, I am using numbers.
Similarly we can write for five flags.
这就像荷兰国旗问题一样,但我们有四种颜色。本质上适用相同的策略。假设我们有(其中 ^ 代表正在扫描的点)。
我们扫描红色
所以我们需要比平时多跟踪或多一个指针。
我们需要跟踪第一个蓝色、第一个 ?、最后一个 ?、最后一个 Y
一般来说,相同的策略适用于任意数量的颜色,但需要越来越多的交换。
This is just like the Dutch national flag problem, but we have four colors. Essentially the same strategy applies. Assume we have (where ^ represents the point being scanned).
and we scan a
So we need to keep track or one more pointer than usual.
We need to keep track of the first blue, the first ?, the last ?, the last Y
In general, the same strategy works for any number of colors, but an increasing numbers of swaps are needed.
基本上,维护以下
代码:
Basically, maintain the following :
Code:
我确实有类似的代码,但插入了
I do have a similar kind of code but insted of