poj3051
bfs
View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxw 85
#define maxh 1005
struct Point
{
int x, y;
Point(int xx, int yy) :
x(xx), y(yy)
{
}
Point()
{
}
} q[maxw * maxh];
int w, h;
char map[maxh][maxw];
bool vis[maxh][maxw];
int dir[4][2] =
{
{ 1, 0 },
{ 0, 1 },
{ -1, 0 },
{ 0, -1 } };
bool ok(Point &a)
{
if (a.x < 0 || a.y < 0 || a.x >= h || a.y >= w)
return false;
return !vis[a.x][a.y] && map[a.x][a.y] == '*';
}
int bfs(Point a)
{
int front = 0;
int rear = 0;
int ret = 0;
q[rear++] = a;
vis[a.x][a.y] = true;
while (front != rear)
{
Point p = q[front++];
ret++;
for (int i = 0; i < 4; i++)
{
Point b(p.x + dir[i][0], p.y + dir[i][1]);
if (ok(b))
{
vis[b.x][b.y] = true;
q[rear++] = b;
}
}
}
return ret;
}
void work()
{
int ans = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (!vis[i][j] && map[i][j] == '*')
ans = max(ans, bfs(Point(i, j)));
printf("%d\n", ans);
}
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &w, &h);
for (int i = 0; i < h; i++)
scanf("%s", map[i]);
work();
return 0;
}