毛里求斯国旗问题

发布于 2024-09-13 08:25:23 字数 429 浏览 6 评论 0原文

我已经为荷兰国旗问题找到了解决方案。

但这一次,我想尝试一些更困难的事情:毛里求斯国旗问题 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泡沫很甜 2024-09-20 08:25:24
let a:string[] = ['1','2','1','0','2','4','3','0','1','3'];

function sort3(a:string[]):void{
    let low = 0;
    let mid1 = 0;
    let mid2 = 0;
    let mid3 = 0;
    let high = a.length - 1;
    while(mid3<=high){
        switch(a[mid3]){
            case '0': [a[mid3],a[low]] = [a[low],a[mid3]];
                    low++;
                    if(mid1 < low)
                    mid1++;
                    if(mid2 < mid1)
                    mid2++;
                    if(mid3 < mid2)
                    mid3++;
                    break;

            case '1': [a[mid3],a[mid1]] = [a[mid1],a[mid3]];
                    mid1++;
                    if(mid2 < mid1)
                    mid2++;
                    if(mid3 < mid2)
                    mid3++
                    break;

            case '2': [a[mid2],a[mid3]] = [a[mid3],a[mid2]];
                        mid2++;
                        mid3++;
                   break;

            case '3':
                    mid3++;break;

            case '4': [a[mid3],a[high]] = [a[high],a[mid3]];
                        high--;
        }
    }
}
let a:string[] = ['1','2','1','0','2','4','3','0','1','3'];

function sort3(a:string[]):void{
    let low = 0;
    let mid1 = 0;
    let mid2 = 0;
    let mid3 = 0;
    let high = a.length - 1;
    while(mid3<=high){
        switch(a[mid3]){
            case '0': [a[mid3],a[low]] = [a[low],a[mid3]];
                    low++;
                    if(mid1 < low)
                    mid1++;
                    if(mid2 < mid1)
                    mid2++;
                    if(mid3 < mid2)
                    mid3++;
                    break;

            case '1': [a[mid3],a[mid1]] = [a[mid1],a[mid3]];
                    mid1++;
                    if(mid2 < mid1)
                    mid2++;
                    if(mid3 < mid2)
                    mid3++
                    break;

            case '2': [a[mid2],a[mid3]] = [a[mid3],a[mid2]];
                        mid2++;
                        mid3++;
                   break;

            case '3':
                    mid3++;break;

            case '4': [a[mid3],a[high]] = [a[high],a[mid3]];
                        high--;
        }
    }
}
牵你手 2024-09-20 08:25:23

这是我想出的。我使用数字而不是颜色。

// l  - index at which 0 should be inserted.
// m1 - index at which 1 should be inserted.
// m2 - index at which 2 should be inserted.
// h  - index at which 3 should be inserted.
l=m1=m2=0;
h=arr.length-1
while(m2 <= h) {
    if (arr[m2] == 0) {
        swap(arr, m2, l);
        l++;

        // m1 should be incremented if it is less than l as 1 can come after all
        // 0's
        //only.
        if (m1 < l) {
            m1++;
        }
        // Now why not always incrementing m2 as we used to do in 3 flag partition
        // while comparing with 0? Let's take an example here. Suppose arr[l]=1
        // and arr[m2]=0. So we swap arr[l] with arr[m2] with and increment l.
        // Now arr[m2] is equal to 1. But if arr[m1] is equal to 2 then we should
        // swap arr[m1] with arr[m2]. That's  why arr[m2] needs to be processed
        // again for the sake of arr[m1]. In any case, it should not be less than
        // l, so incrmenting.
        if(m2<l) {
            m2++;
        }       
    }
    // From here it is exactly same as 3 flag.
    else if(arr[m2]==1) {
        swap(arr, m1, m2)
        m1++;
        m2++;           
    }
    else if(arr[m2] ==2){
        m2++;
    }
    else {
        swap(arr, m2, h);
        h--;
    }           
}


}

同样,我们可以写五个标志。

    l=m1=m2=m3=0;
    h= arr.length-1;
    while(m3 <= h) {
        if (arr[m3] == 0) {
            swap(arr, m3, l);
            l++;
            if (m1 < l) {
                m1++;
            }
            if(m2<l) {
                m2++;
            }
            if(m3<l) {
                m3++;
            }

        }
        else if(arr[m3]==1) {
            swap(arr, m1, m3);
            m1++;
            if(m2<m1) {
                m2++;
            }
            if(m3<m1) {
                m3++;
            }   

        }
        else if(arr[m3] ==2){
            swap(arr,m2,m3);
            m2++;
            m3++;
        }
        else if(arr[m3]==3) {
            m3++;
        }
        else {
            swap(arr, m3, h);
            h--;
        }   
    }

Here is what I came up with. Instead of colors, I am using numbers.

// l  - index at which 0 should be inserted.
// m1 - index at which 1 should be inserted.
// m2 - index at which 2 should be inserted.
// h  - index at which 3 should be inserted.
l=m1=m2=0;
h=arr.length-1
while(m2 <= h) {
    if (arr[m2] == 0) {
        swap(arr, m2, l);
        l++;

        // m1 should be incremented if it is less than l as 1 can come after all
        // 0's
        //only.
        if (m1 < l) {
            m1++;
        }
        // Now why not always incrementing m2 as we used to do in 3 flag partition
        // while comparing with 0? Let's take an example here. Suppose arr[l]=1
        // and arr[m2]=0. So we swap arr[l] with arr[m2] with and increment l.
        // Now arr[m2] is equal to 1. But if arr[m1] is equal to 2 then we should
        // swap arr[m1] with arr[m2]. That's  why arr[m2] needs to be processed
        // again for the sake of arr[m1]. In any case, it should not be less than
        // l, so incrmenting.
        if(m2<l) {
            m2++;
        }       
    }
    // From here it is exactly same as 3 flag.
    else if(arr[m2]==1) {
        swap(arr, m1, m2)
        m1++;
        m2++;           
    }
    else if(arr[m2] ==2){
        m2++;
    }
    else {
        swap(arr, m2, h);
        h--;
    }           
}


}

Similarly we can write for five flags.

    l=m1=m2=m3=0;
    h= arr.length-1;
    while(m3 <= h) {
        if (arr[m3] == 0) {
            swap(arr, m3, l);
            l++;
            if (m1 < l) {
                m1++;
            }
            if(m2<l) {
                m2++;
            }
            if(m3<l) {
                m3++;
            }

        }
        else if(arr[m3]==1) {
            swap(arr, m1, m3);
            m1++;
            if(m2<m1) {
                m2++;
            }
            if(m3<m1) {
                m3++;
            }   

        }
        else if(arr[m3] ==2){
            swap(arr,m2,m3);
            m2++;
            m3++;
        }
        else if(arr[m3]==3) {
            m3++;
        }
        else {
            swap(arr, m3, h);
            h--;
        }   
    }
懷念過去 2024-09-20 08:25:23

这就像荷兰国旗问题一样,但我们有四种颜色。本质上适用相同的策略。假设我们有(其中 ^ 代表正在扫描的点)。

  RRRRBBB???????????YYYYGGGG
         ^

我们扫描红色

  1. ,然后我们将第一个蓝色与当前节点
  2. 蓝色交换我们不做任何事情
  3. 黄色我们与最后一个交换?
  4. 绿色,我们将最后一个黄色与最后一个交换?那么当前节点与交换的 ?

所以我们需要比平时多跟踪或多一个指针。

我们需要跟踪第一个蓝色、第一个 ?、最后一个 ?、最后一个 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).

  RRRRBBB???????????YYYYGGGG
         ^

and we scan a

  1. red, then we swap the first blue with the current node
  2. BLUE we do nothing
  3. yellow we swap with the last ?
  4. Green we swap the last yellow with the last ? Then the current node with the swapped ?

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.

恋竹姑娘 2024-09-20 08:25:23

基本上,维护以下

a[0-p] => '0'
a[p-q] => '1'
a[q-r] => '2'
a[r-s] => traversing! 
a[s-length] => '3'          

代码:

        int p=-1,q=-1,r=0,s=a.length-1;
        while(r<=s){
            if(a[r]==0){
                exchange(a,p+1,r);
                p++;r++;
                if(q!=-1)
                    q++;
            } else if (a[r]==1){
                if(q==-1)
                    q=p;
                exchange(a,q+1,r);
                q++;r++;
            } else if(a[r]==2) {
                r++;
            } else {
                exchange(a,r,s);
                s--;
            }

        }

Basically, maintain the following :

a[0-p] => '0'
a[p-q] => '1'
a[q-r] => '2'
a[r-s] => traversing! 
a[s-length] => '3'          

Code:

        int p=-1,q=-1,r=0,s=a.length-1;
        while(r<=s){
            if(a[r]==0){
                exchange(a,p+1,r);
                p++;r++;
                if(q!=-1)
                    q++;
            } else if (a[r]==1){
                if(q==-1)
                    q=p;
                exchange(a,q+1,r);
                q++;r++;
            } else if(a[r]==2) {
                r++;
            } else {
                exchange(a,r,s);
                s--;
            }

        }
你又不是我 2024-09-20 08:25:23

我确实有类似的代码,但插入了

function sort(a:string[]){    
         let low = 0;
         let mid1 = 0;
         let mid2 = a.length-1;
         let high = a.length-1;

        while(mid1 <= mid2){
             switch(a[mid1]){

                 case '0':

                        [a[mid1],a[low]] = [a[low],a[mid1]];
                        mid1++;
                        low++;
                        break;

                 case '1':mid1++;break;

                 case '2':
                 case '3':[a[mid1],a[mid2]] = [a[mid2],a[mid1]];
                            mid2--;
                            break;

                 }
         }
    //sort 2 and 3
         while(mid1 <= high){
          switch(a[mid1]){
                 case '2': mid1++; break;
                 case '3': [a[mid1],a[high]] = [a[high],a[mid1]]
                            high--;
                            break;

           }
        }

}

I do have a similar kind of code but insted of

function sort(a:string[]){    
         let low = 0;
         let mid1 = 0;
         let mid2 = a.length-1;
         let high = a.length-1;

        while(mid1 <= mid2){
             switch(a[mid1]){

                 case '0':

                        [a[mid1],a[low]] = [a[low],a[mid1]];
                        mid1++;
                        low++;
                        break;

                 case '1':mid1++;break;

                 case '2':
                 case '3':[a[mid1],a[mid2]] = [a[mid2],a[mid1]];
                            mid2--;
                            break;

                 }
         }
    //sort 2 and 3
         while(mid1 <= high){
          switch(a[mid1]){
                 case '2': mid1++; break;
                 case '3': [a[mid1],a[high]] = [a[high],a[mid1]]
                            high--;
                            break;

           }
        }

}
萧瑟寒风 2024-09-20 08:25:23
function sort3(a:string[]):void{
    let low = 0;
    let mid1 = 0;
    let mid2 = 0;
    let high = a.length - 1;
    while(mid2<=high){
        switch(a[mid2]){
            case '0': [a[mid2],a[low]] = [a[low],a[mid2]];
                    low++;
                    if(mid1<low)
                    mid1++;
                    if(mid2<mid1)
                    mid2++;
                    break;

            case '1': [a[mid2],a[mid1]] = [a[mid1],a[mid2]];
                    mid1++;
                    mid2++;
                    break;

            case '2':mid2++
                   break;

            case '3':[a[mid2],a[high]] = [a[high],a[mid2]];
                    high--;
        }
    }
}
function sort3(a:string[]):void{
    let low = 0;
    let mid1 = 0;
    let mid2 = 0;
    let high = a.length - 1;
    while(mid2<=high){
        switch(a[mid2]){
            case '0': [a[mid2],a[low]] = [a[low],a[mid2]];
                    low++;
                    if(mid1<low)
                    mid1++;
                    if(mid2<mid1)
                    mid2++;
                    break;

            case '1': [a[mid2],a[mid1]] = [a[mid1],a[mid2]];
                    mid1++;
                    mid2++;
                    break;

            case '2':mid2++
                   break;

            case '3':[a[mid2],a[high]] = [a[high],a[mid2]];
                    high--;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文