poj 3314 Plaque Pack, 模擬

題意:n個形狀,寬度均爲w,推入寬度爲w高度爲b的盒子中,如果裝不下了就放入一個新盒子裏,輸出每個盒子佔用的高度。

直接模擬就行,就是處理起來有點複雜,具體看代碼吧。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char sh[111][15][15];
char box[111][15];
int th[111];
int cnput(char sh[][15], int dp, int h, int b) {
    int i, j, k;
    i = h - 1;
    j = dp;
    while(i >= 0) {
        for(k = 0; k < b; k++) {
            if(sh[i][k] == 'X' && box[j][k] == 'X')
                return 0;
        }
        i--;
        j--;
    }
    return 1;
}
void putin(char sh[][15], int dp, int h, int w) {
    int i, j, k;
    i = h - 1;
    j = dp;
    while(i >= 0) {
        for(k = 0; k < w; k++) {
            if(sh[i][k] == 'X' || box[j][k] == 'X')
                box[j][k] = 'X';
            else box[j][k] = '.';
        }
        i--;
        j--;
    }
}
int main() {
    int n, w, b, h, i, j;
    while(~scanf("%d%d%d", &n, &w, &b) && (n || w || b)) {
        int ans = 0, t, hs = 0;
        memset(box, 0, sizeof(box));
        for(i = 0; i < n; i++) {
            scanf("%d", &th[i]);
            for(j = 0; j < th[i]; j++)
                scanf("%s", sh[i][j]);
            if(!i) {
                ans = th[i];
                memcpy(box + b - th[i], sh[i], sizeof(sh[i]));
                continue;
            }
            int cnp = 1;
            for(j = max(b - 1 - ans, th[i] - 1); j <= b; j++) {
                if(j == b || !cnput(sh[i], j, th[i], w)) {
                    if(j - 1 < th[i] - 1) break;
                    putin(sh[i], j - 1, th[i], w);
                    cnp = 0;
                    ans = max(ans, (b - j) + th[i]);
                    break;
                }
            }
            if(cnp) {
                if(hs)
                    printf(" ");
                else hs = 1;
                printf("%d", ans);
                ans = th[i];
                memset(box, 0, sizeof(box));
                memcpy(box + b - th[i], sh[i], sizeof(sh[i]));
            }
        }
        if(hs)
            printf(" ");
        printf("%d\n", ans);
    }
    return 0;
}

發佈了67 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章