题解
2023-07-06 16:54:17
发布于:广东
13阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int a[101][101],c , maxs = 0;
bool visit[101][101] = {0};
int n, m;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
void search(int x,int y){
visit[x][y] = 1;
c++;
for (int i = 0; i <= 3; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if (visit[xx][yy] == 0 && a[xx][yy] == 1 && xx > 0 && yy > 0 && xx <= n && yy <= m){
search(xx,yy);
}
}
}
int main(){
cin >> n;
cin >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (visit[i][j] == 0 && a[i][j]){
c = 0;
search(i,j);
}
maxs = max(c, maxs);
}
}
cout << maxs;
}
这里空空如也
有帮助,赞一个