https://oj.leetcode.com/problems/surrounded-regions/
棋盘类的题目。找出所有没有被 X 包围的 O
使用深搜,但是太深了,run time error
可以自己写个栈,模拟深搜,取数据存入,弹出的
后来参考了九章
struct point{ int x; int y; point(int x, int y) { this->x = x; this->y = y; } }; class Solution { public: queue<point> myQueue; int row; int col; void solve(vector<vector<char>> &board) { if(board.empty() || board.size() == 0 || board[0].size() == 0) return; row = board.size(); col = board[0].size(); // push 边上的 O in queue for(int i = 0; i < row; i++) { if(board[i][0] == 'O') myQueue.push(point(i,0)); if(col >= 2 && board[i][col - 1] == 'O') myQueue.push(point(i,col-1)); } for(int j = 0; j< col; j++) { if(board[0][j] == 'O') myQueue.push(point(0,j)); if(board[row-1][j] == 'O') myQueue.push(point(row-1,j)); } point tempPoint(0,0); // 对queue中的每一个位置,处理它的上下左右 while(myQueue.empty() == false) { tempPoint = myQueue.front(); myQueue.pop(); int x = tempPoint.x; int y = tempPoint.y; board[x][y] = 'K'; // mark as K, which can't be chagned into X in the end //left if(y >= 1 && board[x][y-1] == 'O') myQueue.push(point(x,y-1)); //right if(y+1 < col && board[x][y+1] == 'O') myQueue.push(point(x,y+1)); //up if(x-1 >= 0 && board[x-1][y] == 'O') myQueue.push(point(x-1,y)); //down if(x+1 < row && board[x+1][y] == 'O') myQueue.push(point(x+1,y)); } for(int i = 0; i < row; i++) for(int j = 0; j < col; j++) { if(board[i][j] == 'O') board[i][j] = 'X'; else if(board[i][j] == 'K') board[i][j] = 'O'; } return; } };
下面是我的解法,但是测试数据大的时候过不去。。。
class Solution { public: vector<vector<bool> > isSafe; vector<vector<char> > board; vector<vector<bool> > visited; void solve(vector<vector<char>> &board) { if(board.empty() || board.size() == 0 || board[0].size() == 0) return; this->board = board; isSafe.resize(board.size()); visited.resize(board.size()); // init for(int i = 0; i < board.size(); i++) { isSafe[i].resize(board[0].size()); visited[i].resize(board[0].size()); for(int j = 0; j < board[0].size(); j++) { isSafe[i][j] = true; if(board[i][j] == 'X') visited[i][j] = true; else visited[i][j] = false; } } for(int i = 0; i < board.size(); i++) for(int j = 0; j < board[0].size(); j++) { if(!visited[i][j]) isSurround(i,j); } //isSurround(0,0); for(int i = 0; i < board.size(); i++) for(int j = 0; j < board[0].size(); j++) if(isSafe[i][j]) board[i][j] = 'X'; return; } void isSurround(int i, int j) { if(i == board.size()) return; // handle 边 if(board[i][j] == 'O' && (i == 0 || i == board.size() - 1 || j == 0 || j == board[0].size() - 1)) { isSafe[i][j] = false; visited[i][j] = true; } if(isSafe[i][j] == false) { // mark right and down for(int p = j + 1; p < board[0].size(); p++) { if(board[i][p] == 'O') { isSafe[i][p] = false; visited[i][p] = true; } else break; } for(int q = i + 1; q < board.size(); q++) { if(board[q][j] == 'O') { isSafe[q][j] = false; visited[q][j] = true; } else break; } } else if(board[i][j] == 'O') { visited[i][j] = true; // left if(j > 0 && board[i][j-1] == 'O' && !visited[i][j-1]) isSurround(i,j-1); if(j > 0 && isSafe[i][j-1] == false) { isSafe[i][j] = false; return; } // up if(i > 0 && board[i-1][j] == 'O' && !visited[i-1][j]) isSurround(i-1,j); if(i > 0 && isSafe[i-1][j] == false) { isSafe[i][j] = false; return; } // right is O if((j < board[0].size() - 1) &&board[i][j+1] == 'O' && !visited[i][j+1]) isSurround(i,j+1); if((j < board[0].size() - 1) && isSafe[i][j+1] == false) { isSafe[i][j] = false; return; } // down is O if((i < board.size() - 1) && board[i+1][j] == 'O' && !visited[i+1][j]) isSurround(i+1,j); if((j < board.size() - 1) && isSafe[i+1][j] == false) { isSafe[i][j] = false; return; } visited[i][j] = true; } } };