LeetCode OJ Number of Islands

Given a 2d grid map of ‘1‘s (land) and‘0‘s
(water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110110101100000000

Answer: 1

Example 2:

11000110000010000011

Answer: 3

Credits:

Special thanks to @mithmatt for adding this problem and creating all test cases.

int R, C;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
bool ** vis;
char ** G;

void dfs(int pi, int pj) {
	vis[pi][pj] = true;
	int npi, npj;
	for (int i = 0; i < 4; i++) {
		npi = pi + dir[i][0];
		npj = pj + dir[i][1];
		if (0 <= npi && npi < R && 0 <= npj && npj < C && !vis[npi][npj] && G[npi][npj] == '1') dfs(npi, npj);
	}
}

int numIslands(char **grid, int numRows, int numColumns) {
	G = grid;
	vis = (bool **)malloc(sizeof(bool*) * numRows);
	for (int i = 0; i < numRows; i++) vis[i] = (bool *)malloc(sizeof(bool) * numColumns);
	for (int i = 0; i < numRows; i++)
		for (int j = 0; j < numColumns; j++) vis[i][j] = false;
	int ans = 0;
	R = numRows;
	C = numColumns;
	for (int i = 0; i < numRows; i++)
		for (int j = 0; j < numColumns; j++)
			if (!vis[i][j] && G[i][j] == '1') {
				ans++;
				dfs(i, j);
			}
	for (int i = 0; i < numRows; i++) free(vis[i]);
	free(vis);
	return ans;
}
时间: 2024-12-29 16:27:02

LeetCode OJ Number of Islands的相关文章

[LeetCode] 200. Number of Islands 岛屿的数量

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or v

[LeetCode][JavaScript]Number of Islands

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are

Leetcode 200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[LeetCode] 200. Number of Islands 解题思路

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

leetcode 200. Number of Islands(DFS)

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

【LeetCode】Number of Islands 解题报告

[题目] Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounde

【LeetCode】Number of Islands

Number of Islands 问题描述 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid

Leetcode 305. Number of Islands II

Problem: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each