1. 程式人生 > >LintCode 804: Number of Distinct Islands II (BFS/DFS 難題)

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];
    }
};