64,642
社区成员
发帖
与我相关
我的任务
分享
class Solution {
public:
void sloveProblemChange(vector<vector<vector<int>>>& data , vector<vector<int>>& tmp , int c_i , int c_j)
{
int lenVec = data.size() - 1;
bool station = false ;
while (lenVec > -1)
{
for (int c_x = 0 ; c_x < data[lenVec].size() ; c_x ++)
{
if (data[lenVec][c_x][0] == c_i && c_j == data[lenVec][c_x][1])
{
station = true ;
break ;
}
}
if (station)
{
for(auto tmp_d : data[lenVec])
tmp.push_back(tmp_d) ;
//data[lenVec].clear() ;
data.erase(data.begin() + lenVec) ;
break ;
}
lenVec -- ;
}
}
void sloveDelChange(vector<vector<vector<int>>>& data , int c_i , int c_j)
{
int lenVec = data.size() - 1 ;
bool station = false ;
while (lenVec > -1)
{
for (int c_x = 0 ; c_x < data.at(lenVec).size() ; c_x ++)
{
if (data[lenVec][c_x][0] == c_i && c_j == data[lenVec][c_x][1])
{
station = true ;
break ;
}
}
if (station)
{
data[lenVec].clear() ;
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<vector<vector<int>>> data ;
vector<vector<int>> tmp ;
vector<int> single_cmp(2 , -1) ;
int c_i , c_j ;
int lenx = board.size() ;
int leny = board[0].size() ;
for (c_i = 0 ; c_i < lenx ; c_i ++)
{
//single_cmp.clear() ;
for (c_j = 0 ; c_j < leny ; c_j ++)
{
if (board[c_i][c_j] == 'O')
{
single_cmp[0] = c_i ;
single_cmp[1] = c_j ;
if (c_j == 0)
{
if (tmp.size() > 0)
data.push_back(tmp) ;
tmp.clear() ;
}
else if (board[c_i][c_j - 1] != 'O')
{
if (tmp.size() > 0)
data.push_back(tmp) ;
tmp.clear() ;
}
tmp.push_back(single_cmp) ;
if (c_i != 0 && board[c_i - 1][c_j] == 'O')
{
//printVectIn(data) ;
sloveProblemChange(data , tmp , c_i - 1 , c_j) ;
//printVectIn(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' )
sloveDelChange(data , 0 , c_i) ;
if (board[len - 1][c_i] == 'O')
sloveDelChange(data , len - 1 , c_i) ;
}
len = board[0].size() ;
for (c_i = 0 ; c_i < board.size() ; c_i ++)
{
if (board[c_i][0] == 'O' )
sloveDelChange(data , c_i , 0) ;
if (board[c_i][len - 1] == 'O')
sloveDelChange(data , c_i , len - 1) ;
}//for - single
for (c_i = 0 ; c_i < data.size() ; c_i ++)
{
for (auto iter : data[c_i])
board[iter[0]][iter[1]] = 'X' ;
}
}
};