[LEETCODE][694. Number of Distinct Islands]

题目描述

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) 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:

11 1

and

1 11

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.

分析

//小岛问题的话一般考虑进行上色,那么现在多了一个限定条件,如果可以转换的,算作一个
//那么现在关键变成,如何进行相同岛屿的判断
//用一个set来记录当前访问过的岛屿,并且岛屿的形状用一个string来表示,两者限定唯一性
//这个string,我们考虑用坐标来表示,假设当前的源点坐标为x0,y0
//以这个作为参考,每个点到当前源点两个坐标都进行记录,最终形成的岛屿序列我们作为当前岛屿标志

 

//dfs
class Solution {
public:
    int numDistinctIslands(vector<vector<int>>& grid) {
        if(!grid.size() || !grid[0].size())
            return 0;
        unordered_set<string> myset;
        for(int i=0;i<grid.size();++i){
            for(int j=0;j<grid[0].size();++j){
                if(grid[i][j]){
                    string repre; //当前岛屿的标志
                    dfs(grid,i,j,i,j,repre);
                    myset.insert(repre);
                }
            }
        }
        return myset.size();
    }
private:
    void dfs(vector<vector<int>>& grid,int x,int y,int tarx,int tary,string& s){
        if(!isValid(grid,x,y))
            return;
        if(grid[x][y]==0)
            return;
        grid[x][y]=0; //当前点标记为已访问
        s+=to_string(x-tarx)+to_string(y-tary);
        dfs(grid,x-1,y,tarx,tary,s);
        dfs(grid,x+1,y,tarx,tary,s);
        dfs(grid,x,y-1,tarx,tary,s);
        dfs(grid,x,y+1,tarx,tary,s);
    }
    bool isValid(vector<vector<int>>& grid,int x,int y){
        if(x<0||x>=grid.size()||y<0||y>=grid[0].size())
            return false;
        return true;
    }
};
//bfs的写法
class Solution {
public:
    int numDistinctIslands(vector<vector<int>>& grid) {
        if(!grid.size() || !grid[0].size())
            return 0;
        int m=grid.size(),n=grid[0].size();
        vector<int> offsets= {0, 1, 0, -1, 0}; //方向数组,用于四个方向上的扩展
        unordered_set<string> islands;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j]) {
                    grid[i][j] = 0;
                    string island;
                    queue<pair<int,int>> todo; //周围相连的,值为1的所有点的坐标
                    todo.push({i, j});
                    while (!todo.empty()) {
                        pair<int,int> p = todo.front();
                        todo.pop();
                        for (int k = 0; k < 4; k++) {
                            int r = p.first + offsets[k], c = p.second + offsets[k + 1]; //下一个点的坐标
                            if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c]) {
                                grid[r][c] = 0;
                                todo.push({r, c});
                                island += to_string(r - i) + to_string(c - j); //记录对应的string
                            }
                        }
                    }
                    islands.insert(island);
                }
            }
        }
        return islands.size();
    }
};

总结 

基础的bfs和dfs是好写的,关键是一些特殊的题目,如何进行唯一性的标识,可能是上色,可能是string标识,需要多总结
全部评论

相关推荐

05-12 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
牛马43373018...:这人真懂什么叫熵吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务