简单的dfs:
#include<iostream>
using namespace std;
const int MAXN = 55;
int count, a[MAXN][MAXN], used[MAXN][MAXN];
void dfs(int i, int j)
{
used[i][j] = 1;
++count;
if (a[i][j] % 2 == 0) //向西的方向
{
if (!used[i][j - 1])
{
dfs(i, j - 1);
}
}
a[i][j] /= 2;
if (a[i][j] % 2 == 0) //向北的方向
{
if (!used[i - 1][j])
{
dfs(i - 1, j);
}
}
a[i][j] /= 2;
if (a[i][j] % 2 == 0) //向东的方向
{
if (!used[i][j + 1])
{
dfs(i, j + 1);
}
}
a[i][j] /= 2;
if (a[i][j] % 2 == 0) //向南的方向
{
if (!used[i + 1][j])
{
dfs(i + 1,j);
}
}
}
int main()
{
//freopen("1.txt", "r", stdin);
int i, j, r, c;
scanf("%d%d", &r, &c);
for (i = 1; i <= r; i ++)
{
for (j = 1; j <= c; j ++)
{
scanf("%d", &a[i][j]);
used[i][j] = 0;
}
used[i][0] = used[i][c + 1] = 1;
}
for (j = 0; j <= c + 1; j++)
{
used[0][j] = used[r + 1][j] = 1;
}
int max = 0, num = 0;
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
if (!used[i][j])
{
count = 0;
num++;
dfs(i, j);
if (count > max)
{
max = count;
}
}
}
}
printf("%d/n%d/n", num, max);
return 0;
}