P19945 [GeeksforGeeks] Maximum connected group
提高-
题目描述

You are given an n x n binary grid. A grid is said to be binary if every value in grid is either 1 or 0.
You can change at most one cell in grid from 0 to 1.
You need to find the largest group of connected 1's.
Two cells are said to be connected if both are adjacent to each other and both have same value.
Example 1
Input:
2
1 1
0 1
Output:
4
Explanation:
By changing cell (2,1) ,we can obtain a connected group of 4 1's
1 1
1 1

Example 2
Input:
3
1 0 1
1 0 1
1 0 1
Output:
7
Explanation:
By changing cell (3,2) ,we can obtain a connected group of 7 1's
1 0 1
1 0 1
1 1 1
Your Task:
You don't need to read or print anything. Your task is to complete the function MaxConnection() which takes a matrix grid[][] denoting the grid and return the maximum group of connected group of 1s.

Constraints:
1 <= n<= 500
0 <= grid[i][j] <= 1

输入格式

见题面

输出格式

见题面

样例输入
2 1 1 0 1
样例输出
4