LWC 53:694. Number of Distinct Islands

868人阅读 评论(0) 收藏 举报
分类:

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

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

查看评论

Number类

-
  • 1970年01月01日 08:00

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) con...
  • zjucor
  • zjucor
  • 2017-10-08 16:01:01
  • 714

200. Number of Islands Leetcode Python

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is sur...
  • hyperbolechi
  • hyperbolechi
  • 2015-04-15 19:14:15
  • 1745

[leetcode] 305. Number of Islands II 解题报告

题目链接: https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns i...
  • qq508618087
  • qq508618087
  • 2016-03-26 06:50:25
  • 2084

LeetCode 305. Number of Islands II(小岛)

原题网址:https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns is...
  • jmspan
  • jmspan
  • 2016-04-19 13:13:25
  • 1457

[Leetcode] 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...
  • magicbean2
  • magicbean2
  • 2018-02-03 20:55:14
  • 170

Number of Islands BFS

Number of Islands   Given a 2d grid map of '1's (land) and '0's (water), count the number of isl...
  • u012605629
  • u012605629
  • 2015-06-01 17:46:03
  • 393

leetcode 694. Number of Distinct Islands(BFS)

题目:https://leetcode.com/contest/leetcode-weekly-contest-53/problems/number-of-distinct-islands/ 题意:...
  • huatian5
  • huatian5
  • 2017-10-08 11:52:17
  • 525

leetcode -- Number of Islands -- DFS&BFS重点题

https://leetcode.com/problems/number-of-islands/要加上一个mask matrix 记录是否被访问过。思路见http://yucoding.blogspo...
  • xyqzki
  • xyqzki
  • 2015-12-16 16:47:02
  • 504

leetcode 200 : Number of Islands

leetcode 200 : Number of Islands java c++ python
  • xudli
  • xudli
  • 2015-05-22 07:40:37
  • 7683
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 25万+
    积分: 6106
    排名: 5140
    博客专栏
    最新评论