6/03/2014

Leetcode -- Surrounding Regions

Basically just copied from Internet.
 class Solution {  
 public:  
   void solve(vector<vector<char>> &board) {  
     if(board.empty()) {  
       return;  
     }  
     vector<int> xIndex;  
     vector<int> yIndex;  
     int row = board.size();  
     int column = board[0].size();  
     for(int i = 0; i < row; ++i) {  
       if(board[i][0] == 'O') {  
         xIndex.push_back(i);  
         yIndex.push_back(0);  
       }  
       if(board[i][column - 1] == 'O') {  
         xIndex.push_back(i);  
         yIndex.push_back(column - 1);  
       }  
     }  
     for(int i = 1; i < column; ++i) {  
       if(board[0][i] == 'O') {  
         xIndex.push_back(0);  
         yIndex.push_back(i);  
       }  
       if(board[row - 1][i] == 'O') {  
         xIndex.push_back(row - 1);  
         yIndex.push_back(i);  
       }  
     }  
     int k = 0;  
     for(int i = 0; i < xIndex.size(); ++i) {  
       int x = xIndex[i];  
       int y = yIndex[i];  
       board[x][y] = 'Y';  
       if(x > 0 && board[x-1][y] == 'O') {  
         xIndex.push_back(x-1);  
         yIndex.push_back(y);  
       }  
       if(x < row - 1 && board[x+1][y] == 'O') {  
         xIndex.push_back(x + 1);  
         yIndex.push_back(y);  
       }  
       if(y > 0 && board[x][y - 1] == 'O') {  
         xIndex.push_back(x);  
         yIndex.push_back(y- 1);  
       }  
       if(y < column - 1 && board[x][y + 1] == 'O') {  
         xIndex.push_back(x);  
         yIndex.push_back(y+ 1);  
       }  
     }  
     for(int i = 0; i < row; ++i) {  
       for(int j = 0; j < column; ++j) {  
         if(board[i][j] == 'Y') {  
           board[i][j] = 'O';  
         } else {  
           board[i][j] = 'X';  
         }  
       }  
     }  
   }  
 };  

No comments: