题目
Sample Input
3 4 5
3 2
2 2
3 1
2 3
1 1
Sample Output
4
思想
dfs跑迷宫,类似于连续细胞的个数
AC 代码
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI acos(-1)
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e5+7;
const int maxn = 1e3+10;
int mp[maxn][maxn],num;
int d_x[]={1,-1,0,0};
int d_y[]={0,0,1,-1};
void dfs(int x,int y)
{
//标记这个坐标为走过的
mp[x][y] = 0;
for(int i=0;i<4;++i)
{
int a = x + d_x[i];
int b = y + d_y[i];
if(mp[a][b]==1)
{
num++;
dfs(a,b);
}
}
}
int main()
{
int n,m,k,ans;
while(~scanf("%d%d%d",&n,&m,&k))
{
ans = -1;
m(mp);
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x][y] = 1;
}
rep(i, 1, n)
rep(j,1,m){
if(mp[i][j]){
num = 1;
dfs(i,j);
ans = max(ans,num);
}
}
printf("%d\n",ans);
}
return 0;
}