X has not been around to find the point, the rest is surrounded by point, these O changed to X.
class Solution { public: /* * @param board: board a 2D board containing 'X' and 'O' * @return: nothing */ void surroundedRegions(vector<vector<char>> &board) { // write your code here if(board.empty() || board[0].empty()) return; int n = board.size(), m = board[0].size(); vector<vector<int>> mark(n, vector<int>(m, 0)); //记录每个格子的No Vector < BOOL > Surrounded (n-m * + . 1 , to true ); // record number No whether this area is surrounded by X int No = 0 ; // record a link area number for ( int I = 0 ; I <n-; ++ I ) { for ( int J = 0 ; J <m; J ++ ) { IF (Board [I] [J] == ' O ' && Mark [I] [J] == 0 ) { No ++ ; BFS ( board, mark, surrounded, No, i, j); Mark [ I] [J] = No; } } } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(board[i][j] == 'O' && surrounded[mark[i][j]]) //被X环绕 board[i][j] = 'X'; } } } void bfs(vector<vector<char>> &board, vector<vector<int>>& mark, vector<bool>& surrounded, int No, int x, int y ){ int row = board.size(),col = board[0].size(); int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; queue<int> qx; queue<int> qy; qx.push(x); qy.push (Y); //Mark [X] [Y] = No; the while (! qx.empty ()) { int OX = qx.front (); int Oy = qy.front (); qx.pop (); qy.pop (); IF (OX == 0 || == row- OX . 1 || Oy == 0 || == COL- Oy . 1 ) // encountered boundary description is not surrounded by X surrounded [no] = to false ; // the No mark this area to false for ( int I = 0 ; I < . 4 ; I ++ ) { int nx = ox + dx[i]; int ny = oy + dy[i]; if(nx>=0 && nx<row && ny>=0 && ny<col && mark[nx][ny]==0 && board[nx][ny]=='O'){ mark[nx][ny] = No; qx.push(nx); qy.push(ny); } } } } };
Meaning of the questions:
INF: empty room; -1: Wall; 0: Door
Q: How much room is empty from any length to the nearest door.
You can not reach the place filled INF
Ideas:
All rooms [i] [j] = 0 into the node queue and then request the BFS, the shortest path is determined to the nearest door empty room.
BFS can be used to find the shortest edge length 1 of FIG.
class Solution { public: /** * @param rooms: m x n 2D grid * @return: nothing */ static const int inf = 2147483647; int n, m; void wallsAndGates(vector<vector<int>> &rooms) { // write your code here if(rooms.empty() || rooms[0].empty()) return; n = rooms.size(), m = rooms[0].size(); int dx[4] = {-1, 0 , . 1 , 0 }; int Dy [ . 4 ] = { 0 , - . 1 , 0 , . 1 }; Queue < int > QX; Queue < int > QY; // multi-source shortest path end points: all gate queues for ( int I = 0 ; I <n-; I ++ ) { for ( int J = 0 ; J <m; J ++ ) { IF (Rooms [I] [J] == 0 ) { qx.push(i); qy.push(j); } } } while(!qx.empty()){ int ox = qx.front(); int oy = qy.front(); qx.pop(); qy.pop(); for(int i=0; i<4; i++){ int nx = ox + dx[i]; int ny = oy + dy[i]; if(nx>=0 && nx<n && ny>=0 && ny<m && rooms[nx][ny]==inf){ qx.push(nx); qy.push(ny); rooms[nx][ny] = rooms[ox][oy] + 1; } } } } };
Note: dfs function in the x, l, str, not add & will complain:
error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
Reference error Link: https://stackoverflow.com/questions/8293426/error-invalid-initialization-of-non-const-reference-of-type-int-from-an-rval
It is simply because the incoming x, l, str are specific values, so you can not add &
class Solution { public : / * * * @param digits: A Digital String * @return: All posible to Letter Combinations * / Vector < String > ANS; // current number of layers of x; total number of layers l, the intermediate state STR, void DFS ( int X, int L, String STR, String digits, Vector < String > & Phone) { // exit condition IF (X == L) { ans.push_back (STR); return ; } int NUM = digits [X] -'0'; for(int i=0; i<phone[num].size(); i++){ //数字num有多少种拓展情况 dfs(x+1, l, str+phone[num][i], digits, phone); } } vector<string> letterCombinations(string &digits) { // write your code here vector<string> phone = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; if(digits.empty()) return ans; dfs(0, digits.size(), "", digits, phone); return ans; } };