Fortune law basis class5- left island of title 5

1. Title: problem Island

Only two kinds of values 0 and 1 in a matrix, and each location can own up, down, left, and right position is connected, if there is a 1 together, this part is called an island, Seeking a matrix have how many islands?
For example:
0 0 0. 1. 1 0
. 1. 1. 1 0 0. 1
. 1. 1 0 0 0 0
0 0 0 0 0 0
The matrix has three islands.

2. Analysis

Thinking this question is very simple, each element of the matrix is ​​traversed. If the current element is 1, the number of islands plus 1, then the current element to 2 (indicates that the island had been recorded), recursive search up and down four of its elements, if there are elements of 1, continue to 2 (phase o 1 is the same island, it has been recorded over), constantly up and down about four recursive element until the element is not 1 or look out of bounds.

3. core code

(1) The recursion Island

We recursive function is defined as infect, infect the same function as the function, if the element found is 1, continue to look at whether there are elements of the four elements around a value of 1.
Note:
Conditions ① recursive direct return is an array of cross-border element and the current value is not 1;
② need to first current element value to 2 recursively, or will continue to look for loop

void infect(int a[][width],int j,int i)  //传二维数组需将第二维指定
{
	if(j < 0||j >= width ||i < 0|| i >= height || a[i][j] != 1)
		return;
	a[i][j] = 2;
	//查找上下左右四个元素
	infect(a,j - 1,i);
	infect(a,j + 1,i);
	infect(a,j,i - 1);
	infect(a,j,i + 1);
}

(2) the number of the island's demand

To traverse the matrix, if we find elements of the elements of the value 1, the number of islands increased by 1 and calls the recursive function to find the entire island. Finally, return the number of islands.

int countIslands(int a[][width])
{
	int num = 0;
	for(int i = 0;i < height;i++)
	{
		for(int j = 0;j < width;j++)
		{
			if(a[i][j] == 1)
			{
				num++;
				infect(a,j,i);
			}
		}
	}
	return num;
}

4. The complete code

#include<iostream>
//#include<vector>
using namespace std;
#define width 6
#define height 4

void infect(int a[][width],int j,int i)
{
	if(j < 0||j >= width ||i < 0|| i >= height || a[i][j] != 1)
		return;
	a[i][j] = 2;
	infect(a,j - 1,i);
	infect(a,j + 1,i);
	infect(a,j,i - 1);
	infect(a,j,i + 1);
}

int countIslands(int a[][width])
{
	int num = 0;
	for(int i = 0;i < height;i++)
	{
		for(int j = 0;j < width;j++)
		{
			if(a[i][j] == 1)
			{
				num++;
				infect(a,j,i);
			}
		}
	}
	return num;
}

int main()
{
	int a[height][width] = {
		{0,0,1,0,1,0},
		{1,1,1,0,1,0},
		{1,0,0,1,0,0},
		{0,0,0,0,0,0}};
	cout<<countIslands(a);

	system("pause");
	return 0;
}

5. Extension: Parallel Problems

If you ask for a lot of how to solve the matrix. Using parallel thinking, the island is divided into n, separately. Island in the border will be more likely to be considered, because if the neighboring island on the border is 1, each part will be recorded separately, the sum of it overcharged. Solution is to: design variables, records the beginning of each island. Such re-examination when the boundary, if two different islands starting position, the number of islands minus 1 (actually an island), then the same two islands merge starting position.

Published 51 original articles · won praise 1 · views 1367

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104226582