题目:130. 被围绕的区域
给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。
找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。
示例:
X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:
X X X X
X X X X
X X X X
X O X X
解释:
被围绕的区间不会存在于边界上,换句话说,任何边界上的 ‘O’ 都不会被填充为 ‘X’。 任何不在边界上,或不与边界上的 ‘O’ 相连的 ‘O’ 最终都会被填充为 ‘X’。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
解题思路:
解题步奏
1)、每行从左往右合并;sloveProblem()主要是合并正上方有关联的数据;map<Vec_2 , bool , cmpStruct>存储的是有联系的位置信息;
2)、删除含有边界的map集合;
3)、剩下的map集合修改,将‘O’修改为‘X’;
具体实现
class Solution {
public:
struct Vec_2{
int first ;
int second ;
Vec_2(int x , int y): first(x) , second(y){}
};
struct cmpStruct
{
bool operator()(const Vec_2 & x , const Vec_2 & y) const
{
return y.first < x.first || (y.first == x.first && y.second < x.second) ;
}
};
void printMap(map<Vec_2 , bool, cmpStruct>& data)
{
for (auto tmp : data)
cout << '[' << tmp.first.first << ',' << tmp.first.second << "] = " << tmp.second << '\t' ;
cout << endl ;
}
void printData(vector<map<Vec_2 , bool , cmpStruct>>& data)
{
int c_i = 0 ;
while (c_i < data.size())
{
for (auto tmp : data[c_i])
cout << '[' << tmp.first.first << ',' << tmp.first.second << "] = " << tmp.second << '\t' ;
cout << endl ;
c_i ++ ;
}
}
void sloveDel(vector<map<Vec_2 , bool , cmpStruct>>& data , Vec_2 tmp)
{
int lenVec = data.size() - 1 ;
while (lenVec > -1)
{
if (data[lenVec].find(tmp) != data[lenVec].end())
{
data[lenVec].clear() ;
data.erase(data.begin() + lenVec) ;
}
lenVec -- ;
}
}
void sloveProblem(vector<map<Vec_2 , bool , cmpStruct>>& data , map<Vec_2 , bool , cmpStruct>& tmp , int c_i , int c_j)
{
int lenVec = data.size() - 1;
Vec_2 tmpStruct{c_i , c_j} ;
while (lenVec > -1)
{
if (data[lenVec].find(tmpStruct) != data[lenVec].end())
{
for(auto tmp_d : data[lenVec])
tmp[tmp_d.first] = tmp_d.second ;
data.erase(data.begin() + lenVec) ;
break ;
}
lenVec -- ;
}
}
void solve(vector<vector<char>>& board) {
if (board.size() < 2)
return ;
else if (board[0].size() < 2)
return ;
vector<map<Vec_2 , bool , cmpStruct>> data ;
map<Vec_2 , bool, cmpStruct> tmp ;
int c_i , c_j ;
for (c_i = 0 ; c_i < board.size() ; c_i ++)
{
for (c_j = 0 ; c_j < board[c_i].size() ; c_j ++)
{
if (board[c_i][c_j] == 'O')
{
Vec_2 cdata{c_i , c_j} ;
if (c_j == 0)
{
printMap(tmp) ;
if (tmp.size() > 0)
data.push_back(tmp) ;
tmp.clear() ;
}
else if (board[c_i][c_j - 1] != 'O')
{
printMap(tmp) ;
if (tmp.size() > 0)
data.push_back(tmp) ;
tmp.clear() ;
}
tmp.insert(make_pair(cdata , true)) ;
if (c_i && board[c_i - 1][c_j] == 'O')
{
sloveProblem(data , tmp , c_i - 1 , c_j) ;
printData(data) ;
}
}// inner-if
}// in-for
}//out-for
data.push_back(tmp) ;
int len = board.size() ;
for (c_i = 0 ; c_i < board[c_i].size() ; c_i ++)
{
if (board[0][c_i] == 'O' )
{
Vec_2 delStruct{0 , c_i} ;
sloveDel(data , delStruct) ;
}
if (board[len - 1][c_i] == 'O')
{
Vec_2 delStruct{len - 1 , c_i} ;
sloveDel(data , delStruct) ;
}
}
len = board[0].size() ;
for (c_i = 0 ; c_i < board.size() ; c_i ++)
{
if (board[c_i][0] == 'O' )
{
Vec_2 delStruct{c_i , 0} ;
sloveDel(data , delStruct) ;
}
if (board[c_i][len - 1] == 'O')
{
Vec_2 delStruct{c_i , len - 1} ;
sloveDel(data , delStruct) ;
}
}//for - single
for (c_i = 0 ; c_i < data.size() ; c_i ++)
{
for (auto iter : data[c_i])
board[iter.first.first][iter.first.second] = 'X' ;
}
}
};