Number of Distinct Islands - LintCode

原创 2018年04月15日 18:36:13

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.

Notice that:

11
1

and

 1
11

are considered different island shapes, because we do not consider reflection / rotation.

注意事项
The length of each dimension in the given grid does not exceed 50.

样例

Given grid = 
[
[1,1,0,0,0],
[1,1,0,0,0],
[0,0,0,1,1],
[0,0,0,1,1]
]

return 1
Given grid =

[
[1,1,0,1,1],
[1,0,0,0,0],
[0,0,0,0,1],
[1,1,0,1,1]
]

return 3

思路:
类似于求岛屿的个数,但是要存放每个岛屿的位置。对于如何表示可以由位移得到的岛屿,可以令岛屿每个位置都减去其最左和最上的值,得到的数字存入字符串中。可以由位移得到的两个岛屿,其字符串是相同的。

#ifndef C860_H
#define C860_H
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
class Solution {
public:
    /**
    * @param grid: a list of lists of integers
    * @return: return an integer, denote the number of distinct islands
    */
    int numberofDistinctIslands(vector<vector<int>> &grid) {
        // write your code here
        if (grid.empty() || grid[0].empty())
            return 0;
        int rows = grid.size();
        int cols = grid[0].size();
        vector<vector<int>> dic{ { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
                map<int, int> visited;
        set<string> island;
        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                if (grid[i][j] == 1 && visited.find(i*cols + j) == visited.end())
                {
                    vector<pair<int, int>> pos;
                    helper(i, j, grid, dic, pos, visited);
                    //可由平移得到的岛屿其字符串是相同的
                    island.insert(build(pos, cols));
                }
            }
        }
        return island.size();
    }
    //对位置(i,j)判断其是否为岛屿,并找到包含它的最大岛屿,位置是否被访问存放在visited中,岛屿的位置存放在pos中
    void helper(int i, int j, vector<vector<int>> &grid, vector<vector<int>> &dic,vector<pair<int,int>> &pos, map<int, int> &visited)
    {
        int rows = grid.size();
        int cols = grid[0].size();
        if (i < 0 || i >= rows || j < 0 || j >= cols)
            return ;
        if (grid[i][j] == 0 || visited[i*cols + j] == 1)
            return ;
        visited[i*cols + j] = 1;
        pos.push_back(make_pair(i,j));
        for (auto c : dic)
        {
            int x = i + c[0];
            int y = j + c[1];
            helper(x, y, grid, dic, pos, visited);
        }
    }
    //对于岛屿,找到其最左left和最上的位置top,用每个位置i,j分别减去left和top
    //得到的i-left和j-top存入字符串
    string build(vector<pair<int,int>> pos, int cols)
    {
        int left = INT_MAX, top = INT_MAX;
        for (auto c : pos)
        {
            if (c.first < left)
                left = c.first;
            if (c.second < top)
                top = c.second;
        }
        sort(pos.begin(), pos.end());
        string str;
        for (auto c : pos)
        {
            str += to_string(c.first - left) + to_string(c.second-top);
        }
        return str;
    }
};
#endif
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaohengchuan/article/details/79951694

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
  • 684

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
  • 1452

[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
  • 2081

[Lintcode]Number of Islands

Given a boolean 2D matrix, find the number of islands. Example Given graph: [ [1, 1, 0, 0...
  • jc69186918
  • jc69186918
  • 2016-02-23 05:21:58
  • 168

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
  • 1738

[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
  • 165

【LeetCode】Number of Islands 解题报告

【题目】 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An isl...
  • ljiabin
  • ljiabin
  • 2015-04-10 10:16:24
  • 17785

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
  • 392

leetcode 200 : Number of Islands

leetcode 200 : Number of Islands java c++ python
  • xudli
  • xudli
  • 2015-05-22 07:40:37
  • 7668

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
  • 503
收藏助手
不良信息举报
您举报文章:Number of Distinct Islands - LintCode
举报原因:
原因补充:

(最多只允许输入30个字)