http://acm.hdu.edu.cn/showproblem.php?pid=1312
//深度优先搜索
#include <iostream>
using namespace std;
char map[30][30];
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
int M, N, countn;
void dfs(int x, int y)
{
int i;
int X, Y;
if(map[x][y]=='#')
{
return;
}
else
{
map[x][y]='#';
countn++;
for(i = 0; i < 4; i++)
{
X = x + dir[i][0];
Y = y + dir[i][1];
if(X>=0&&X<N&&Y>=0&&Y<M&&map[X][Y]!='#')
{
dfs(X, Y);
}
}
}
}
int main()
{
int i, j;
int sx, sy;
while(cin>>M>>N,M+N)
{
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
sx = i;
sy = j;
}
}
}
countn = 0;
dfs(sx, sy);
cout<<countn<<endl;
}
}
//广度优先搜索
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define MAXN 25
char mapmap[MAXN][MAXN];
int visited[MAXN][MAXN];
int w, h;
struct point
{
int x, y;
};
int num;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
int i, j;
point cur, temp, p;
while (cin >> w >> h, w||h)
{
for (i = 0; i < h; i++)
{
for (j = 0; j < w; j++)
{
cin >> mapmap[i][j];
if (mapmap[i][j] == '@')
{
cur.x = i;
cur.y = j;
}
}
}
memset(visited, 0, sizeof (visited));
num = 1;
visited[cur.x][cur.y] = 1;
queue<point> myqueue;
myqueue.push(cur);
while (!myqueue.empty())
{
temp = myqueue.front();
myqueue.pop();
for (i = 0; i < 4; i++)
{
int x = temp.x + dir[i][0];
int y = temp.y + dir[i][1];
if (!visited[x][y] && mapmap[x][y] == '.' && x >= 0 && x < h && y >= 0 && y < w)
{
visited[x][y] = 1;
p.x = x;
p.y = y;
myqueue.push(p);
num++;
}
}
}
cout << num << endl;
}
return 0;
}