LintCode 804: Number of Distinct Islands II (BFS/DFS 难题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/roufoo/article/details/85461706
  1. Number of Distinct Islands II
    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 they have the same shape, or have the same shape after rotation (90, 180, or 270 degrees only) or reflection (left/right direction or up/down direction).

Example
Example 1:

11000
10000
00001
00011
Given the above grid map, return 1.

Notice that:

11
1
and

1
11
are considered same island shapes. Because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.

Example 2:

11100
10001
01001
01110
Given the above grid map, return 2.

Here are the two distinct islands:

111
1
and

1
1
Notice that:

扫描二维码关注公众号,回复: 4724609 查看本文章

111
1
and

1
111
are considered same island shapes. Because if we flip the first array in the up/down direction, then they have the same shapes.

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

思路:
参考了
https://blog.csdn.net/magicbean2/article/details/79282937
首先用DFS或BFS找出各个island,然后用normalize函数将其进行各种平移,旋转(共8种)。用set取normalized的版本。最后返回islands.size()。
注意:

  1. normalize()的版本里面,对8个版本内部都要进行排序,然后减去offset。最后对8个版本还要排序,取第一个作为normalize的版本。
  2. 可以对2维vector调用sort()函数,其效果应该是按每个1维vector的首元素排序。
    代码如下:
struct Node {
    int x;
    int y;
    Node(int row, int col) : x(row), y(col) {}
    bool operator < (const Node & node) const {
        return (x < node.x) || ((x == node.x) && (y < node.y));
    }
};

class Solution {
public:
    /**
     * @param grid: the 2D grid
     * @return: the number of distinct islands
     */
    int numDistinctIslands2(vector<vector<int>> &grid) {
        nRow = grid.size();
        nCol = grid[0].size();
        set<vector<Node>> islands;
        
        for (int r = 0; r < nRow; ++r) {
            for (int c = 0; c < nCol; ++c) {
                if (grid[r][c] == 1) {
                    vector<Node> island;
                    Node src = Node(r, c);  //Node src(r, c) is also OK
                    Node dest(src);
                    dfs(grid, src, dest, island);
                    islands.insert(normalize(island));
                }
            }
        }
        return islands.size();
    }
    
private:
    int nRow;
    int nCol;
    void dfs(vector<vector<int>> &grid, Node &src, Node &dest, vector<Node> &island) {
        //left, down, right, up
        vector<int> dirX = {-1, 0, 1, 0}; //row
        vector<int> dirY = {0, -1, 0, 1}; //col
        int x = dest.x;
        int y = dest.y;
        if ((x >= 0) && (x < nRow) && (y >= 0) && (y < nCol) && (grid[x][y] > 0)) {
            grid[x][y] = 0;
            island.push_back(Node(x,y));
            for (int i = 0; i < 4; ++i) {
                Node neighbor = Node(dest.x + dirX[i], dest.y + dirY[i]);
                dfs(grid, src, neighbor, island);
            }
        }
    }
    
    vector<Node> normalize(vector<Node> &island) {
        vector<vector<Node>> result(8, vector<Node>());
        for (auto i : island) {
            int x = i.x, y = i.y;
            result[0].push_back(Node(x, y));
            result[1].push_back(Node(x, -y));
            result[2].push_back(Node(-x, y));
            result[3].push_back(Node(-x, -y));
            result[4].push_back(Node(y, x));
            result[5].push_back(Node(y, -x));
            result[6].push_back(Node(-y, x));
            result[7].push_back(Node(-y, -x));
        }
        
        for (int i = 0; i < 8; ++i) {
            sort(result[i].begin(), result[i].end());
            int len = result[i].size();
            int offsetCol = result[i][0].x;
            int offsetRow = result[i][0].y;
            for (int j = 0; j < len; ++j) {
                result[i][j].x -= offsetCol;
                result[i][j].y -= offsetRow;
            }
        }
        sort(result.begin(), result.end());
        return result[0];
    }
};

猜你喜欢

转载自blog.csdn.net/roufoo/article/details/85461706
今日推荐