b076中等

100 桃竹苗區資訊學科能力競賽(2011)|俄羅斯方塊問題

完整枚舉下一塊能消掉的行數。

解題思路

清單文字中的形狀有缺字,已核對原掃描 https://zerojudge.tw/ShowImage?id=522:五種為O、四格直線I、兩個鏡像折形、T。程式以四個格子的座標保存各形狀,不憑缺漏轉寫猜測。

對每種形狀枚舉四個90度旋轉,每次平移使最小x,y為0,再枚舉不超出8欄的水平位置。從盤面上方垂直下落,直到下一步碰到既有方塊或地板,這就是該方位的唯一落點。

加入落下方塊,統計完整8格的列數乘8。每種取其最大,最後輸出達到全域最大值的所有編號。沒有懸空輸入且保證可消列,因此不用加入遊戲外的側滑或旋轉穿牆規則。

形狀數、旋轉數、寬度與既有高度皆固定,時間及空間O(1)。

Step-by-step animation

旋轉形狀、水平枚舉與垂直落下|上列全空,下列****0000。

回報動畫錯誤
步驟 1 / 3

盤面

上列全空,下列****0000。

00000000
****0000
空缺
右4格

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
int main() {
    string top, bottom;
    cin >> top >> bottom;
    set<pair<int, int>> board;
    for (int x = 0; x < 8; x++) {
        if (bottom[x] == '*')
            board.insert({x, 0});
        if (top[x] == '*')
            board.insert({x, 1});
    }
    vector<vector<pair<int, int>>> sh = {{{0, 0}, {1, 0}, {0, 1}, {1, 1}},
                                         {{0, 0}, {1, 0}, {2, 0}, {3, 0}},
                                         {{0, 2}, {0, 1}, {1, 1}, {1, 0}},
                                         {{1, 2}, {0, 1}, {1, 1}, {0, 0}},
                                         {{1, 1}, {0, 0}, {1, 0}, {2, 0}}};
    vector<int> score(5);
    for (int k = 0; k < 5; k++) {
        auto s = sh[k];
        for (int rot = 0; rot < 4; rot++) {
            int mnx = 100, mny = 100, mx = 0;
            for (auto [x, y] : s) {
                mnx = min(mnx, x);
                mny = min(mny, y);
            }
            for (auto &[x, y] : s) {
                x -= mnx;
                y -= mny;
                mx = max(mx, x);
            }
            for (int shift = 0; shift + mx < 8; shift++) {
                auto can = [&](int h) {
                    for (auto [x, y] : s)
                        if (y + h < 0 || board.count({x + shift, y + h}))
                            return false;
                    return true;
                };
                int h = 6;
                while (can(h - 1))
                    h--;
                auto b = board;
                for (auto [x, y] : s)
                    b.insert({x + shift, y + h});
                int full = 0;
                for (int row = 0; row < 6; row++) {
                    bool ok = true;
                    for (int x = 0; x < 8; x++)
                        if (!b.count({x, row}))
                            ok = false;
                    full += ok;
                }
                score[k] = max(score[k], 8 * full);
            }
            for (auto &[x, y] : s) {
                int z = x;
                x = -y;
                y = z;
            }
        }
    }
    int best = *max_element(score.begin(), score.end());
    bool first = true;
    for (int k = 0; k < 5; k++)
        if (score[k] == best) {
            if (!first)
                cout << " ";
            first = false;
            cout << k + 1;
        }
    cout << "\n" << best << "\n";
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多