周围区域不应位于边界上,这意味着板边框上的任何“O”都不会翻转到“X”。 任何不在边框上并且未连接到边框上的“O”的“O”将翻转为“X”。 如果它们是水平或垂直连接的相邻单元,则连接两个单元。
解决思路:只有边界上是‘O’以及与边界是‘O’相连的不变,其余的‘O’要变为’X’
对棋盘进行遍历,利用DFS将边界上的以及与边界相连的‘O’先存成’*’
然后对整个棋盘进行遍历,将依旧是’O’的改为’X’,将是’*’的改为’O’
C++代码
struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
};
class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[0].empty())
return;
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i ++)
{
for(int j = 0; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == 0 || i == m-1 || j == 0 || j == n-1)
{// remain 'O' on the boundry
dfs(board, i, j, m, n);
}
}
}
}
for(int i = 0; i < m; i ++)
{
for(int j = 0; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
stack<POS*> stk;
POS* pos = new POS(i, j);
stk.push(pos);
board[i][j] = '*';
while(!stk.empty())
{
POS* top = stk.top();
if(top->x > 0 && board[top->x-1][top->y] == 'O')
{
POS* up = new POS(top->x-1, top->y);
stk.push(up);
board[up->x][up->y] = '*';
continue;
}
if(top->x < m-1 && board[top->x+1][top->y] == 'O')
{
POS* down = new POS(top->x+1, top->y);
stk.push(down);
board[down->x][down->y] = '*';
continue;
}
if(top->y > 0 && board[top->x][top->y-1] == 'O')
{
POS* left = new POS(top->x, top->y-1);
stk.push(left);
board[left->x][left->y] = '*';
continue;
}
if(top->y < n-1 && board[top->x][top->y+1] == 'O')
{
POS* right = new POS(top->x, top->y+1);
stk.push(right);
board[right->x][right->y] = '*';
continue;
}
stk.pop();
}
}
};
将其改写为C代码+STL
C中的结构体+DFS代码块