`

Find Islands

 
阅读更多

Given matrix MxN of 0s and 1s, return and output all groups of adjacent 1s in form of (row,col)...

1s are considered adjacent if they are adjacent horizontally or vertically.

1 1 0 1 0 1
1 1 1 1 0 1
0 0 0 0 1 1
1 0 1 0 1 0

Output:

A: (0,0)(1,0)(0,1)(1,1)(1,2)(1,3)(0,3)
B: (0,5)(1,5)(2,4)(2,5)(3,4)
C: (3,0)
D: (3,2)

Order in the output is insignificant.

#include <iostream>
#include <vector>
#include <functional>
#include <unordered_map>
using namespace std;

// solution with changing the original matrix
vector<vector<pair<int,int>>> findIslands(vector<vector<int>>& mat) {
	int m = mat.size(), n = mat[0].size();
	vector<vector<pair<int,int>>> res;
	vector<pair<int,int>> list;
	vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
	function<void(int,int)> dfs = [&](int r, int c) {
		list.emplace_back(r,c);
		mat[r][c] = 0;
		for(auto& dir:dirs) {
			int i = r+dir[0], j = c+dir[1];
			if(i<0 || j<0 || i>=m || j>=n || !mat[i][j]) continue;
			dfs(i, j);
		}
	};
	for(int i=0; i<m; i++) {
		for(int j=0; j<n; j++) {
			if(mat[i][j]) {
				list.clear();
				dfs(i, j);
				res.push_back(list);
			}
		}
	}
	return res;
}

// solution without changing the original matrix, using additional visited bool matrix
vector<vector<pair<int,int>>> findIslands1(vector<vector<int>>& mat) {
	int m = mat.size(), n = mat[0].size();
	vector<vector<pair<int,int>>> res;
	vector<pair<int,int>> list;
	vector<vector<bool>> visited(m, vector<bool>(n));
	vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
	function<void(int,int)> dfs = [&](int r, int c) {
		list.emplace_back(r,c);
		visited[r][c] = true;
		for(auto& dir:dirs) {
			int i = r+dir[0], j = c+dir[1];
			if(i<0 || j<0 || i>=m || j>=n || !mat[i][j] || visited[i][j]) continue;
			dfs(i, j);
		}
	};
	for(int i=0; i<m; i++) {
		for(int j=0; j<n; j++) {
			if(mat[i][j] && !visited[i][j]) {
				list.clear();
				dfs(i, j);
				res.push_back(list);
			}
		}
	}
	return res;
}

// solution without changing the original matrix and without additional memory space
vector<unordered_map<int,vector<pair<int,int>>>> findIslands2(vector<vector<int>>& mat) {
	int m = mat.size(), n = mat[0].size();
	vector<unordered_map<int,vector<pair<int,int>>>> res;
	unordered_map<int,vector<pair<int,int>>> map;
	vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
	
	auto exists = [&](int r, int c) {
		for(auto& mp:res) {
			if(mp.count(r)) {
				for(auto& p:mp[r])
					if(p.first <= c && p.second >= c)
						return true;
			}
		}
		return false;
	};
	
	function<void(int,int)> dfs = [&](int r, int c) {
		if(map.count(r)) {
			bool inserted = false;
			for(auto& p:map[r]) {
				if(p.first <= c && p.second >= c) return;
				if(c+1 == p.first) {
					p.first = c;
					inserted = true; 
					break;
				} else if(c-1 == p.second) {
					p.second = c;
					inserted = true; 
					break;
				}
			}
			if(!inserted)
				map[r].push_back({c, c});
		} else {
			map[r].push_back({c, c});
		}
		for(auto& dir:dirs) {
			int i = r+dir[0], j = c+dir[1];
			if(i<0 || j<0 || i>=m || j>=n || !mat[i][j]) continue;
			dfs(i, j);
		}
	};
	
	for(int i=0; i<m; i++) {
		for(int j=0; j<n; j++) {
			if(mat[i][j] && !exists(i,j)) {
				map.clear();
				dfs(i, j);
				res.push_back(map);
			}
		}
	}
	return res;
}

int main() {
	vector<vector<int>> mat = { {1, 1, 0, 1, 0, 1},
								{1, 1, 1, 1, 0, 1},
								{0, 0, 0, 0, 1, 1},
								{1, 0, 1, 0, 1, 0}};
	auto res1 = findIslands1(mat);
	for(auto& list:res1) {
		for(auto& p:list)
			cout << "(" << p.first << "," << p.second << ") ";
		cout << endl;
	}
	
	auto res2 = findIslands2(mat);
	for(auto& map:res2) {
		for(auto& kv:map)
			for(auto& pair:kv.second)
				for(int i=pair.first; i<=pair.second; i++)
					cout << "(" << kv.first << "," << i << ") ";
		cout << endl;
	}
	return 0;
}
分享到:
评论

相关推荐

    ISLANDS:查找矩阵中所有 4 连通分量的“孤岛”。-matlab开发

    ISLANDS 查找矩阵中所有 4 连通分量的“岛”。 该函数将返回三个输出参数,包括输入矩阵中最大的连接岛。 有关所有说明,请参阅详细帮助。 灵感来自最近发布的益智游戏: ...

    java-leetcode题解之Number of Islands II.java

    针对这个问题,LeetCode上的Number of Islands II题目要求通过编写算法来解决这个问题。本篇题解将详细探讨如何使用Java语言实现这一问题的解决方案。 首先,理解问题的背景至关重要。在Number of Islands II的问题...

    14islands-com:14islands旧v2网页

    新的14islands网站(2014年版)。 设置 npm install bower install 如果您没有gem install bundle软件: gem install bundle bundle install 跑步 grunt serve在开发模式下的上运行该站点以进行查看。 使用ruby...

    islands.csv

    由于给出的文件信息中【标题】为"islands.csv",【描述】为"islands",而【标签】为空,且【部分内容】未提供,所以直接生成知识点的素材将会有限。不过我们可以基于文件名和描述推测其可能涉及的内容,并构建相关...

    java-leetcode题解之Number of Islands.java

    LeetCode上有一道经典的算法题,题目名为“Number of Islands”,即“岛屿数量”。在编程语言Java中,解决这一问题的算法通常涉及到深度优先搜索(DFS)的实现。岛屿问题本质上是使用二维数组模拟一个网格,其中“1...

    java-leetcode题解之Number of Closed Islands.java

    在处理给定的文件内容时,我们可以聚焦于解析"Number of Closed Islands"问题,并讨论其在Java语言中的实现。在这部分中,我们将详细介绍关于该问题的背景、概念、关键点以及Java实现的代码逻辑。 首先,"Number of...

    java-leetcode题解之Number of Big Islands.java

    Java LeetCode题解之Number of Big Islands是一篇针对特定编程问题的详细解决方案文章,它使用Java语言编写。该问题涉及到图论和深度优先搜索算法。在该题目中,需要识别在一个二维网格中,由“1”组成的大型岛屿的...

    unity3d 游戏场景模型 Islands unity3d 地形公路场景模型.zip模型资源unity模型资源下载

    unity3d 游戏场景模型 Islands unity3d 地形公路场景模型.zip模型资源unity模型资源下载unity3d 游戏场景模型 Islands unity3d 地形公路场景模型.zip模型资源unity模型资源下载unity3d 游戏场景模型 Islands unity3d...

    python-leetcode题解之200-Number-of-Islands.py

    对于200号问题“Number of Islands”,这是一个典型的图论问题,主要考察了深度优先搜索(DFS)和广度优先搜索(BFS)算法的应用。在Python中,解决此类问题通常会使用递归或队列等数据结构。 此题目的核心是求解...

    Java Low Poly Islands v1.0

    一大批岛屿资产供您在下一Unity项目中使用!包括热带岛屿、火山岛、热带山脉、植被、乡村房屋、木板路、船只、粒子、后期FX等。 适用于原型设计、移动、LOD或风格化游戏。 模块化部分很容易在Unity网格上组装在一起...

    Floating Islands - Fantasy Environment Pack.rar

    《浮空岛屿:幻想环境包》是为游戏开发者和虚拟世界构建者提供的一款资源集合,主要适用于Unity引擎。这个资源包以独特的浮空岛屿为主题,为创造梦幻般的奇幻世界提供了丰富的素材。它不仅包含了设计精美的环境元素...

    卡通农场岛屿:Cartoon Farm Islands Exteriors 1.0

    10 个岛屿 + 175 个资产 整个集合使用一种纹理(2048/1024/512/256/128 像素)和一种材质。 全套有 33.5 万个三角形(外部)。 外部道具: 建筑物(房屋、磨坊、谷仓、仓库、平台、水井)。 景观 / 植被(树木、树桩...

    islands_vue_client:具有Vue组件和Vuex的“孤岛游戏”的Web界面

    离岛Vue客户具有Vue组件和Vuex的“孤岛游戏”的Web界面。基于Lance Halvorsen的《 》一书。 要启动Phoenix服务器: 使用mix deps.get安装依赖mix deps.get 使用npm install在assets目录中安装Node.js依赖项使用mix ...

    unity3d群岛场景资源包Super Islands

    雪山延绵不断,有层层叠叠的山峦,树木重重,白雪皑皑,有你想要,下你想载,需要的赶快下载吧

    responsive-landing-page-islands-travel

    "responsive-landing-page-islands-travel"项目可能是一个专门针对岛屿旅游的响应式网站落地页,旨在为用户提供无缝且吸引人的浏览体验,无论他们是在桌面、平板还是手机上访问。 SCSS,全称Sass CSS,是一种预...

    Ecotourism and sustainability in Mediterranean islands

    Ecotourism and sustainability in Mediterranean islands T 427 Ecotourism and Sustainability in Mediterranean Islands Dimitrios Diamantis Executive Summary Sustainable and ecotourism practices in...

    findIslands(input,m​ode,threshold):在向量中查找特定值的岛。-matlab开发

    在MATLAB编程环境中,`findIslands`函数是一个自定义函数,用于在向量中查找特定值的连续区域,这些区域被其他不同的值所包围,我们称之为“孤岛”。这个功能在数据分析和处理中非常有用,特别是在寻找某种特定特征...

    百慕大群岛(英)Bermuda Islands (UK)112.xlsx

    百慕大群岛是位于北大西洋的一组群岛,由约138个岛屿组成,这些岛屿隶属于英国,但享有高度自治。百慕大群岛的邮政编码系统与英国本土的系统有所不同,采用的是四位数字编码。这些编码为百慕大群岛上的每个地区和...

Global site tag (gtag.js) - Google Analytics