zoukankan      html  css  js  c++  java
  • 694. Number of Distinct Islands

    问题描述:

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

    Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.

    Example 1:

    11000
    11000
    00011
    00011
    

    Given the above grid map, return 1.

    Example 2:

    11011
    10000
    00001
    11011

    Given the above grid map, return 3.

    Notice that:

    11
    1
    

    and

     1
    11
    

    are considered different island shapes, because we do not consider reflection / rotation.

    Note: The length of each dimension in the given grid does not exceed 50.

    解题思路:

    可以通过平移进行转换的岛被认为是相似的岛。

    确定岛的形状可以用BFS/DFS(最近爱用BFS,但是BFS过于耗时。。)

    这道题的重点是如何生成一个岛的hashcode:

      - 观察题目可以看到,岛是由坐标组成。

      - C++中的container 并不具备有hash function的能力。

      - 我们可以将左上点挪至原点,并对其他的点做相同的平移: x- deltaX, y - deltaY

      - 用字符串记录路径并且作为唯一标识

    代码:

    class Solution {
    public:
        int numDistinctIslands(vector<vector<int>>& grid) {
            unordered_set<string> paths;
            for (int i=0; i<grid.size(); i++) {
                for (int j=0; j<grid.front().size(); j++) {
                    if(grid[i][j] == 1){
                        paths.insert(dfs(grid, i, j, i, j));
                    }
                }
            }
            return paths.size();
        }
    private:
        string dfs(vector<vector<int>> &grid, int i, int j, int deltaI, int deltaJ){
            grid[i][j] = 2;
            int m = grid.size();
            int n = grid[0].size();
            string path = to_string(i-deltaI) +"," + to_string(j-deltaJ);
            if(i-1 > -1 && grid[i-1][j] == 1){
                path.push_back('_');
                path.append(dfs(grid, i-1, j, deltaI, deltaJ));
            }
            if(j+1 < n && grid[i][j+1] == 1){
                path.push_back('_');
                path.append(dfs(grid, i, j+1, deltaI, deltaJ));
            }
            if(i+1 < m && grid[i+1][j] == 1){
                path.push_back('_');
                path.append(dfs(grid, i+1, j, deltaI, deltaJ));
            }
            if(j-1 > -1 && grid[i][j-1] == 1){
                path.push_back('_');
                path.append(dfs(grid, i, j-1, deltaI, deltaJ));
            }
            return path;
        }
    };

    用lambda表达式会更快一些:

    参考Tiejun的解法

    int numDistinctIslands(vector<vector<int>>& grid) {
            function<bool(int, int, int, int, string&)> dfs = 
                [&](int x, int y, int a, int b, string& path) -> bool {
                if (x < 0 || y < 0 || x >= grid.size() || y >= grid.front().size())
                    return false;
                if (grid[x][y] != 1) return false;
                grid[x][y] = 2;
                path.append(to_string(a));
                path.append(to_string(b));
                path.append(",");
                dfs(x + 1, y, a+1, b, path);
                dfs(x, y + 1, a, b+1, path);
                dfs(x - 1, y, a-1, b, path);
                dfs(x, y - 1, a, b-1, path);
                return true;
            };   
            
            unordered_set<string> paths;
            string path;
            for (int i=0; i<grid.size(); i++) {
                for (int j=0; j<grid.front().size(); j++) {
                    if (dfs(i, j, 0,0, path)) {
                        paths.insert(path);
                        path.clear(); 
                    }
                }
            }
            return paths.size();
        }
  • 相关阅读:
    Neo4j ETL工具快速上手:简化从关系数据库到图数据库的数据迁移
    关于 Neo4j 属性个数的限制
    大数据、人工智能、知识图谱业务蓝图、技术架构-笔记
    数据库设计——评论回复功能
    搜索引擎和知识图谱那些事
    知识图谱在企业中的典型应用和注意事项
    这是一份通俗易懂的知识图谱技术与应用指南
    Qt实用技巧:使用QTableView、QSqlTableMode与QSqlDatabase对数据库数据进行操作
    Qt实用技巧:界面切换使用Dialog全屏切换
    pushbutton成为可点击的图标(实现全透明,不论点击与否都只显示Icon)(也就是一个万能控件)
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/11633242.html
Copyright © 2011-2022 走看看