zoukankan      html  css  js  c++  java
  • [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

    思路:

    leetcode的新题,题目思路很简单,DFS或者BFS,而且还不用回溯,直接判断,难度不大。因为自己用弱项BFS写出来了,贴出来备忘。

    题解:

    class Solution {
    public:
        int dir[4][2] = {{-1,0}, {0,-1}, {1,0},{0,1}};
        void dfs(vector<vector<char> > &grid, int x, int y) {
            grid[x][y] = '0';
            for(int k=0;k<4;k++) {
                int nx = x+dir[k][0];
                int ny = y+dir[k][1];
                if(nx>=0 && nx<grid.size() && ny>=0 && ny<grid[0].size())
                    if(grid[nx][ny]=='1')
                        dfs(grid, nx, ny);
            }
        }
        int numIslands(vector<vector<char> > &grid) {
            int m = grid.size();
            if(m==0)
                return 0;
            int n = grid[0].size();
            int res = 0;
            for(int i=0;i<m;i++)
                for(int j=0;j<n;j++)
                    if(grid[i][j]=='1') {
                        dfs(grid, i, j);
                        res++;
                    }
            return res;
        }
    };
    DFS
    class Solution {
    public:
        struct point {
            point(int xx=0, int yy=0): x(xx), y(yy) {}
            int x;
            int y;
        };
        int dir[4][2] = {{0,-1}, {-1,0}, {0,1}, {1,0}};
        void BFS(vector<vector<char> > &grid, int x, int y) {
            queue<point> q;
            int curx, cury;
            q.push(point(x,y));
            while(!q.empty()) {
                curx = q.front().x;
                cury = q.front().y;
                q.pop();
                grid[curx][cury] = '0';
                for(int k=0;k<4;k++) {
                    int nx = curx+dir[k][0];
                    int ny = cury+dir[k][1];
                    if(nx>=0 && nx<grid.size() && ny>=0 && ny<grid[0].size())
                        if(grid[nx][ny]=='1') {
                            q.push(point(nx,ny));
                            grid[nx][ny] = '0';
                        }
                }
            }
        }
        int numIslands(vector<vector<char> > &grid) {
            int m = grid.size();
            if(m==0)
                return 0;
            int n = grid[0].size();
            int res = 0;
            for(int i=0;i<m;i++)
                for(int j=0;j<n;j++) {
                    if(grid[i][j] == '1') {
                        ++res;
                        BFS(grid, i,j);
                    }
                }
            return res;
        }
    };
    BFS
  • 相关阅读:
    软件开发和机械制造的核心差别
    不做纯技术导向的程序员与中国特色的社会主义
    从代码里你可以看到什么?
    多少钱才可让人重拾理想
    项目经理一定比码农好么?
    技术还是管理?
    评李彦宏先生的内部邮件
    组织行为学对项目管理的意义(1)
    组织行为学对项目管理的意义:动机理论
    Silverlight与ashx通讯序列化DateTime时需注意的细节
  • 原文地址:https://www.cnblogs.com/jiasaidongqi/p/4420914.html
Copyright © 2011-2022 走看看