LWC 53:694. Number of Distinct Islands

LWC 53:694. Number of Distinct Islands

传送门:694. Number of Distinct Islands

Problem:

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:
1 1
1
and
1
1 1
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.

思路:
首先dfs求出每个区域,并记录每个区域的起点,接着根据这些起点再dfs一次,判断四个方向上的长度是否相同。比如:

0 1 0 0 0 1 0
1 1 1 0 1 1 1
0 1 0 0 0 1 0

可以记录每个位置上四个方向的延伸长度,如果每个位置的上的四个值均相同,则说明两个区域重复。

实际上超时,可以采用HASH的思想,在第一次DFS时,就把四个方向的信息取回来,最终存一个起点的HASH值即可。

代码如下:

    int[][] dir = {{1, 0},{0, 1},{-1, 0},{0, -1}};
    int[][] dp = new int[50 * 50 + 16][5]; // four direction

    boolean[][] vis = new boolean[52][52];
    int n, m;

    Set<Integer> set = new HashSet<>();
    public int numDistinctIslands(int[][] grid) {
        n = grid.length;
        if (n == 0) return 0;
        m = grid[0].length;
        if (m == 0) return 0;
        vis = new boolean[52][52];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1) {
                    if (!vis[i][j]) {
                        si = i;
                        sj = j;
                        dfs(grid, i, j);
                        set.add(dp[i * m + j][4]);
                    }
                }
            }
        }

        return set.size();
    }

    boolean check(int i, int j) {
        return i >= 0 && i < n && j >= 0 && j < m;
    }

    int si, sj;
    public int[] dfs(int[][] grid, int x, int y) {
        vis[x][y] = true;

        for (int k = 0; k < 4; ++k) {
            int[] d = dir[k];
            int nx = d[0] + x;
            int ny = d[1] + y;
            if (check(nx, ny) && !vis[nx][ny] && grid[nx][ny] == 1) {
                dp[x * m + y][k] = 1 + dfs(grid, nx, ny)[k];
                dp[x * m + y][4] += (dp[nx * m + ny][4] + 1) * (k + 1);
            }
        }

        for (int k = 0; k < 4; ++k) {
            dp[x * m + y][4] += (((x - si + 1) * m + (y - sj + 1)) * 177) *  (dp[x * m + y][k] + 1) * (k + 1);
        }

        return dp[x * m + y];
    }

上述代码采用数值的做法,当然,你也可以直接遍历一条链,以字符串的形式存储遍历的顺序,理解起来更直观,代码如下:

    int[][] dir = {{1, 0},{0, 1},{-1, 0},{0, -1}};
    char[]  c   = {'D','R','U','L'};
    int n, m;

    boolean[][] vis;

    public int numDistinctIslands(int[][] grid) {
        n = grid.length;
        if (n == 0) return 0;
        m = grid[0].length;
        if (m == 0) return 0;
        vis = new boolean[n][m];
        Set<String> set = new HashSet<>();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1 && !vis[i][j]) {
                    si = i;
                    sj = j;
                    set.add(dfs(grid, i, j));
                }
            }
        }
        return set.size();
    }

    int si, sj;
    public String dfs(int[][] grid, int i, int j) {
        String ans = "" + ((i - si) * m + (j - sj));
        vis[i][j] = true;
        for (int k = 0; k < 4; ++k) {
            int[] d = dir[k];
            int ni = d[0] + i;
            int nj = d[1] + j;
            if (check(ni, nj) && !vis[ni][nj] && grid[ni][nj] == 1) {
                ans += c[k] + dfs(grid, ni, nj);
            }
        }
        return ans;
    }

    boolean check(int i, int j) {
        return i >= 0 && i < n && j >= 0 && j < m;
    }

注意:把相对位置的信息带进来,否则个别案例过不了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值