DFS
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
char s[30][30];
bool t[30][30];
int n, m;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dfs(PII st) {
t[st.first][st.second] = true;
int ans = 1;
for (int i = 0; i < 4; i ++) {
int x = dx[i] + st.first, y = dy[i] + st.second;
if (x >= 1 && x <= m && y >= 1 && y <= n && s[x][y] == '.' && !t[x][y]) {
t[x][y] = true;
PII c = {x, y};
ans += dfs(c);
}
}
return ans;
}
int main() {
while (cin >> n >> m) {
if (!n && !m) break;
PII st;
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
cin >> s[i][j];
if (s[i][j] == '@') {
st = {i, j};
}
}
}
memset(t, 0, sizeof t);
int ans = dfs(st);
cout << ans << endl;
}
return 0;
}
BFS
#include<cstring>
#include<queue>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
char s[30][30];
bool t[30][30];
int n, m;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int bfs(PII st) {
t[st.x][st.y] = true;
queue<PII>q;
int ans = 1;
q.push(st);
while(q.size())
{
PII f = q.front();
q.pop();
for (int i = 0; i < 4; i ++) {
int x = dx[i] + f.x, y = dy[i] + f.y;
if(x >= 1 && x <= m && y >= 1 && y <= n && s[x][y] == '.' && !t[x][y]) {
t[x][y] = true;
PII c = {x, y};
q.push(c);
ans ++;
}
}
}
return ans;
}
int main() {
while(cin >> n >> m){
if(!n && !m) break;
PII st;
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
cin >> s[i][j];
if (s[i][j] == '@') {
st = {i, j};
}
}
}
memset(t,0,sizeof t);
int ans = bfs(st);
cout << ans << endl;
}
return 0;
}