Number of Distinct Islands - LintCode

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaohengchuan/article/details/79951694

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

有趣 的 ponds and islands 递归

02-04

今天在教室的黑板上看到了这个题目,觉得挺有趣。rn:rn给出一个矩阵,即通 ponds 或者 islands 的个数, 相连的 0 为一个pond , 相连的 1 为 一个island。 rn(其实跟找迷宫差不多)rn这是我写的递归程序,可是得不到正确答案。 请高手指教我错在哪里了??rnrn数据: 每个矩阵开头一行是说明列行大小的rnrn5 5rn0 1 0 1 0rn0 1 0 1 0rn0 0 0 1 0rn1 1 0 0 1rn0 0 0 1 0rn6 10rn0 0 0 1 0 0 1 0 1 0rn1 1 0 1 1 0 1 0 1 0rn0 0 0 0 0 0 0 0 0 0rn1 1 1 0 1 1 1 0 1 1rn0 0 0 0 1 1 1 0 1 0rn1 1 1 0 0 0 0 0 0 0rn10 10rn0 0 0 0 1 0 0 0 0 0rn1 1 0 1 1 1 1 0 1 1rn0 0 0 0 1 0 0 0 0 0rn1 0 0 0 1 1 1 1 0 1rn0 0 0 0 0 1 0 0 0 0rn1 0 0 0 1 1 1 0 1 1rn0 0 0 0 0 0 0 0 0 0rn1 1 1 1 1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1 0 1 0 1 0 1 0 1 0rnrnrnrn[code=C/C++]rn#include rn#include rn#include rn#include rnusing namespace std;rnrntypedef struct nodernrn int data;// 0 for island 1 for pondrn bool visited;rnrnnode;rnrnvoid recMark(node **Matrix, int y, int x, int flag)//rec find and markrnrn Matrix[y][x].visited = true;rn if(Matrix[y-1][x].data = flag)rn recMark(Matrix, y-1, x, flag);rn if(Matrix[y][x+1].data = flag)rn recMark(Matrix, y, x+1, flag);rn if(Matrix[y+1][x].data = flag)rn recMark(Matrix, y+1, x, flag);rn if(Matrix[y][x-1].data = flag)rn recMark(Matrix, y, x-1, flag);rnrnrnrnrnrnvoid loopFind(node **Matrix, int y , int x)//y and x represent the size of the matrixrnrn int pondCounts = 0;rn int islandCounts = 0;rn for(int _y = 1; _y <= y-2; _y++ )rn rn for(int _x = 1; _x <= x-2 ; _x++)rn rn if(Matrix[_y][_x].visited == false)//if not visited, then visit all relatedrn rn if(Matrix[_y][_x].data == 0)rn rn recMark(Matrix, _y, _x, 0);rn pondCounts++;rn rn else if(Matrix[_y][_x].data == 1)rn rn recMark(Matrix, _y, _x, 1);rn islandCounts++;rnrn rn rn rn rn cout<<"the number of pond: "<>y>>x;rn node theMatrix[y+2][x+2];rn for(int _x = 0;_x>theMatrix[tempX][tempY].data;rnrn rn rnrn //cout<<"reach here "<

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试