codeforces 26C Parquet 貪心 模擬

Parquet

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Once Bob decided to lay a parquet floor in his living room. The living room is of size n × m metres. Bob had planks of three types: a planks 1 × 2 meters, b planks 2 × 1 meters, and c planks 2 × 2 meters. Help Bob find out, if it is possible to parquet the living room with such a set of planks, and if it is possible, find one of the possible ways to do so. Bob doesn’t have to use all the planks.

Input
The first input line contains 5 space-separated integer numbers n, m, a, b, c (1 ≤ n, m ≤ 100, 0 ≤ a, b, c ≤ 104 ), n and m — the living room dimensions, a, b and c — amount of planks 1 × 2, 2 × 1 и 2 × 2 respectively. It’s not allowed to turn the planks.

Output
If it is not possible to parquet the room with such a set of planks, output IMPOSSIBLE. Otherwise output one of the possible ways to parquet the room — output n lines with m lower-case Latin letters each. Two squares with common sides should contain the same letters, if they belong to one and the same plank, and different letters otherwise. Different planks can be marked with one and the same letter (see examples). If the answer is not unique, output any.

Examples
input
2 6 2 2 1
output
aabcca
aabdda
input
1 1 100 100 100
output
IMPOSSIBLE
input
4 4 10 10 10
output
aabb
aabb
bbaa
bbaa

題目鏈接

題意:你有1*2,2*1和2*2的木板各a,b,c個,然後問你能否用這些木板拼成n*m的木板。

解題思路:明顯的貪心題,剛開始卻不知道從何下手,可以知道的是我們肯定可以先把2*2的木板全部用完,因爲沒有想過可以把2*2的木板先取負,後來纔想到可以,然後用餘下的1*2和2*1的木板對其進行補足就好了。如果木板不足或是n*m無法整除2,那麼我們就無法拼成n*m的木板。
然後之後還需要對其進行編號,因爲是100*100,所以可以直接暴力找編號就好,就是比較的時候真的是煩到不行…

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a,b,c,st[105][105],flag=1;
char st1[105][105];
void cc(int i,int j){
    st[i][j]=st[i+1][j]=st[i][j+1]=st[i+1][j+1]=3;
}
void bb(int i,int j){
    st[i][j]=st[i+1][j]=2;
}
void aa(int i,int j){
    st[i][j]=st[i][j+1]=1;
}

void aaa(int x,int y){
    char s;
    for(char i='a';;i++){
        if(i!=st1[x-1][y]&&i!=st1[x+1][y]&&i!=st1[x][y-1]
        &&i!=st1[x][y+2]&&i!=st1[x-1][y+1]&&i!=st1[x+1][y+1]){
            s=i;
            break;
        }
    }
    st1[x][y]=st1[x][y+1]=s;
    st[x][y]=st[x][y+1]=0;
}

void bbb(int x,int y){
    char s;
    for(char i='a';;i++){
        if(i!=st1[x-1][y]&&i!=st1[x+2][y]&&i!=st1[x][y-1]
        &&i!=st1[x][y+1]&&i!=st1[x+1][y+1]&&i!=st1[x+1][y-1]){
            s=i;
            break;
        }
    }
    st1[x][y]=st1[x+1][y]=s;
    st[x][y]=st[x+1][y]=0;
}

void ccc(int x,int y){
    char s;
    for(char i='a';;i++){
        if(i!=st1[x-1][y]&&i!=st1[x-1][y+1]&&i!=st1[x][y-1]
        &&i!=st1[x][y+2]&&i!=st1[x+1][y-1]&&i!=st1[x+1][y+2]
        &&i!=st1[x+2][y]&&i!=st1[x+2][y+1]){
            s=i;
            break;
        }
    }
    st1[x][y]=st1[x][y+1]=st1[x+1][y]=st1[x+1][y+1]=s;
    st[x][y]=st[x][y+1]=st[x+1][y]=st[x+1][y+1]=0;
}
int main(){
    scanf("%d%d%d%d%d",&n,&m,&a,&b,&c);
    if((m*n)%2==1||m*n>2*a+2*b+4*c){
        printf("IMPOSSIBLE\n");
        return 0; 
    }
    c-=(m/2)*(n/2);
    for(int i=1;i<=n;i+=2){
        for(int j=1;j<=m;j+=2){
            cc(i,j);
        }
    }
    if(m%2==1){
        for(int i=1;i<=n;i+=2){
            bb(i,m);
            b--;
        }
    }
    if(n%2==1){
        for(int i=1;i<=m;i+=2){
            aa(n,i);
            a--;
        }
    }
    if(a<0||b<0){
        printf("IMPOSSIBLE\n");
        return 0; 
    }
    if(c<0){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(st[i][j]==3){
                    if(a>=2){
                        aa(i,j);
                        aa(i+1,j);
                        a-=2;
                        c++;
                    }
                    else if(b>=2){
                        bb(i,j);
                        bb(i,j+1);
                        b-=2;
                        c++;
                    }
                    else{
                        flag=0;
                        break;
                    }
                }   
                if(c==0)    break;
            }
            if(flag==0) break;
            if(c==0)    break;
        }
    }   
    if(flag==0){
        printf("IMPOSSIBLE\n");
        return 0; 
    }
    else{
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(st[i][j]==0) continue;
                else if(st[i][j]==1){
                    aaa(i,j);
                }
                else if(st[i][j]==2){
                    bbb(i,j);
                }
                else if(st[i][j]==3){
                    ccc(i,j);
                }
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)   printf("%c",st1[i][j]);
            printf("\n");
        }
    }
    return 0;   
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章