错误代码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
但是上面这段代码的问题在于:
判断是否有可以被capture
的0
时,需要递归,也即BFS
额。。bfs版本晚上洗完了衣服就写~