Leetcode: Surrounded regions

Avril發表於2013-08-30

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.


A region is captured by flipping all 'O's into 'X's in that surrounded region .


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
 

我的最初思路是直接找被包圍的O區域,如果這個區域不與邊界接觸則是被包圍區域,但是這種判斷要等到dfs完成才能知道是否是被包圍區域,所以需要再一次dfs來將這些點變成'X'。所以這樣的複雜度太高,Judge Large超時。這個版本的程式碼包括下面的dfs和changeBoard,以及Solve中被註釋掉的部門程式碼;後來參考了網上的一個程式碼,將思路轉變為先對不被包圍的區域做標記'C',然後再遍歷一遍按要求修改。這樣就避免了做兩遍深度搜尋。

程式碼如下:(有點難看,見諒……)

  1 //
  2 //  SurroundedRegions.cpp
  3 //  SJMcode
  4 //
  5 //  Created by Jiamei Shuai on 13-8-30.
  6 //  Copyright (c) 2013年 Jiamei Shuai. All rights reserved.
  7 //
  8 
  9 
 10 #include <iostream>
 11 #include <vector>
 12 #include <assert.h>
 13 using namespace std;
 14 
 15 class Solution {
 16 public:
 17     
 18     void dfs2(vector<vector<char>> &board, int i, int j)
 19     {
 20         if(i > board.size()-1 || i < 0 || j > board[0].size()-1 || j < 0)
 21             return;
 22         
 23         if(board[i][j] == 'O')
 24         {
 25             board[i][j] = 'C';
 26             dfs2(board, i+1, j);
 27             dfs2(board, i-1, j);
 28             dfs2(board, i, j-1);
 29             dfs2(board, i, j+1);
 30         }
 31             
 32 
 33     }
 34     
 35     bool dfs(vector<vector<char>> &board, int i, int j, int height, int width, vector<vector<int>> &isVis, bool &flag) // working but too slow
 36     {
 37         //if(board[i][j] == 'X')  return true;
 38         
 39         if(i == height-1 || i == 0 || j == width-1 || j == 0)
 40             flag = false; // 'o' touch the border: means this block cannot be the answer
 41         
 42         isVis[i][j] = 1;
 43         
 44         
 45         if(i >= 1 && !isVis[i-1][j] && board[i-1][j] == 'O')
 46         {
 47             dfs(board, i-1, j, height, width, isVis, flag); //
 48         }
 49         if(i < (int)board.size()-1 && !isVis[i+1][j] && board[i+1][j] == 'O')
 50         {
 51             dfs(board, i+1, j, height, width, isVis, flag); //
 52         }
 53         if(j >= 1 && !isVis[i][j-1] && board[i][j-1] == 'O')
 54         {
 55             dfs(board, i, j-1, height, width, isVis, flag); //
 56         }
 57         if(j < (int)board[0].size() && !isVis[i][j+1] && board[i][j+1] == 'O')
 58         {
 59             dfs(board, i, j+1, height, width, isVis, flag); //
 60         }
 61         
 62         return flag;
 63     }
 64     
 65     void changeBoard(vector<vector<char>> &board, int i, int j) // working but too slow
 66     {
 67         vector<int> queue;
 68         board[i][j] = 'X';
 69         assert(i > 0 && i < board.size()-1 && j > 0 && j < board[0].size()-1);
 70         if(board[i-1][j] == 'O')
 71             changeBoard(board, i-1, j);
 72         if(board[i+1][j] == 'O')
 73             changeBoard(board, i+1, j);
 74         if(board[i][j-1] == 'O')
 75             changeBoard(board, i, j-1);
 76         if(board[i][j+1] == 'O')
 77             changeBoard(board, i, j+1);
 78     }
 79     
 80     void solve(vector<vector<char>> &board) {
 81         // Start typing your C/C++ solution below
 82         // DO NOT write int main() function
 83         int height = (int)board.size();
 84         if(height == 0) return;
 85         int width = (int)board[0].size();
 86         
 87 //        vector<int> temp(width, 0);
 88 //        
 89 //        vector<vector<int>> isVis(height, temp);
 90 //        bool flag = true;
 91 //        
 92 //        for(int i = 0; i < height; i++)
 93 //        {
 94 //            for(int j = 0; j < width; j++)
 95 //            {
 96 //                if(board[i][j] == 'O' && !isVis[i][j])
 97 //                {
 98 //                    flag = true;
 99 //                    if(dfs(board, i, j, height, width, isVis, flag)) // surround regions
100 //                    {
101 //                        changeBoard(board, i, j);  // Find surround region directly may cause runtime error
102 //                    }
103 //                }
104 //            }
105 //        }
106         
107         // Change my strategy to mark unsurrounded regions
108         
109         for(int i = 0; i < height; i++)
110         {
111             dfs2(board, i, 0);
112             dfs2(board, i, width-1);
113         }
114         
115         for(int j = 0; j < width; j++)
116         {
117             dfs2(board, 0, j);
118             dfs2(board, height-1, j);
119         }
120         
121         for(int i = 0; i < height; i++)
122         {
123             for(int j = 0; j < width; j++)
124             {
125                 if(board[i][j] == 'O') board[i][j] = 'X';
126                 if(board[i][j] == 'C') board[i][j] = 'O';
127             }
128         }
129         
130         // print result
131         for(int i = 0; i < height; i++)
132         {
133             for(int j = 0; j < width; j++)
134             {
135                 cout << board[i][j] << ' ';
136             }
137             cout << endl;
138         }
139         
140     }
141     
142 };
143 
144 int main()
145 {
146     vector<vector<char>> board{{'X','X','X','X'},{'X','O','O','X'},{'X','X','O','X'},{'X','O','X','X'}};
147     
148     
149     Solution sln;
150     sln.solve(board);
151     
152     return 0;
153 }
View Code

 此外,提交的程式碼是不能有輸出語句的,否則會報Internal Error。

 BFS也可以做:http://blog.sina.com.cn/s/blog_b9285de20101j1dt.html

相關文章