UVA-11134(greedy)

Fabled Rooks
UVA-11134(greedy)_第1张图片
Sample Input
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
0
Sample Output
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
思路:
1.行号列号二维问题相互独立,可以转化为两个一维问题;
2.采用贪心策略,每次选右区间最小求左区间要<=col的;
3.答案不唯一,不要被测试用例吓到;
ac code:

#include
#include
using namespace std;
const int maxn=5002;
int x1[maxn],y1[maxn],x2[maxn],y2[maxn];
int ans_x[maxn],ans_y[maxn];
int n;

bool f(int *a,int *b,int *c){
     

    for(int col=1;col<=n;col++){
     
        int index=-1;
        int minb=n+1;

        for(int i=1;i<=n;i++){
     
            if(c[i]<0&&b[i]<minb&&a[i]<=col){
     
                minb=b[i];
                index=i;
            }
        }
        if(col>minb||index<0)return false;
        c[index]=col;
    }
    return true;
}

int main(){
     

    while(scanf("%d",&n)!=EOF&&n){
     
		memset(ans_x,-1,sizeof(ans_x));
		memset(ans_y,-1,sizeof(ans_y));
        for(int i=1;i<=n;i++){
     
            scanf("%d %d %d %d",&x1[i],&y1[i],&x2[i],&y2[i]);
        }
        if(f(x1,x2,ans_x)&&f(y1,y2,ans_y)){
     
            for(int i=1;i<=n;i++){
     
                printf("%d %d\n",ans_x[i],ans_y[i]);
            }
        }else{
     
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}

你可能感兴趣的:(UVA)