leetcode: 827. Making A Large Island

In a 2D grid of 0s and 1s, we change at most one 0 to a 1.

After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).

Example 1:

Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2:

Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 1.

Example 3:

Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 1.
 
 

First of all, We have to start all the islands from 2 numbered, then carries on the depth first search, if we have 1, keep to the four directions, until the number of zero or greater than 1, if we meet with 0, test whether there are other islands around 0, and calculate the total area of the surrounding islands. for example: if we get array image

after 	 depth first search,it have changed:		

image

we come up to 0(The 0 that was marked) at position (3,1)when traverse the fourth island, we're going to get the total area of the island in the top, left, and bottom directions. Add to that the area of the fourth island that we have traversed, and we get the maximum 18.

class Solution {
public:
	int largestIsland(vector<vector<int>>& g) {
		int n = g.size();
		vector<int> vCount(2,0);    //Storage area of islands with different Numbers
		int number = 2;                //We set the number of the first island to 2
		int maxNum = 1;                
		for (int i = 0; i<g.size(); i++)
			for (int j = 0; j<g[i].size(); j++)    
				if (g[i][j] == 1)
				{
					int others = 0;
					int count = 0;
					fun(g,vCount, count, i, j, number,others);    //Depth first search
					number++;
					vCount.push_back(count);
					maxNum = max(maxNum, count + others+1);
				}
		return maxNum>n*n ? n*n : maxNum;
	}
	void fun(vector<vector<int>>& g, vector<int>& vCount, int& count, int i, int j, int& number,int& others) {
		if (i<0 || i==g.size() || j<0 || j==g.size() || g[i][j] > 1) return;
		if (g[i][j] == 0) {
			int up(0), down(0), left(0),temp(0);  
			if (i > 0 && g[i - 1][j] != number) {
				temp += vCount[g[i - 1][j]];    //There is  a islands on the top of different number;
				up = g[i - 1][j];
			}
			if (i < g.size() - 1 && g[i + 1][j] != number&&g[i + 1][j] != up) {
				temp += vCount[g[i + 1][j]];   //There is  a islands below of different number;
				down = g[i + 1][j];
			}
			if (j > 0 && g[i][j - 1] != number&&g[i][j - 1] != up&&g[i][j - 1] != down) {
				temp += vCount[g[i][j - 1]];   //There is  a islands on the left of different number;
				left = g[i][j - 1];
			}
			if (j < g.size() - 1 && g[i][j + 1] != number&&g[i][j + 1] != up&&g[i][j + 1] != down&&g[i][j + 1] != left) 
				temp += vCount[g[i][j + 1]];       //There is  a islands on the right of different number;
			others = max(others, temp);    //If we change this 0 to 1, the total area of the surrounding islands
			return;
		}
		g[i][j] = number;
		count++;
		fun(g, vCount, count, i - 1, j, number,others);
		fun(g, vCount, count, i + 1, j, number, others);
		fun(g, vCount, count, i, j - 1, number, others);
		fun(g, vCount, count, i, j + 1, number, others);
	}
};


猜你喜欢

转载自blog.csdn.net/qq_41445952/article/details/80977267
今日推荐