Number of Distinct Islands II in C++


Suppose we have a non-empty 2D binary array called grid, here an island is a group of 1's (representing land) connected 4-directionally. We can also assume all four edges of the grid are surrounded by water.

We have to 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 of 90, 180, or 270 degrees only or reflection of left/right direction or up/down direction.

So, if the input is like,

11000
10000
00001
00011

then the output will be 1

To solve this, we will follow these steps −

  • Define one map m

  • Define a function dfs(), this will take i, j, grid, idx,

  • if i and j are in range of the grid and grid[i,j] is 0 then −

    • return

  • grid[i, j] := 0

  • insert { i, j } at the end of m[idx]

  • dfs(i + 1, j, grid, idx)

  • dfs(i - 1, j, grid, idx)

  • dfs(i, j - 1, grid, idx)

  • dfs(i, j + 1, grid, idx)

  • Define one function norm(), this will take an array v

    • Define one 2D array s of pairs with 8 rows

    • for initialize i := 0, when i < size of v, update (increase i by 1), do −

      • x := v[i].first

      • y := v[i].second

      • insert { x, y } at the end of s[0]

      • insert { x, -y } at the end of s[1]

      • insert { - x, y } at the end of s[2]

      • insert { - x, -y } at the end of s[3]

      • insert { y, x } at the end of s[4]

      • insert { y, -x } at the end of s[5]

      • insert { - y, x } at the end of s[6]

      • insert { - y, -x } at the end of s[7]

    • for initialize i := 0, when i < size of s, update (increase i by 1), do −

      • sort the array s[i]

    • for initialize i := 0, when i < size of s, update (increase i by 1), do −

      • for initialize j := 1, when j < size of v, update (increase j by 1), do −

        • s[i, j].first := s[i, j].first - s[i, 0].first

        • s[i, j].second := s[i, j].second - s[i, 0].second

      • s[i, 0].first := 0

      • s[i, 0].second := 0

    • sort the array s

    • return s[0]

  • From the main method do the following −

  • Define one set pts

  • cnt := 1

  • for initialize i := 0, when i < size of grid, update (increase i by 1), do −

    • for initialize j := 0, when j < size of grid[0], update (increase j by 1), do &miuns;

      • if grid[i, j] is same as 1, then −

        • (increase cnt by 1)

        • dfs(i, j, grid, cnt)

        • insert norm(m[cnt]) into pts

  • return size of pts

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   map < int, vector < pair <int, int> > > m;
   void dfs(int i, int j, vector < vector <int> >& grid, int idx){
      if (i >= grid.size() || j >= grid[0].size() || i < 0 || !grid[i][j])
         return;
      grid[i][j] = 0;
      m[idx].push_back({ i, j });
      dfs(i + 1, j, grid, idx);
      dfs(i - 1, j, grid, idx);
      dfs(i, j - 1, grid, idx);
      dfs(i, j + 1, grid, idx);
   }
   vector < pair <int, int> > norm(vector < pair < int, int > > v){
      vector<vector<pair<int, int> > > s(8);
      for (int i = 0; i < v.size(); i++) {
         int x = v[i].first;
         int y = v[i].second;
         s[0].push_back({ x, y });
         s[1].push_back({ x, -y });
         s[2].push_back({ -x, y });
         s[3].push_back({ -x, -y });
         s[4].push_back({ y, x });
         s[5].push_back({ y, -x });
         s[6].push_back({ -y, x });
         s[7].push_back({ -y, -x });
      }
      for (int i = 0; i < s.size(); i++) {
         sort(s[i].begin(), s[i].end());
      }
      for (int i = 0; i < s.size(); i++) {
         for (int j = 1; j < v.size(); j++) {
            s[i][j].first = s[i][j].first - s[i][0].first;
            s[i][j].second = s[i][j].second - s[i][0].second;
         }
         s[i][0].first = 0;
         s[i][0].second = 0;
      }
      sort(s.begin(), s.end());
      return s[0];
   }
   int numDistinctIslands2(vector<vector<int>>& grid) {
      set<vector<pair<int, int> > > pts;
      int cnt = 1;
      for (int i = 0; i < grid.size(); i++) {
         for (int j = 0; j < grid[0].size(); j++) {
            if (grid[i][j] == 1) {
               cnt++;
               dfs(i, j, grid, cnt);
               pts.insert(norm(m[cnt]));
            }
         }
      }
      return pts.size();
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,1,0,0,0},{1,0,0,0,0},{0,0,0,0,1},{0,0,0,1,1}};
   cout << (ob.numDistinctIslands2(v));
}

Input

{{1,1,0,0,0},{1,0,0,0,0},{0,0,0,0,1},{0,0,0,1,1}}

Output

1

Updated on: 11-Jul-2020

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements