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

  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:

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];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值