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();
    }

 

posted @ 2019-10-08 01:08  妖域大都督  阅读(168)  评论(0编辑  收藏  举报