a541中等二維畫布與迭代洪水填色

CPE:Graphical Editor (UVa 10267)

按照命令更新像素,保持座標、連通性與整行解析正確。

前往題目練習 →回報教學錯誤矩形與清除最壞 O(MN),填色 O(MN),空間 O(MN)。

解題思路

用 N 列、M 欄的字元矩陣表示畫布,外部座標 (X,Y) 對應陣列 [Y−1][X−1]。L、V、H、K 分別處理單點、直線與矩形;V、H 兩端可能反向,先取 min/max。I 重新建立白色畫布,C 只清色。

F 從指定點取原色,用堆疊走訪四方向同色格,入堆疊前立即改色以免重複。新色等於原色時直接略過,避免無限走訪。每次整行解析,未知指令整行忽略;S 先印名稱,再逐列連續印字元,沒有格間空白。X 結束。

Step-by-step animation

示意:3×2 畫布,點(2,1)先塗A

回報動畫錯誤
步驟 1 / 3

初始化

I 3 2 建立六個 O。

OOO
OOO
寬
3
高
2

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;

int main() {
    vector<string> g;
    int m = 0, n = 0;
    string line;
    while (getline(cin, line)) {
        istringstream in(line);
        char op;
        if (!(in >> op))
            continue;
        if (op == 'X')
            break;
        if (op == 'I') {
            in >> m >> n;
            g.assign(n, string(m, 'O'));
        } else if (op == 'C')
            for (auto &r : g)
                r.assign(m, 'O');
        else if (op == 'L') {
            int x, y;
            char c;
            in >> x >> y >> c;
            g[y - 1][x - 1] = c;
        } else if (op == 'V') {
            int x, a, b;
            char c;
            in >> x >> a >> b >> c;
            for (int y = min(a, b); y <= max(a, b); ++y)
                g[y - 1][x - 1] = c;
        } else if (op == 'H') {
            int a, b, y;
            char c;
            in >> a >> b >> y >> c;
            for (int x = min(a, b); x <= max(a, b); ++x)
                g[y - 1][x - 1] = c;
        } else if (op == 'K') {
            int a, b, x, y;
            char c;
            in >> a >> b >> x >> y >> c;
            for (int r = min(b, y); r <= max(b, y); ++r)
                for (int s = min(a, x); s <= max(a, x); ++s)
                    g[r - 1][s - 1] = c;
        } else if (op == 'F') {
            int x, y;
            char c;
            in >> x >> y >> c;
            --x;
            --y;
            char old = g[y][x];
            if (old == c)
                continue;
            vector<pair<int, int>> st{{y, x}};
            g[y][x] = c;
            while (!st.empty()) {
                auto [r, s] = st.back();
                st.pop_back();
                for (auto [dr, dc] : vector<pair<int, int>>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
                    int a = r + dr, b = s + dc;
                    if (a >= 0 && a < n && b >= 0 && b < m && g[a][b] == old) {
                        g[a][b] = c;
                        st.emplace_back(a, b);
                    }
                }
            }
        } else if (op == 'S') {
            string name;
            in >> name;
            cout << name << '\n';
            for (auto &r : g)
                cout << r << '\n';
        }
    }
}

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

更多