zoukankan      html  css  js  c++  java
  • # 分治算法实例代码

    平面点集最接近点对问题

    大致是要先横坐标排序,再纵坐标排序。
    OI-wiki详解

    选择问题

    和快速排序的过程有点相似,都需要将元素分成小于某个数的一堆,和大于这个数的一堆,然后递归下去。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int a[1001];
    
    int select(int left,int right,int k){
        if( left >= right )
            return a[left];
        int x = a[left];
        int i = left;
        int j = right+1;
        while( true ){
            do{
                i++;
            }while(a[i]<x);
            do{
                j--;
            }while(a[j]>x);
            if( i>=j )
                break ;
            swap(a[i],a[j]);
        }
        if( j-left+1 == k )
            return x;
        a[left] = a[j];
        a[j] = x;
        if( j-left+1 < k )
            return select(j+1,right,k-j+left-1);//左闭右闭 区间
        else
            return select(left,j-1,k);
    }
    
    int main(){
        int n;
        while( scanf("%d",&n) != EOF ){
            int k;//第k小
            cin>>k;
            for( int i = 0; i < n; i++ )
                cin>>a[i];
            cout<<select(0,n-1,k)<<endl;
        }
        return 0;
    }
    

    残缺棋盘问题

    详解

    #include <iostream>
     
    using namespace std;
     
    int amount,Board[100][100];
     
    void Cover(int tr,int tc,int dr,int dc,int size)
    {
        int s,t;
        if(size<2)
            return ;
        amount++;
        t=amount;
        s=size/2;
        if(dr<tr+s&&dc<tc+s)//残缺在左上角
        {
            //覆盖中间位置
            Board[tr+s-1][tc+s]=t;
            Board[tr+s][tc+s-1]=t;
            Board[tr+s][tc+s]=t;
     
            Cover(tr,tc,dr,dc,s);//覆盖左上
            Cover(tr,tc+s,tr+s-1,tc+s,s);//覆盖右上
            Cover(tr+s,tc,tr+s,tc+s-1,s);//覆盖左下
            Cover(tr+s,tc+s,tr+s,tc+s,s);//覆盖右下
        }
        else if(dr<tr+s&&dc>=tc+s)//残缺在右上角
        {
            Board[tr+s-1][tc+s-1]=t;
            Board[tr+s][tc+s-1]=t;
            Board[tr+s][tc+s]=t;
     
            Cover(tr,tc,tr+s-1,tc+s-1,s);
            Cover(tr,tc+s,dr,dc,s);
            Cover(tr+s,tc,tr+s,tc+s-1,s);
            Cover(tr+s,tc+s,tr+s,tc+s,s);
        }
        else if(dr>=tr+s&&dc<tc+s)//残缺在左下
        {
            Board[tr+s-1][tc+s-1]=t;
            Board[tr+s-1][tc+s]=t;
            Board[tr+s][tc+s]=t;
     
            Cover(tr,tc,tr+s-1,tc+s-1,s);
            Cover(tr,tc+s,tr+s-1,tc+s,s);
            Cover(tr+s,tc,dr,dc,s);
            Cover(tr+s,tc+s,tr+s,tc+s,s);
        }
        else
        {
            Board[tr+s-1][tc+s-1]=t;
            Board[tr+s-1][tc+s]=t;
            Board[tr+s][tc+s-1]=t;
     
            Cover(tr,tc,tr+s-1,tc+s-1,s);
            Cover(tr,tc+s,tr+s-1,tc+s,s);
            Cover(tr+s,tc,tr+s,tc+s-1,s);
            Cover(tr+s,tc+s,dr,dc,s);
        }
    }
     
    void Print(int s)
    {
        for(int i=1;i<=s;i++)
        {
            for(int j=1;j<=s;j++)
                printf("%5d ",Board[i][j]);
            printf("
    ");
        }
    }
     
    int main()
    {
        int s=1,k,x,y;
        printf("输入2残缺棋盘的规模:2^k,k=");
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
            s*=2;
        printf("输入棋盘残缺位置(x,y):");
        scanf("%d%d",&x,&y);
        Board[x][y]=0;
        Cover(1,1,x,y,s);
        Print(s);
        return 0;
    }
    
  • 相关阅读:
    父亲的心
    英美主要报刊杂志网站整合【更新订正版】
    【更新】免费下载百度文库文档
    About Pull Strings 英语走后门议论文
    实验室安全事故读后感
    WorldWind上风羽线的显示
    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确
    DevExpress的Web控件汉化方法
    安装rails卡住很慢 出现302 Moved Temporarily
    MSCRM4 在过滤后的LOOKUP框中实现查找
  • 原文地址:https://www.cnblogs.com/sstealer/p/13297794.html
Copyright © 2011-2022 走看看