毛里求斯国旗问题

时间:2010-08-08 09:30:55

标签: algorithm scheme

我已经为Dutch national flag problem提供了解决方案。

但是这一次,我想尝试一些更难的事情:毛里求斯国旗问题 - 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))

6 个答案:

答案 0 :(得分:6)

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

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

我们扫描一个

  1. 红色,然后我们将第一个蓝色与当前节点交换
  2. BLUE我们什么都不做
  3. 黄色我们换了最后一个?
  4. 绿色我们将最后一个黄色与最后一个黄色交换?那么当前节点与交换?
  5. 所以我们需要跟踪或比通常多一个指针。

    我们需要跟踪第一个蓝色,第一个?,最后一个?,最后一个Y

    一般来说,相同的策略适用于任意数量的颜色,但需要越来越多的交换。

答案 1 :(得分:4)

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

// 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--;
        }   
    }

答案 2 :(得分:1)

基本上,请保持以下内容:

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--;
            }

        }

答案 3 :(得分:0)

我确实有类似的代码,但是安装了

reloadData()

答案 4 :(得分:0)

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--;
        }
    }
}

答案 5 :(得分:0)

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--;
        }
    }
}