leetcode surround regions

错误代码1:
class Solution {
public:
    const int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
    bool isvalid(int tmph, int tmpw,const int h,const int w){
        return (tmph > 0 && tmph < h - 1 && w > 0 && w < tmpw - 1);
    }
    bool isEdge(int tmph, int tmpw,const int h, const int w){
        if(tmph == 0 || tmph == h - 1 || tmpw == 0 || tmpw == w - 1){
            return true;
        }
        return false;
    }
    bool isAbleToFill(int tmph, int tmpw, const int h, const int w, vector<vector<char> >&board){
        bool isAble = true;
        if(board[tmph][tmpw] == 'X'){
            return true;
        }
        for(int i = 0; i < 4; ++i){
            int tmpx = tmph + dir[i][0];
            int tmpy = tmpw + dir[i][1];
            if(isvalid(tmpx, tmpy, h, w) || ())
        }
    }
    void solve(vector<vector<char>> &board) {
        int h = board.size();
        if(h == 0){
            return;
        }
        int w = board[0].size();
        if(w == 0){
            return;
        }
        if(h <= 2 || w <= 2){
            return;
        }
        int uph = 1, dwh = h - 2, lw = 1, rw = w - 2;
        while(uph <= dwh && lw <= rw){ // a rectangle
            for(int i = lw; i <= rw; ++i){
                isAbleToFill(uph, i, h, w, board);
                isAbleToFill(dwh, i, h, w, board);
            }
            for(int i = uph; i <= dwh; ++i){
                isAbleToFill(i, lw, h, w, board);
                isAbleToFill(i, rw, h, w, board);
            }
            uph++;
            dwh--;
            lw++;
            rw--;
        }
    }
};

思路:最外圈的’0’无法被capture,从外向内一圈一圈的扫描,看到有’0’,就看它上下左右的四个board格子里的值,如果都是’X’,就可以被capture,否则,如果有可以被capture的’0’,也可以被caputre
但是上面这段代码的问题在于:
判断是否有可以被capture0时,需要递归,也即BFS

额。。bfs版本晚上洗完了衣服就写~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值