zoukankan      html  css  js  c++  java
  • leetcode

    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


    解题思路:
    重新开辟一个空间: 存放每个点的信息。每个node的struct的结构如下:

    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&&this->y == t.y;
    		}
    		int x;
    		int y;
    		bool yes;             // 广度优先搜索中的 clor ,初始值为 false ,当遍历之后变为 true
    		char c;
    	};
    

    1/初始化每个node , c的值与board中的值相同,board中每个值变成‘X’

    2/从每四条边 搜索 yes == false 的 ‘O’

    3/ 沿着搜索到的‘O’ 向四个方向 扩展(广度优先搜索) , 利用queue存储周围的c==‘O’ &&yes==false 的node

    注: 每次向四周扩展时,在进入队列之前把yes=true; // 否则在后面搜索过程中还会把该点加入队列,当矩阵较大时程序用时增加上千倍  

    4/ 根据node矩阵中 为true的点都是被遍历过的点,将board中相同位置的值设为'O'

    void solve(vector<vector<char>>& board) {
            const int h = board.size();
            if(h==0) return ;
            const int l = board[0].size();
            vector<vector<node>> t;     
            for(int i=0;i<h;i++){                 // 初始化node矩阵t
                vector<node> temp;
                for(int j=0;j<l;j++){
                    node t_node(i,j,board[i][j],false);
                    temp.push_back(t_node);
                    board[i][j] = 'X';           // board矩阵全部置为‘X’,
                }
                t.push_back(temp);
            }
            for(int i=0;i<l;i++){
                if(t[0][i].c=='O'&&t[0][i].yes==false){
                    set(t,t[0][i],h,l);
                }
            }
            for(int i=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(int i=0;i<h;i++){
                if(t[i][0].c=='O'&&t[i][0].yes==false){
                    set(t,t[i][0],h,l);
                }
            }
            for(int i=0;i<h;i++){
                if(t[i][l-1].c=='O'&&t[i][l-1].yes==false){
                    set(t,t[i][l-1],h,l);
                }
            }  
            for(int i=0;i<h;i++){                      // 根据t中yes是否被遍历过,判断board中值是否为‘O’
                for(int j=0;j<l;j++){
                    if(t[i][j].yes){
                        board[i][j]='O';
                    }
                }
            }
            return ;
        }
        void set(vector<vector<node>>&d, node a, const int h, const int l){
            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;           //进入队列时,把 yes的值改为true ,下次遍历时将不会再加入队列
              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;
      }







  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/5547111.html
Copyright © 2011-2022 走看看