uva 331 Mapping the Swaps__搜尋

來源:互聯網
上載者:User

題目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267

題目描述:

Mapping the Swaps 

Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, in the sequence they are to be swapped, we obtain what might be called a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in that order. If the subscripts for this array are 1, 2, and 3, sorting the array can be accomplished by swapping A2 and A3, then swapping A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to be swapped, then this sorting process would be characterized with the swap map 2 1 2.

It is instructive to note that there may be many ways in which swapping of adjacent array entries can be used to sort an array. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally swapping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.

For a given array, how many different swap maps exist? A little thought will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change the order of the elements. Thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. But how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.

Input

The input data will contain an arbitrary number of test cases, followed by a single 0. Each test case will have a integer n that gives the size of an array, and will be followed by the n integer values in the array.

Output

For each test case, print a message similar to those shown in the sample output below. In no test case willn be larger than 5.

Sample Input

2 9 72 12 503 3 2 13 9 1 50

Sample Output

There are 1 swap maps for input data set 1.There are 0 swap maps for input data set 2.There are 2 swap maps for input data set 3.There are 1 swap maps for input data set 4.

題意:排序方法多少種

題解:暴力搜尋

代碼:

/*uva Mapping the Swaps 331*/#include<stdio.h>#include<stdlib.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>#define DM 9999999using namespace std;int n=0;int A[5+5]={0};int As[5+5]={0};int Cases=0;int Sizes=0;int MinStep=DM;/*dfs the sort strategy,the step's value is the count of the swap*/int DFS(int cur,int step){    if(memcmp(As,A,n*sizeof(int))==0)    {        if(step>0)        {            if(step<MinStep)            {                Sizes=1;                MinStep=step;            }            else if(step==MinStep)            {                Sizes++;            }        }    }    else    {        int i=0;        for(i=0;i<=n-1-1;i++)        {            if(A[i]>A[i+1])            {                //swap                int temp=A[i];                A[i]=A[i+1];                A[i+1]=temp;                DFS(cur+1,step+1);                //note the instrucion's sorting                A[i+1]=A[i];                A[i]=temp;            }        }    }    return(0);}/*for test*/int test(){    while(scanf("%d",&n)!=EOF&&n>0)    {        Cases++;        Sizes=0;        MinStep=DM;        int i=0;        for(i=0;i<=n-1;i++)        {            scanf("%d",&A[i]);            As[i]=A[i];        }        sort(As,As+n);        int B[5+5]={1,3,2};        printf("%d %d\n",memcmp(As,A,n*sizeof(int)),memcmp(As,B,n*sizeof(int)));        //it's not memcmp(As,A,n);//        char *s1="abc";//        char *s2="acd";//        int r=memcmp(s1,s2,3);    }    return(0);}/*main process*/int MainProc(){    while(scanf("%d",&n)!=EOF&&n>0)    {        Cases++;        Sizes=0;        MinStep=DM;        int i=0;        for(i=0;i<=n-1;i++)        {            scanf("%d",&A[i]);            As[i]=A[i];        }        sort(As,As+n);        DFS(0,0);        printf("There are %d swap maps for input data set %d.\n",Sizes,Cases);    }    return(0);}int main(){    MainProc();    //test();    return(0);}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.