Pass-Muraille POJ - 1230(贪心)

传送门

题解:从左到右扫描,如果某一行所要越过的墙数已经大于能量,那么就得拆除多余的墙,怎么拆呢,依次拆除最长的墙就行了

附上代码:


#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxn=150;

int mapp[maxn][maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(mapp,0,sizeof(mapp));
        int n,k;
        scanf("%d%d",&n,&k);
        int y1,x1,y2,x2,maxx=-1,maxy=-1;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
            maxx=max(maxx,x2);
            maxy=max(maxy,max(y1,y2));
            if(y1<y2){
                for(int j=y1;j<=y2;j++){
                    mapp[x1][j]=i;
                }
            }else{
                for(int j=y2;j<=y1;j++){
                    mapp[x1][j]=i;
                }
            }
        }
        int temp1,ans=0;
        for(int i=0;i<=maxy;i++){
            temp1=0;
            for(int j=0;j<=maxx;j++){
                if(mapp[j][i]){
                   temp1++;
                }
            }
            int temp2=temp1-k;
            if(temp2>0){
                ans+=temp2;
                while(temp2--){
                    int tempcnt=0,tempr,tempmax=-1;
                    for(int k=0;k<=maxx;k++){
                        tempcnt=0;
                        if(mapp[k][i]>0){
                            for(int l=i+1;l<=maxy;l++){
                                if(mapp[k][l]==mapp[k][i]){
                                    tempcnt++;
                                }else{
                                    break;
                                }
                            }
                        }
                        if(tempcnt>tempmax){
                            tempmax=tempcnt;
                            tempr=k;
                        }
                    }
                    for(int m=i;m<=i+tempmax;m++){
                        mapp[tempr][m]=0;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/81771444