1591. 奇怪的打印机 II

贪心贪出一片天。先判断图上的完整的长方形,然后设置成0,0认为是任意颜色,然后继续找长方形,直到找不到为止,如果矩形全部为0则表示可达。

看别人是用的拓扑排序。

class Solution {
public:
    bool solve(vector<vector<int>>&mp, int color, int x, int y, int x_max, int y_max) {
    //    cout << x << " " << y << " " << x_max << " " << y_max << endl;
        // const int &color = mp[x][y];
        for (int i = 0; i < mp.size(); i++) {
            for (int j = 0; j < mp[i].size(); j++) {
                if (x <= i && i <= x_max && y <= j && j <= y_max) {
                    if (mp[i][j] != color && mp[i][j] != 0)
                        return false;
                }
                else if (mp[i][j] == color)
                    return false;
            }
        }
        for (int i = x; i <= x_max; i++)
            for (int j = y; j <= y_max; j++)
                mp[i][j] = 0;
        // cout << x << " " << y << " " << x_max << " " << y_max << endl;
        return true;
    }
    bool delete_color(vector<vector<int>> &mp) {
        const int maxn = 61;
        bool color[maxn] = {false};
        for (int i = 0; i < mp.size(); i++) {
            for (int j = 0; j < mp[i].size(); j++) {
                const int &index = mp[i][j];
                if (!color[index] && index != 0) {
                    color[index] = true;
                    int x_begin = INT_MAX;
                    int y_begin = INT_MAX;
                    int x_end = 0;
                    int y_end = 0;
                    for (int x = 0; x < mp.size(); x++) {
                        for (int y = 0; y < mp[x].size(); y++)
                            if (index == mp[x][y]) {
                                x_begin = min(x_begin, x);
                                y_begin = min(y_begin, y);
                                x_end = max(x_end, x);
                                y_end = max(y_end, y);
                            }
                    }
                    if (solve(mp, index, x_begin, y_begin, x_end, y_end))
                        return true;
                }
            }
        }
        return false;
    }
    bool judge(vector<vector<int>> &mp) {
        for (auto &tmp:mp) {
            for (auto &index:tmp) {
                if (index)
                    return false;
            }
        }
        return true;
    }
    bool isPrintable(vector<vector<int>>& targetGrid) {
        if (targetGrid.empty() || targetGrid[0].empty())
            return true;
        while (delete_color(targetGrid)) {
        }
        return judge(targetGrid);
    }
};

/*
[6,2,2,5],
[2,2,2,5],
[2,2,2,5],
[4,3,3,4]
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值