广度优先搜索(Nunber of Islands、Surrounded Region)

广度优先:伪代码

void BFS(int x)
    visited[x]=true;
    Queue.push(x);
    while(!Queue.empth())
        v=Queue.pop();
        for(v的每个邻接点w)
            if(!visited[w])
                visited[w]=true;
                Queue.push(w);

下面给出leetcode 上的一道题目,leetcode-Number of Islands
Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

题目的大意是1代表陆地,0代表海洋,问矩阵中有几片岛屿,1连在一起就是岛屿,可以通过上下左右四个方向相连。这道题目可以通过bfs的方法在有陆地的地方探测边界,也就是FloodFill算法。
class Solution {


public:


    int numIslands(vector<vector<char>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
        if(n==0||m==0){
            return 0;
        }

        int ans=0;
        queue<int> qx,qy;
        int fx[4]={1,-1,0,0};
        int fy[4]={0,0,1,-1};


        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(grid[i][j]=='1'){
                    grid[i][j]='0';
        qx.push(i);
        qy.push(j);
        while(!qx.empty()){
            int px=qx.front();
            int py=qy.front();
            for(int k=0;k<4;k++){
                int newx=px+fx[k];
                int newy=py+fy[k];
                if(grid[newx][newy]=='1'&& newx>=0 && newx<n && newy>=0 && newy<m){
                    grid[newx][newy]='0';
                    qx.push(newx);
                    qy.push(newy);

                }
            }
            qx.pop();
            qy.pop();
            }
        ans++;
        }
        }
        }
                return ans;
        }
};

这份代码在我看来是正确的,可是在leetcode的oj上总是RE,我暂时不知道是哪里出问题了,不过我在线下测试过我写的这个bfs算法,发现没有错误。


发现错误了!if(语句)这里出错了,应该先判断下标的合法性,再可以又下标取数组内容。不可以反着来。所以更新后的代码

class Solution {


public:


    int numIslands(vector<vector<char>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
        if(n==0||m==0){
            return 0;
        }

        int ans=0;
        queue<int> qx,qy;
        int fx[4]={1,-1,0,0};
        int fy[4]={0,0,1,-1};


        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(grid[i][j]=='1'){
                    grid[i][j]='0';
        qx.push(i);
        qy.push(j);
        while(!qx.empty()){
            int px=qx.front();
            int py=qy.front();
            for(int k=0;k<4;k++){
                int newx=px+fx[k];
                int newy=py+fy[k];
                if(newx>=0 && newx<n && newy>=0 && newy<m&&grid[newx][newy]=='1'){
                    grid[newx][newy]='0';
                    qx.push(newx);
                    qy.push(newy);

                }
            }
            qx.pop();
            qy.pop();
            }
        ans++;
        }
        }
        }
                return ans;
        }
};

在leetcode上有一道题目和这道题目很相似,就是Surrounded Region

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

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

这道题目的大致和上一道题岛的数量形式差不多,不过比Number of Islands难度高两个等级,都是用FloodFIll算法,具体点是用bfs算法遍历,不过要多声明一套队列tmpx,tmpy,用于储存所有挖到的陆地,等下有用,还要再加多一个v[][]数组用于标记那些地方是访问过,防止某些小岛被还回去之后,又再次被挖,造成时间上的极大浪费,比如一个矩阵上全为O的情况。 访问过的陆地就把它变为海洋,然后当遍历到这片陆地有与矩阵四条边中任意一条有交接时,标志isHuan为true,最后判断如果isHuan为true,就将队列中的陆地吐出来补回去。

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        int n=board.size();
        if(n==0)return;
        int m=board[0].size();

        queue<int> qx,qy;
        int fx[4]={1,-1,0,0};
        int fy[4]={0,0,1,-1};
        bool isHuan=false;

        bool v[1000][1000];

        for(int i=0;i<1000;i++)
                        for(int j=0;j<1000;j++)
                            v[i][j]=false;

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(board[i][j]=='O'&&!v[i][j]){
                    isHuan=false;
                    queue<int> tmpx,tmpy;
                    board[i][j]='X';
                    v[i][j]=true;
                    qx.push(i);
                    qy.push(j);
                    tmpx.push(i);
                    tmpy.push(j);
                    while(!qx.empty()){
                        int px=qx.front();
                        int py=qy.front();
                        for(int k=0;k<4;k++){
                            int newx=px+fx[k];
                            int newy=py+fy[k];
                            if(newx>=0&&newx<n&&newy>=0&&newy<m){
                                if(board[newx][newy]=='O'&&!v[newx][newy]){
                                    board[newx][newy]='X';
                                    v[newx][newy]=true;
                                    qx.push(newx);
                                    qy.push(newy);
                                    tmpx.push(newx);
                                    tmpy.push(newy);
                                }
                            }else{
                                isHuan=true;
                            }
                        }
                        qx.pop();
                        qy.pop();
                    }
                    if(isHuan){
                        while(!tmpx.empty()){
                            int tx=tmpx.front();
                            int ty=tmpy.front();
                            board[tx][ty]='O';
                            tmpx.pop();
                            tmpy.pop();
                        }
                    }

                }

            }
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值