Consider a matrix with rows and columns, where each cell contains either a or a and any cell containing a is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally; in other words, cell is connected to cells , , , , , , , and , provided that the location exists in the matrix for that .
If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to zero or more cells in the region but is not necessarily directly connected to all the other cells in the region.
Task
Given an matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.
Input Format
The first line contains an integer, , denoting the number of rows in the matrix.
The second line contains an integer, , denoting the number of columns in the matrix.
Each line of the subsequent lines contains space-separated integers describing the respective values filling each row in the matrix.
Constraints
Output Format
Print the number of cells in the largest region in the given matrix.
Sample Input
4
4
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0
Sample Output
5
Explanation
The diagram below depicts two regions of the matrix; for each region, the component cells forming the region are marked with an X
:
X X 0 0 1 1 0 0
0 X X 0 0 1 1 0
0 0 X 0 0 0 1 0
1 0 0 0 X 0 0 0
The first region has five cells and the second region has one cell. Because we want to print the number of cells in the largest region of the matrix, we print .
//思路:就是dfs连通快
AC代码:
#include <iostream>
using namespace std;
int m,n,maze[11][11];
int dfs(int x,int y)
{
maze[x][y]=0;
int sum=1;
for(int i=-1;i<=1;++i)
{
for(int j=-1;j<=1;++j)
{
int newx=x+i,newy=y+j;
if(newx>=0&&newx<m&&newy>=0&&newy<n&&maze[newx][newy]==1)
{
sum+=dfs(newx,newy);
}
}
}
return sum;
}
int main()
{
cin>>m>>n;
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
cin>>maze[i][j];
int max_n=0;
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
if(maze[i][j]==1)
{
max_n=max(max_n,dfs(i,j));
}
}
}
cout<<max_n<<endl;
return 0;
}