leetcode-surrounded regions

Source: Internet
Author: User

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

A region was captured by flipping all ‘O‘ s into ‘X‘ s-surrounded region.

For example,

x x x xx o o xx x o xx o x x

After running your function, the board should is:

x x x xx x x xx x x xx O x x


Problem Solving Ideas:
Re-open a space: store information for each point. The structure of each node's struct is as follows:

struct Node{public:~node () {}node (int x, int y, char c): X (x), Y (y), C (c) {yes = false;} node (int x, int y, char C, bool a): X (x), Y (y), C (c), yes (a) {}bool operator== (const node &t) {return this->x = = t.x& Amp;&this->y = = T.y;} int X;int Y;bool yes;             Clor in breadth-first search, with an initial value of false, which becomes Truechar C after traversal;};

1/Initialize each node, the value of C is the same as the value in board, and each value in board becomes ' X '

2/Search for ' O ' from every four sides Yes = = False

3/along the search to ' o ' in four directions (breadth-first search), using the queue to store the c== ' O ' &&yes==false node

Note: Each time you extend to the perimeter, Yes=true is put before entering the queue, or the point will be added to the queue during the subsequent search, and when the matrix is larger, the program time is increased by thousand times.

4/points that are true according to the node matrix are traversed points, and the values in the same position in the board are set to ' O '

voidSolve (vector<vector<Char>>&Board) {        Const inth =board.size (); if(h==0)return ; Const intL = board[0].size (); Vector<vector<node>>T;  for(intI=0; i) {//Initialize node matrix T vector<node>temp;  for(intj=0; j<l;j++) {node T_node (i,j,board[i][j],false);                Temp.push_back (T_node); BOARD[I][J]='X';        The board matrix is all set to ' X ',} t.push_back (temp); }         for(intI=0; i<l;i++){            if(t[0][i].c=='O'&&t[0][i].yes==false){                Set(t,t[0][i],h,l); }        }         for(intI=0; i<l;i++){            if(t[h-1][i].c=='O'&&t[h-1][i].yes==false){                Set(t,t[h-1][i],h,l); }        }         for(intI=0; i){            if(t[i][0].c=='O'&&t[i][0].yes==false){                Set(t,t[i][0],h,l); }        }         for(intI=0; i){            if(t[i][l-1].c=='O'&&t[i][l-1].yes==false){                Set(t,t[i][l-1],h,l); }        }           for(intI=0; i{//depending on if yes is traversed in T, determine if the value in board is ' O ' for(intj=0; j<l;j++){                if(T[i][j].yes) {Board[i][j]='O'; }            }        }        return ; }    void Set(Vector<vector<node>>&d, Node A,Const intHConst intl) {Queue<node>Q;        Q.push (a); D[a.x][a.y].yes=true; D[A.X][A.Y].C='X';  while(!Q.empty ()) {Node T=Q.front ();            Q.pop (); if(T.x +1< H&&d[t.x +1][t.y].yes = =false&& D[t.x +1][T.Y].C = ='O') {Q.push (d[t.x+1][t.y]); D[t.x+1][t.y].yes =true; When entering the queue, change the value of Yes to true, and the next time it will no longer join the queue
D[t.x+1][T.Y].C ='X';
}
        if(T.x-1>=0&& D[t.x-1][t.y].yes = =false&& D[t.x-1][T.Y].C = ='O')
{
Q.push (D[t.x-1][t.y]);
D[t.x-1][t.y].yes =true;
D[t.x-1][T.Y].C ='X';
}
        if(T.y +1< L&&d[t.x][t.y +1].yes = =false&& D[t.x][t.y +1].C = ='O')
{
Q.push (D[t.x][t.y+1]);
D[t.x][t.y+1].yes =true;
D[t.x][t.y+1].C ='X';
}
        if(T.y-1>=0&& D[t.x][t.y-1].yes = =false&& D[t.x][t.y-1].C = ='O')
{
Q.push (D[t.x][t.y-1]);
D[t.x][t.y-1].yes =true;
D[t.x][t.y-1].C ='X';
}
}
     return;
}







leetcode-surrounded regions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.