LeetCode: Surrounded Regions
看了网上答案,dfs
1 class Solution { 2 public: 3 void dfs(vector<vector<char>> &board, int x, int y) { 4 if (x < 0 || x >= board.size() || y < 0 || y >= board.size()) return; 5 if (board[x][y] == 'O') { 6 board[x][y] = '#'; 7 dfs(board, x-1, y); 8 dfs(board, x+1, y); 9 dfs(board, x, y-1); 10 dfs(board, x, y+1); 11 } 12 } 13 void solve(vector<vector<char>> &board) { 14 // Start typing your C/C++ solution below 15 // DO NOT write int main() function 16 if (!board.size()) return; 17 int n = board[0].size(); 18 for (int i = 1; i < n-1; i++) { 19 dfs(board, i, 0); 20 dfs(board, i, n-1); 21 } 22 for (int i = 0; i < n; i++) { 23 dfs(board, 0, i); 24 dfs(board, n-1, i); 25 } 26 for (int i = 0; i < n; i++) { 27 for (int j = 0; j < n; j++) { 28 if (board[i][j] == 'O') board[i][j] = 'X'; 29 if (board[i][j] == '#') board[i][j] = 'O'; 30 } 31 } 32 } 33 };
自己写的bfs代码:
1 class Solution { 2 public: 3 struct node { 4 int x, y; 5 node(int a, int b) : x(a), y(b) { } 6 node() : x(0), y(0) { } 7 }; 8 void solve(vector<vector<char>> &board) { 9 if (board.size() <= 2 || board[0].size() <= 2) return; 10 int m = board.size(); 11 int n = board[0].size(); 12 vector<vector<bool> > visit(m, vector<bool>(n)); 13 int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; 14 queue<node> S; 15 for (int i = 1; i < m-1; i++) { 16 if (board[i][0] == 'O') S.push(node(i, 0)); 17 if (board[i][n-1] == 'O') S.push(node(i, n-1)); 18 } 19 for (int i = 1; i < n-1; i++) { 20 if (board[0][i] == 'O') S.push(node(0, i)); 21 if (board[m-1][i] == 'O') S.push(node(m-1, i)); 22 } 23 while (!S.empty()) { 24 node front = S.front(); 25 S.pop(); 26 for (int i = 0; i < 4; i++) { 27 int xx = front.x + dir[i][0]; 28 int yy = front.y + dir[i][1]; 29 if (xx <= 0 || xx >= m-1 || yy <= 0 || yy >= n-1 || board[xx][yy] == 'X' || visit[xx][yy]) continue; 30 S.push(node(xx, yy)); 31 visit[xx][yy] = true; 32 } 33 } 34 for (int i = 1; i < m-1; i++) { 35 for (int j = 1; j < n-1; j++) { 36 if (board[i][j] == 'X') continue; 37 else if (!visit[i][j]) board[i][j] = 'X'; 38 } 39 } 40 } 41 };
C#
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Solution { 2 public class node { 3 public int x; 4 public int y; 5 public node() { x = 0; y = 0; } 6 public node (int a, int b) { x = a; y = b; } 7 } 8 public void Solve(char[,] board) { 9 int m = board.GetLength(0); 10 int n = board.GetLength(1); 11 if (m <= 2 || n <= 2) return; 12 bool[,] visit = new bool[m, n]; 13 int[,] dir = new int[4, 2] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; 14 Queue<node> S = new Queue<node>(); 15 for (int i = 1; i < m-1; i++) { 16 if (board[i, 0] == 'O') S.Enqueue(new node(i, 0)); 17 if (board[i, n-1] == 'O') S.Enqueue(new node(i, n-1)); 18 } 19 for (int i = 1; i < n-1; i++) { 20 if (board[0, i] == 'O') S.Enqueue(new node(0, i)); 21 if (board[m-1, i] == 'O') S.Enqueue(new node(m-1, i)); 22 } 23 while (S.Count > 0) { 24 node peek = S.Peek(); 25 S.Dequeue(); 26 for (int i = 0; i < 4; i++) { 27 int xx = peek.x + dir[i, 0]; 28 int yy = peek.y + dir[i, 1]; 29 if (xx <= 0 || xx >= m-1 || yy <= 0 || yy >= n-1 || board[xx, yy] == 'X' || visit[xx, yy]) continue; 30 S.Enqueue(new node(xx, yy)); 31 visit[xx, yy] = true; 32 } 33 } 34 for (int i = 1; i < m-1; i++) { 35 for (int j = 1; j < n-1; j++) { 36 if (board[i, j] == 'X') continue; 37 else if (!visit[i, j]) board[i, j] = 'X'; 38 } 39 } 40 } 41 }