leetcode[130] Surrounded Regions
给定一个类似棋盘,有X和O,把X圈住的O变为X例如:
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X X
class Solution { public: void dfs(vector<vector<char> > &board, int x, int y) { if(x<0 || x>=board.size() || y<0 || y>=board[0].size() || board[x][y]!='O') return; board[x][y]='#'; dfs(board, x-1,y); dfs(board, x+1,y); dfs(board, x,y-1); dfs(board, x,y+1); } void solve(vector<vector<char> > &board) { if (board.size() < 3) return ; if (board[0].size() < 3) return ; int m = board.size(), n = board[0].size(); for(int j=0;j<n;j++) { dfs(board,0,j); dfs(board,m-1,j); } for(int i=1;i<m-1;i++) { dfs(board,i,0); dfs(board,i,n-1); } 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'; } } };
代码就是对上下左右每个边开始DFS解决。可是会栈溢出,哎,这个error略高级,一直摸不清。
如果把DFS的函数改为如下的形式,就可以AC了。
不同之处就是上面的dfs中判断了上下左右,而下面这个的上下左右是在主函数里,dfs是对内层处理。
class Solution { public: void dfs(vector<vector<char>> &board, int i, int j) { int row = board.size(), col = board[0].size(); if(i > 1 && board[i-1][j] == 'O') { board[i-1][j] = '#'; dfs(board, i-1, j); } if(i < row-1 && board[i+1][j] == 'O') { board[i+1][j] = '#'; dfs(board, i+1, j); } if(j > 1 && board[i][j-1] == 'O') { board[i][j-1] = '#'; dfs(board, i, j-1); } if(j < col-1 && board[i][j+1] == 'O') { board[i][j+1] = '#'; dfs(board, i, j+1); } } void solve(vector<vector<char> > &board) { if (board.size() < 3) return ; if (board[0].size() < 3) return ; int m = board.size(), n = board[0].size(); for(int j=0;j<n;j++) { if (board[0][j] == 'O') { board[0][j] = '#'; dfs(board,0,j); } if (board[m-1][j] == 'O') { board[m-1][j] = '#'; dfs(board,m-1,j); } } for(int i=1;i<m-1;i++) { if (board[i][0] == 'O') { board[i][0] = '#'; dfs(board,i,0); } if (board[i][n-1] == 'O') { board[i][n-1] = '#'; dfs(board,i,n-1); } } 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'; } } };
如果利用BFS的话:
BFS就是每次处理的时候对该元素的四周的进行处理,而不是和上面所述一样进行元素的一边处理到返回为止。这里用BFS就不会出现溢出的问题。
我们用pair来存x和y的值,注意可以用make_pair()来获取pair对。
如下:
class Solution { public: //130 bfs void bfs130(vector<vector<char> > &board, int x, int y) { if(x<0 || x>=board.size() || y<0 || y>=board[0].size() || board[x][y]!='O') return; board[x][y] = '#'; queue<pair<int, int> > que; que.push(make_pair(x, y)); // the use of make_pair while(!que.empty()) { pair<int, int> tmpPair = que.front(); que.pop(); int tmpx = tmpPair.first, tmpy = tmpPair.second; if (tmpx > 0 && board[tmpx - 1][tmpy] == 'O') // up { board[tmpx - 1][tmpy] = '#'; que.push(make_pair(tmpx-1, tmpy)); } if (tmpx < board.size()-1 && board[tmpx + 1][tmpy] == 'O') // down { board[tmpx + 1][tmpy] = '#'; que.push(make_pair(tmpx+1, tmpy)); } if (tmpy > 0 && board[tmpx][tmpy - 1] == 'O') // left { board[tmpx][tmpy - 1] = '#'; que.push(make_pair(tmpx, tmpy-1)); } if (tmpy < board[0].size()-1 && board[tmpx][tmpy + 1] == 'O') // right { board[tmpx][tmpy + 1] = '#'; que.push(make_pair(tmpx, tmpy+1)); } } return ; } void solve(vector<vector<char> > &board) { if (board.size() < 3) return ; if (board[0].size() < 3) return ; for (int j = 0; j < board[0].size(); ++j) { bfs130(board, 0, j); bfs130(board, board.size()-1, j); } for (int i = 1; i < board.size()-1; ++i) // i == 0 && i == board.size()-1 has been done before { bfs130(board, i, 0); bfs130(board, i, board[0].size()-1); } for (int i = 0; i < board.size(); ++i) for (int j = 0; j < board[0].size(); ++j) { if (board[i][j] == 'O') board[i][j] = 'X'; else if (board[i][j] == '#') board[i][j] = 'O'; } } };