leetcode【200】【tag DFS】Number of Islands【c++版本,递归与非递归】

问题描述:

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:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

Output: 3

源码:

题目意思就是找一下一共有多少个岛屿呢?注意周边溢出的部分全部算0。

知道小编第一眼看到这个啥想法嘛,这不17年蓝桥杯湖北省赛A组的题目嘛,当年小编我实力不济,好不容易编出来,结果把题目看错了,漏了个条件,最后很遗憾(其实也怪我菜)。

这题一看就是DFS,不断的找新的岛屿,用一个visit矩阵存着当前点是否访问过的标识。展示一下比较cuo的写法:

class Solution {
public:
    int count = 0;
    int x[4]={-1,0,1,0};
    int y[4]={0,1,0,-1};
    void DFS(vector<vector<char>>& grid, int row, int col, vector<vector<bool>>& visit){
        visit[row][col] = true;
        for(int i=0; i<4; i++){
            int xd = row+x[i];
            int yd = col+y[i];
            if(xd>=0 && xd<visit.size() && yd>=0 && yd<visit[0].size() && !visit[xd][yd] && grid[xd][yd]=='1'){
                DFS(grid, xd, yd, visit);
            }
        }
    }
    
    int numIslands(vector<vector<char>>& grid) {
        int m=grid.size();
        if(m==0)    return 0;
        int n = grid[0].size();
        vector<vector<bool>> visit(m, vector<bool>(n, false));
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(!visit[i][j] && grid[i][j]=='1'){
                    count++;
                    DFS(grid, i, j, visit);   
                }
            }
        }
        return count;
    }
};

是不是很冗余,那就写的规整点吧,而且也不用visit了,直接在原数组分配成'2'表示访问过了:

class Solution {
public:
    void DFS(vector<vector<char>>& grid, int row, int col){
        int n=grid.size(), m=grid[0].size();
        grid[row][col] = '2';
        if(row-1>=0 && grid[row-1][col]=='1')      DFS(grid, row-1, col);
        if(row+1< n && grid[row+1][col]=='1')      DFS(grid, row+1, col);
        if(col-1>=0 && grid[row][col-1]=='1')      DFS(grid, row, col-1);
        if(col+1< m && grid[row][col+1]=='1')      DFS(grid, row, col+1);
    }
    
    int numIslands(vector<vector<char>>& grid) {
        int m=grid.size();
        if(m==0)    return 0;
        int n = grid[0].size(), count=0;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(grid[i][j]=='1'){
                    count++;
                    DFS(grid, i, j);   
                }
            }
        }
        return count;
    }
};

看完了递归,就来看看非递归的版本吧,这个也就是把非递归深搜改了一下。

class Solution {
public:  
    int numIslands(vector<vector<char>>& grid) {
        int m=grid.size();
        if(m==0)    return 0;
        int n = grid[0].size(), count=0;
        stack<pair<int, int>> st;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(grid[i][j]=='1'){
                    count++;
                    st.push(make_pair(i, j));
                    while(!st.empty()){
                        auto tmp = st.top();
                        st.pop();
                        int row = tmp.first, col = tmp.second;
                        if(grid[row][col] == '2')       continue;
                        // cout<<row<<" "<<col<<endl;
                        grid[row][col] = '2';
                        if(row-1>=0 && grid[row-1][col]=='1')      st.push(make_pair(row-1, col));
                        if(row+1< m && grid[row+1][col]=='1')      st.push(make_pair(row+1, col));
                        if(col-1>=0 && grid[row][col-1]=='1')      st.push(make_pair(row, col-1));
                        if(col+1< n && grid[row][col+1]=='1')      st.push(make_pair(row, col+1));
                    }
                }
            }
        }
        return count;
    }
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值