class Solution {
public:
void solve(vector<vector<char>>& board) {
for(int i = 0; i < board.size(); i++)
for(int j = 0; j < board[0].size(); j++)
if(board[i][j] == 'O')
{
vector<pair<int, int>> temp;
if(!dfs(board, temp, i, j))
{
for(int k = 0; k < temp.size(); k++)
{
board[temp[k].first][temp[k].second] = 'Z';
}
}
}
for(int i = 0; i < board.size(); i++)
for(int j = 0; j < board[0].size(); j++)
if(board[i][j] == 'Z')
board[i][j] = 'O';
}
bool dfs(vector<vector<char>>& board, vector<pair<int, int>>& all, int x, int y)
{
all.push_back(make_pair(x, y));
board[x][y] = 'X';
bool t1, t2, t3, t4;
t1 = t2 = t3 = t4 = true;
if(x1 >= 0 && board[x1][y] == 'O')
t1 = dfs(board, all, x1, y);
if(y1 >= 0 && board[x][y1] == 'O')
t2 = dfs(board, all, x, y1);
if(x+1 < board.size() && board[x+1][y] == 'O')
t3 = dfs(board, all, x+1, y);
if(y+1 < board[0].size() && board[x][y+1] == 'O')
t4 = dfs(board, all, x, y+1);
if(x == 0  x == board.size()1  y == 0  y == board[0].size()1)
return false;
return t1 && t2 && t3 && t4;
}
};
C++ solution. I think my solution should work. But it failed in the 200row test case. Anyone can help me with this?


if(x1 > 0 && board[x1][y] == 'O') {// no equal board[x1][y] = 'X'; //set first t1 = dfs(board, all, x1, y); } if(y1 > 0 && board[x][y1] == 'O'){ board[x][y1] = 'X'; //set first t2 = dfs(board, all, x, y1); } if(x+1 < board.size() && board[x+1][y] == 'O'){ board[x+1][y] = 'X'; //set first t3 = dfs(board, all, x+1, y); } if(y+1 < board[0].size() && board[x][y+1] == 'O'){ board[x][y+1] = 'X'; //set first t4 = dfs(board, all, x, y+1); }

@yin10 Format your code properly, please! Thank you! B.T.W. your suggestion is right.

@ylf951 Actually there several posts discussing on this topic. https://discuss.leetcode.com/topic/49025/themostintuitivecsolutionyetencounteredunknownruntimeerror/6